about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/home-automation/govee2mqtt.nix
blob: 1dee5999fa3befff2dcf8a07fa1c671f59010438 (plain) (blame)
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
{ config, lib, pkgs, ... }:

let
  cfg = config.services.govee2mqtt;
in {
  meta.maintainers = with lib.maintainers; [ SuperSandro2000 ];

  options.services.govee2mqtt = {
    enable = lib.mkEnableOption "Govee2MQTT";

    package = lib.mkPackageOption pkgs "govee2mqtt" { };

    user = lib.mkOption {
      type = lib.types.str;
      default = "govee2mqtt";
      description = "User under which Govee2MQTT should run.";
    };

    group = lib.mkOption {
      type = lib.types.str;
      default = "govee2mqtt";
      description = "Group under which Govee2MQTT should run.";
    };

    environmentFile = lib.mkOption {
      type = lib.types.path;
      example = "/var/lib/govee2mqtt/govee2mqtt.env";
      description = ''
        Environment file as defined in {manpage}`systemd.exec(5)`.

        See upstream documentation <https://github.com/wez/govee2mqtt/blob/main/docs/CONFIG.md>.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    users = {
      groups.${cfg.group} = { };
      users.${cfg.user} = {
        description = "Govee2MQTT service user";
        inherit (cfg) group;
        isSystemUser = true;
      };
    };

    systemd.services.govee2mqtt = {
      description = "Govee2MQTT Service";
      wantedBy = [ "multi-user.target" ];
      after = [ "networking.target" ];
      serviceConfig = {
        CacheDirectory = "govee2mqtt";
        Environment = [
          "GOVEE_CACHE_DIR=/var/cache/govee2mqtt"
        ];
        EnvironmentFile = cfg.environmentFile;
        ExecStart = "${lib.getExe cfg.package} serve --govee-iot-key=/var/lib/govee2mqtt/iot.key --govee-iot-cert=/var/lib/govee2mqtt/iot.cert"
          + " --amazon-root-ca=${pkgs.cacert.unbundled}/etc/ssl/certs/Amazon_Root_CA_1:66c9fcf99bf8c0a39e2f0788a43e696365bca.crt";
        Group = cfg.group;
        Restart = "on-failure";
        StateDirectory = "govee2mqtt";
        User = cfg.user;

        # Hardening
        AmbientCapabilities = "";
        CapabilityBoundingSet = "";
        LockPersonality = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateMounts = true;
        PrivateTmp = true;
        PrivateUsers = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = "strict";
        RemoveIPC = true;
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
      };
    };
  };
}