summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimon Weber <sweber2342@gmail.com>2019-10-27 18:28:53 +0100
committerSimon Weber <sweber2342@gmail.com>2020-07-20 21:48:14 +0200
commit1af8759693aa0e37e81597e4e410a23d709a7a41 (patch)
tree66b9e7fa9fa94401e676f21ce8af9688e431c239
parent85091e0181cb245f2de76e0164a245a7e7c7cacf (diff)
downloadnixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar.gz
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar.bz2
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar.lz
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar.xz
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.tar.zst
nixpkgs-1af8759693aa0e37e81597e4e410a23d709a7a41.zip
nixos/zigbee2mqtt: init
-rw-r--r--nixos/modules/misc/ids.nix2
-rw-r--r--nixos/modules/module-list.nix1
-rw-r--r--nixos/modules/services/misc/zigbee2mqtt.nix98
3 files changed, 101 insertions, 0 deletions
diff --git a/nixos/modules/misc/ids.nix b/nixos/modules/misc/ids.nix
index 3409e7ba22e..4692ea32656 100644
--- a/nixos/modules/misc/ids.nix
+++ b/nixos/modules/misc/ids.nix
@@ -345,6 +345,7 @@ in
       zoneminder = 314;
       paperless = 315;
       #mailman = 316;  # removed 2019-08-30
+      zigbee2mqtt = 317;
 
       # When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399!
 
@@ -645,6 +646,7 @@ in
       zoneminder = 314;
       paperless = 315;
       #mailman = 316;  # removed 2019-08-30
+      zigbee2mqtt = 317;
 
       # When adding a gid, make sure it doesn't match an existing
       # uid. Users and groups with the same name should have equal
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index f361163ca63..a3bc489d0b7 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -510,6 +510,7 @@
   ./services/misc/uhub.nix
   ./services/misc/weechat.nix
   ./services/misc/xmr-stak.nix
+  ./services/misc/zigbee2mqtt.nix
   ./services/misc/zoneminder.nix
   ./services/misc/zookeeper.nix
   ./services/monitoring/alerta.nix
diff --git a/nixos/modules/services/misc/zigbee2mqtt.nix b/nixos/modules/services/misc/zigbee2mqtt.nix
new file mode 100644
index 00000000000..0957920f1a0
--- /dev/null
+++ b/nixos/modules/services/misc/zigbee2mqtt.nix
@@ -0,0 +1,98 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+let
+  cfg = config.services.zigbee2mqtt;
+
+  configJSON = pkgs.writeText "configuration.json"
+    (builtins.toJSON (recursiveUpdate defaultConfig cfg.config));
+  configFile = pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } ''
+    ${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out
+  '';
+
+  # the default config contains all required settings,
+  # so the service starts up without crashing.
+  defaultConfig = {
+    homeassistant = false;
+    permit_join = false;
+    mqtt = {
+      base_topic = "zigbee2mqtt";
+      server = "mqtt://localhost:1883";
+    };
+    serial.port = "/dev/ttyACM0";
+    # put device configuration into separate file because configuration.yaml
+    # is copied from the store on startup
+    devices = "devices.yaml";
+  };
+in
+{
+  meta.maintainers = with maintainers; [ sweber ];
+
+  options.services.zigbee2mqtt = {
+    enable = mkEnableOption "enable zigbee2mqtt service";
+
+    package = mkOption {
+      description = "Zigbee2mqtt package to use";
+      default = pkgs.zigbee2mqtt.override {
+        dataDir = cfg.dataDir;
+      };
+      defaultText = "pkgs.zigbee2mqtt";
+      type = types.package;
+    };
+
+    dataDir = mkOption {
+      description = "Zigbee2mqtt data directory";
+      default = "/var/lib/zigbee2mqtt";
+      type = types.path;
+    };
+
+    config = mkOption {
+      default = {};
+      type = with types; nullOr attrs;
+      example = literalExample ''
+        {
+          homeassistant = config.services.home-assistant.enable;
+          permit_join = true;
+          serial = {
+            port = "/dev/ttyACM1";
+          };
+        }
+      '';
+      description = ''
+        Your <filename>configuration.yaml</filename> as a Nix attribute set.
+      '';
+    };
+  };
+
+  config = mkIf (cfg.enable) {
+    systemd.services.zigbee2mqtt = {
+      description = "Zigbee2mqtt Service";
+      wantedBy = [ "multi-user.target" ];
+      after = [ "network.target" ];
+      serviceConfig = {
+        ExecStart = "${cfg.package}/bin/zigbee2mqtt";
+        User = "zigbee2mqtt";
+        WorkingDirectory = cfg.dataDir;
+        Restart = "on-failure";
+        ProtectSystem = "strict";
+        ReadWritePaths = cfg.dataDir;
+        PrivateTmp = true;
+        RemoveIPC = true;
+      };
+      preStart = ''
+        cp --no-preserve=mode ${configFile} "${cfg.dataDir}/configuration.yaml"
+      '';
+    };
+
+    users.users.zigbee2mqtt = {
+      home = cfg.dataDir;
+      createHome = true;
+      group = "zigbee2mqtt";
+      extraGroups = [ "dialout" ];
+      uid = config.ids.uids.zigbee2mqtt;
+    };
+
+    users.groups.zigbee2mqtt.gid = config.ids.gids.zigbee2mqtt;
+  };
+}