summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/hardware/hddfancontrol.nix
blob: e6f80950b91bd6263a3d4457742282a72105f6cb (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.hddfancontrol;
in

{
  meta.maintainers = with lib.maintainers; [ jadewilk ];

  imports = [
    (lib.mkRemovedOptionModule [
      "services"
      "hddfancontrol"
      "smartctl"
    ] "Smartctl is now automatically used when necessary, which makes this option redundant")
    (lib.mkRemovedOptionModule [
      "services"
      "hddfancontrol"
      "disks"
    ] "Disks should now be specified per hddfancontrol instance in its attrset")
    (lib.mkRemovedOptionModule [
      "services"
      "hddfancontrol"
      "pwmPaths"
    ] "Pwm Paths should now be specified per hddfancontrol instance in its attrset")
    (lib.mkRemovedOptionModule [
      "services"
      "hddfancontrol"
      "logVerbosity"
    ] "Log Verbosity should now be specified per hddfancontrol instance in its attrset")
    (lib.mkRemovedOptionModule [
      "services"
      "hddfancontrol"
      "extraArgs"
    ] "Extra Args should now be specified per hddfancontrol instance in its attrset")
  ];

  options = {
    services.hddfancontrol.enable = lib.mkEnableOption "hddfancontrol daemon";
    services.hddfancontrol.package = lib.mkPackageOption pkgs "hddfancontrol" { };

    services.hddfancontrol.settings = lib.mkOption {
      type = lib.types.attrsWith {
        placeholder = "drive-bay-name";
        elemType = (
          lib.types.submodule (
            { ... }:
            {
              options = {
                disks = lib.mkOption {
                  type = lib.types.listOf lib.types.str;
                  default = [ ];
                  description = ''
                    Drive(s) to get temperature from

                    Can also use command substitution to automatically grab all matching drives; such as all scsi (sas) drives
                  '';
                  example = [
                    "/dev/sda"
                    "`find /dev/disk/by-id -name \"scsi*\" -and -not -name \"*-part*\" -printf \"%p \"`"
                  ];
                };

                pwmPaths = lib.mkOption {
                  type = lib.types.listOf lib.types.str;
                  default = [ ];
                  description = ''
                    PWM filepath(s) to control fan speed (under /sys), followed by initial and fan-stop PWM values
                    Can also use command substitution to ensure the correct hwmonX is selected on every boot
                  '';
                  example = [
                    "/sys/class/hwmon/hwmon2/pwm1:30:10"
                    "`echo /sys/devices/platform/nct6775.656/hwmon/hwmon[[:print:]]`/pwm4:80:20"
                  ];
                };

                logVerbosity = lib.mkOption {
                  type = lib.types.enum [
                    "TRACE"
                    "DEBUG"
                    "INFO"
                    "WARN"
                    "ERROR"
                  ];
                  default = "INFO";
                  description = ''
                    Verbosity of the log level
                  '';
                };

                extraArgs = lib.mkOption {
                  type = lib.types.listOf lib.types.str;
                  default = [ ];
                  description = ''
                    Extra commandline arguments for hddfancontrol
                  '';
                  example = [
                    "--min-fan-speed-prct=10"
                    "--interval=1min"
                  ];
                };
              };
            }
          )
        );
      };
      default = { };
      description = ''
        Parameter-sets for each instance of hddfancontrol.
      '';
      example = lib.literalExpression ''
        {
          harddrives = {
            disks = [
              "/dev/sda"
              "/dev/sdb"
              "/dev/sdc"
            ];
            pwmPaths = [
              "/sys/class/hwmon/hwmon1/pwm1:25:10"
            ];
            logVerbosity = "DEBUG";
          };
          ssddrives = {
            disks = [
              "/dev/sdd"
              "/dev/sde"
              "/dev/sdf"
            ];
            pwmPaths = [
              "/sys/class/hwmon/hwmon1/pwm2:25:10"
            ];
            extraArgs = [
              "--interval=30s"
            ];
          };
        }
      '';
    };
  };

  config = lib.mkIf cfg.enable (
    let
      args =
        cnf:
        lib.concatLists [
          [ "-d" ]
          cnf.disks
          [ "-p" ]
          cnf.pwmPaths
          cnf.extraArgs
        ];

      createService = cnf: {
        description = "HDD fan control";
        documentation = [ "man:hddfancontrol(1)" ];
        after = [ "hddtemp.service" ];
        wants = [ "hddtemp.service" ];
        script =
          let
            argString = lib.strings.concatStringsSep " " (args cnf);
          in
          "${lib.getExe cfg.package} -v ${cnf.logVerbosity} daemon ${argString}";
        serviceConfig = {
          CPUSchedulingPolicy = "rr";
          CPUSchedulingPriority = 49;

          ProtectSystem = "strict";
          PrivateTmp = true;
          ProtectHome = true;
          SystemCallArchitectures = "native";
          MemoryDenyWriteExecute = true;
          NoNewPrivileges = true;
        };
        wantedBy = [ "multi-user.target" ];
      };

      services = lib.attrsets.mergeAttrsList [
        (lib.attrsets.mapAttrs' (
          name: cnf: lib.nameValuePair "hddfancontrol-${name}" (createService cnf)
        ) cfg.settings)
        {
          "hddfancontrol".enable = false;
        }
      ];
    in
    {
      systemd.packages = [ cfg.package ];

      hardware.sensor.hddtemp = {
        enable = true;
        drives = lib.lists.flatten (lib.attrsets.catAttrs "disks" (lib.attrsets.attrValues cfg.settings));
      };

      systemd.services = services;
    }
  );
}