summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/hardware/pdudaemon.nix
blob: 3f400848436e11edb5eb6a4a45052934fee31db4 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.services.pdudaemon;
  configFile = pkgs.writeText "pdudaemon.conf" (
    lib.generators.toJSON { } {
      daemon = {
        hostname = cfg.bindAddress;
        port = cfg.port;
        logging_level = cfg.logLevel;
        listener = cfg.listener;
      };
      pdus = cfg.pdus;
    }
  );
in
{
  meta = {
    maintainers = with lib.maintainers; [
      aiyion
      emantor
    ];
  };

  options = {
    services.pdudaemon = {
      enable = lib.mkEnableOption "PDUDaemon";

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

      bindAddress = lib.mkOption {
        default = "0.0.0.0";
        type = lib.types.str;
        description = "Bind address for the PDUDaemon.";
      };

      port = lib.mkOption {
        default = 16421;
        type = lib.types.port;
        description = "Port to bind to.";
      };

      openFirewall = lib.mkOption {
        default = false;
        type = lib.types.bool;
        description = ''
          Whether to automatically open the PDUDaemon listen port in the firewall.
        '';
      };

      listener = lib.mkOption {
        default = "http";
        type = lib.types.enum [
          "http"
          "tcp"
        ];
        description = "Which kind of listener to provide.";
      };

      logLevel = lib.mkOption {
        default = "error";
        type = lib.types.enum [
          "debug"
          "info"
          "warning"
          "error"
        ];
        description = "PDUDaemon log level.";
      };

      pdus = lib.mkOption {
        type = with lib.types; attrsOf anything;
        default = { };
        description = ''
          Structural pdus section of PDUDaemon's pdudaemon.conf.
          Refer to <https://github.com/pdudaemon/pdudaemon/blob/main/share/pdudaemon.conf>
          for more examples.
        '';
        example = lib.literalExpression ''
          {
            cbs350-poe-switch = {
              driver = "snmpv1";
              community = "private";
              oid = ".1.3.6.1.2.1.105.1.1.1.3.1.*;
              onsetting = 1;
              offsetting = 2;
            };
            energenie = {
              driver = "EG-PMS";
              device = "aa:bb:cc:xx:yy";
            };
            local = {
              driver = "localcmdline";
            };
          };
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ];

    systemd.services.pdudaemon = {
      after = [ "network-online.target" ];
      description = "Control and Queueing daemon for PDUs";
      serviceConfig = {
        ExecStart = "${lib.getExe cfg.package} --conf ${configFile}";
        Type = "simple";
        DynamicUser = "yes";
        StateDirectory = "pdudaemon";
        ProtectHome = true;
        Restart = "on-failure";
        CapabilityBoundingSet = "";
        PrivateDevices = true;
        ProtectClock = true;
        ProtectKernelLogs = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        SystemCallArchitectures = "native";
        MemoryDenyWriteExecute = true;
        RestrictNamespaces = true;
        ProtectHostname = true;
        LockPersonality = true;
        ProtectKernelTunables = true;
        RestrictRealtime = true;
        ProtectProc = "invisible";
        ProcSubset = "pid";
        PrivateUsers = true;
        SystemCallFilter = [
          "@system-service"
          "~@privileged"
          "~@resources"
        ];
      };

      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
    };
  };
}