1 { config, lib, pkgs, ... }:
14 cfg = config.services.esphome;
16 stateDir = "/var/lib/esphome";
19 if cfg.enableUnixSocket
20 then "--socket /run/esphome/esphome.sock"
21 else "--address ${cfg.address} --port ${toString cfg.port}";
24 meta.maintainers = with maintainers; [ oddlama ];
26 options.services.esphome = {
27 enable = mkEnableOption (mdDoc "esphome");
31 default = pkgs.esphome;
32 defaultText = literalExpression "pkgs.esphome";
33 description = mdDoc "The package to use for the esphome command.";
36 enableUnixSocket = mkOption {
39 description = lib.mdDoc "Listen on a unix socket `/run/esphome/esphome.sock` instead of the TCP port.";
44 default = "localhost";
45 description = mdDoc "esphome address";
51 description = mdDoc "esphome port";
54 openFirewall = mkOption {
57 description = mdDoc "Whether to open the firewall for the specified port.";
60 allowedDevices = mkOption {
61 default = ["char-ttyS" "char-ttyUSB"];
62 example = ["/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0"];
63 description = lib.mdDoc ''
64 A list of device nodes to which {command}`esphome` has access to.
65 Refer to DeviceAllow in systemd.resource-control(5) for more information.
66 Beware that if a device is referred to by an absolute path instead of a device category,
67 it will only allow devices that already are plugged in when the service is started.
69 type = types.listOf types.str;
73 config = mkIf cfg.enable {
74 networking.firewall.allowedTCPPorts = mkIf (cfg.openFirewall && !cfg.enableUnixSocket) [cfg.port];
76 systemd.services.esphome = {
77 description = "ESPHome dashboard";
78 after = ["network.target"];
79 wantedBy = ["multi-user.target"];
82 # platformio fails to determine the home directory when using DynamicUser
83 environment.PLATFORMIO_CORE_DIR = "${stateDir}/.platformio";
86 ExecStart = "${cfg.package}/bin/esphome dashboard ${esphomeParams} ${stateDir}";
90 WorkingDirectory = stateDir;
91 StateDirectory = "esphome";
92 StateDirectoryMode = "0750";
93 Restart = "on-failure";
94 RuntimeDirectory = mkIf cfg.enableUnixSocket "esphome";
95 RuntimeDirectoryMode = "0750";
98 CapabilityBoundingSet = "";
99 LockPersonality = true;
100 MemoryDenyWriteExecute = true;
101 DevicePolicy = "closed";
102 DeviceAllow = map (d: "${d} rw") cfg.allowedDevices;
103 SupplementaryGroups = ["dialout"];
104 #NoNewPrivileges = true; # Implied by DynamicUser
106 #PrivateTmp = true; # Implied by DynamicUser
108 ProtectControlGroups = true;
110 ProtectHostname = true;
111 ProtectKernelLogs = true;
112 ProtectKernelModules = true;
113 ProtectKernelTunables = true;
114 ProtectProc = "invisible";
115 ProcSubset = "all"; # Using "pid" breaks bwrap
116 ProtectSystem = "strict";
117 #RemoveIPC = true; # Implied by DynamicUser
118 RestrictAddressFamilies = [
124 RestrictNamespaces = false; # Required by platformio for chroot
125 RestrictRealtime = true;
126 #RestrictSUIDSGID = true; # Implied by DynamicUser
127 SystemCallArchitectures = "native";
130 "@mount" # Required by platformio for chroot