aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/nixos/modules/services/misc/zigbee2mqtt.nix
blob: cd987eb76c76ccfe99270081d59dfcd6b4a09d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{ 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" ];
      environment.ZIGBEE2MQTT_DATA = cfg.dataDir;
      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;
  };
}