1 { config, lib, pkgs, ... }:
13 cfg = config.services.esphome;
15 stateDir = "/var/lib/esphome";
18 if cfg.enableUnixSocket
19 then "--socket /run/esphome/esphome.sock"
20 else "--address ${cfg.address} --port ${toString cfg.port}";
23 meta.maintainers = with maintainers; [ oddlama ];
25 options.services.esphome = {
26 enable = mkEnableOption "esphome, for making custom firmwares for ESP32/ESP8266";
28 package = lib.mkPackageOption pkgs "esphome" { };
30 enableUnixSocket = mkOption {
33 description = "Listen on a unix socket `/run/esphome/esphome.sock` instead of the TCP port.";
38 default = "localhost";
39 description = "esphome address";
45 description = "esphome port";
48 openFirewall = mkOption {
51 description = "Whether to open the firewall for the specified port.";
54 allowedDevices = mkOption {
55 default = ["char-ttyS" "char-ttyUSB"];
56 example = ["/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0"];
58 A list of device nodes to which {command}`esphome` has access to.
59 Refer to DeviceAllow in systemd.resource-control(5) for more information.
60 Beware that if a device is referred to by an absolute path instead of a device category,
61 it will only allow devices that already are plugged in when the service is started.
63 type = types.listOf types.str;
69 description = "Use ping to check online status of devices instead of mDNS";
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"];
83 # platformio fails to determine the home directory when using DynamicUser
84 PLATFORMIO_CORE_DIR = "${stateDir}/.platformio";
85 } // lib.optionalAttrs cfg.usePing { ESPHOME_DASHBOARD_USE_PING = "true"; };
88 ExecStart = "${cfg.package}/bin/esphome dashboard ${esphomeParams} ${stateDir}";
92 WorkingDirectory = stateDir;
93 StateDirectory = "esphome";
94 StateDirectoryMode = "0750";
95 Restart = "on-failure";
96 RuntimeDirectory = mkIf cfg.enableUnixSocket "esphome";
97 RuntimeDirectoryMode = "0750";
100 CapabilityBoundingSet = "";
101 LockPersonality = true;
102 MemoryDenyWriteExecute = true;
103 DevicePolicy = "closed";
104 DeviceAllow = map (d: "${d} rw") cfg.allowedDevices;
105 SupplementaryGroups = ["dialout"];
106 #NoNewPrivileges = true; # Implied by DynamicUser
108 #PrivateTmp = true; # Implied by DynamicUser
110 ProtectControlGroups = true;
112 ProtectHostname = false; # breaks bwrap
113 ProtectKernelLogs = false; # breaks bwrap
114 ProtectKernelModules = true;
115 ProtectKernelTunables = false; # breaks bwrap
116 ProtectProc = "invisible";
117 ProcSubset = "all"; # Using "pid" breaks bwrap
118 ProtectSystem = "strict";
119 #RemoveIPC = true; # Implied by DynamicUser
120 RestrictAddressFamilies = [
126 RestrictNamespaces = false; # Required by platformio for chroot
127 RestrictRealtime = true;
128 #RestrictSUIDSGID = true; # Implied by DynamicUser
129 SystemCallArchitectures = "native";
132 "@mount" # Required by platformio for chroot