summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/hardware/thinkfan.nix
blob: daf865252653e0c914cb1b1525f74d69d3f3e3fd (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{
  config,
  lib,
  pkgs,
  ...
}:
let

  cfg = config.services.thinkfan;
  settingsFormat = pkgs.formats.yaml { };
  configFile = settingsFormat.generate "thinkfan.yaml" cfg.settings;
  thinkfan = pkgs.thinkfan.override { inherit (cfg) smartSupport; };

  # fan-speed and temperature levels
  levelType =
    with lib.types;
    let
      tuple =
        ts:
        lib.mkOptionType {
          name = "tuple";
          merge = lib.mergeOneOption;
          check = xs: lib.all lib.id (lib.zipListsWith (t: x: t.check x) ts xs);
          description = "tuple of" + lib.concatMapStrings (t: " (${t.description})") ts;
        };
      level = ints.unsigned;
      special = enum [
        "level auto"
        "level full-speed"
        "level disengaged"
      ];
    in
    tuple [
      (either level special)
      level
      level
    ];

  # sensor or fan config
  sensorType =
    name:
    lib.types.submodule {
      freeformType = lib.types.attrsOf settingsFormat.type;
      options = {
        type = lib.mkOption {
          type = lib.types.enum [
            "hwmon"
            "atasmart"
            "tpacpi"
            "nvml"
          ];
          description = ''
            The ${name} type, can be
            `hwmon` for standard ${name}s,

            `atasmart` to read the temperature via
            S.M.A.R.T (requires smartSupport to be enabled),

            `tpacpi` for the legacy thinkpac_acpi driver, or

            `nvml` for the (proprietary) nVidia driver.
          '';
        };
        query = lib.mkOption {
          type = lib.types.str;
          description = ''
            The query string used to match one or more ${name}s: can be
            a fullpath to the temperature file (single ${name}) or a fullpath
            to a driver directory (multiple ${name}s).

            ::: {.note}
            When multiple ${name}s match, the query can be restricted using the
            {option}`name` or {option}`indices` options.
            :::
          '';
        };
        indices = lib.mkOption {
          type = with lib.types; nullOr (listOf ints.unsigned);
          default = null;
          description = ''
            A list of ${name}s to pick in case multiple ${name}s match the query.

            ::: {.note}
            Indices start from 0.
            :::
          '';
        };
      }
      // lib.optionalAttrs (name == "sensor") {
        correction = lib.mkOption {
          type = with lib.types; nullOr (listOf int);
          default = null;
          description = ''
            A list of values to be added to the temperature of each sensor,
            can be used to equalize small discrepancies in temperature ratings.
          '';
        };
      };
    };

  # removes NixOS special and unused attributes
  sensorToConf =
    { type, query, ... }@args:
    (lib.filterAttrs (k: v: v != null) (
      lib.removeAttrs args [
        "type"
        "query"
      ]
    ))
    // {
      "${type}" = query;
    };

  syntaxNote = name: ''
    ::: {.note}
    This section slightly departs from the thinkfan.conf syntax.
    The type and path must be specified like this:
    ```
      type = "tpacpi";
      query = "/proc/acpi/ibm/${name}";
    ```
    instead of a single declaration like:
    ```
      - tpacpi: /proc/acpi/ibm/${name}
    ```
    :::
  '';

in
{

  options = {

    services.thinkfan = {

      enable = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Whether to enable thinkfan, a fan control program.

          ::: {.note}
          This module targets IBM/Lenovo thinkpads by default, for
          other hardware you will have configure it more carefully.
          :::
        '';
        relatedPackages = [ "thinkfan" ];
      };

      smartSupport = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = ''
          Whether to build thinkfan with S.M.A.R.T. support to read temperatures
          directly from hard disks.
        '';
      };

      sensors = lib.mkOption {
        type = lib.types.listOf (sensorType "sensor");
        default = [
          {
            type = "tpacpi";
            query = "/proc/acpi/ibm/thermal";
          }
        ];
        description = ''
          List of temperature sensors thinkfan will monitor.

          ${syntaxNote "thermal"}
        '';
      };

      fans = lib.mkOption {
        type = lib.types.listOf (sensorType "fan");
        default = [
          {
            type = "tpacpi";
            query = "/proc/acpi/ibm/fan";
          }
        ];
        description = ''
          List of fans thinkfan will control.

          ${syntaxNote "fan"}
        '';
      };

      levels = lib.mkOption {
        type = lib.types.listOf levelType;
        default = [
          [
            0
            0
            55
          ]
          [
            1
            48
            60
          ]
          [
            2
            50
            61
          ]
          [
            3
            52
            63
          ]
          [
            6
            56
            65
          ]
          [
            7
            60
            85
          ]
          [
            "level auto"
            80
            32767
          ]
        ];
        description = ''
          [LEVEL LOW HIGH]

          LEVEL is the fan level to use: it can be an integer (0-7 with thinkpad_acpi),
          "level auto" (to keep the default firmware behavior), "level full-speed" or
          "level disengaged" (to run the fan as fast as possible).
          LOW is the temperature at which to step down to the previous level.
          HIGH is the temperature at which to step up to the next level.
          All numbers are integers.
        '';
      };

      extraArgs = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
        example = [
          "-b"
          "0"
        ];
        description = ''
          A list of extra command line arguments to pass to thinkfan.
          Check the {manpage}`thinkfan(1)` manpage for available arguments.
        '';
      };

      settings = lib.mkOption {
        type = lib.types.attrsOf settingsFormat.type;
        default = { };
        description = ''
          Thinkfan settings. Use this option to configure thinkfan
          settings not exposed in a NixOS option or to bypass one.
          Before changing this, read the {manpage}`thinkfan.conf(5)`
          manpage and take a look at the example config file at
          <https://github.com/vmatare/thinkfan/blob/master/examples/thinkfan.yaml>
        '';
      };

    };

  };

  config = lib.mkIf cfg.enable {

    environment.systemPackages = [ thinkfan ];

    services.thinkfan.settings = lib.mapAttrs (k: v: lib.mkDefault v) {
      sensors = map sensorToConf cfg.sensors;
      fans = map sensorToConf cfg.fans;
      levels = cfg.levels;
    };

    systemd.packages = [ thinkfan ];

    systemd.services = {
      thinkfan.environment.THINKFAN_ARGS = lib.escapeShellArgs (
        [
          "-c"
          configFile
        ]
        ++ cfg.extraArgs
      );
      thinkfan.serviceConfig = {
        Restart = "on-failure";
        RestartSec = "30s";

        # Hardening
        PrivateNetwork = true;
      };

      # must be added manually, see issue #81138
      thinkfan.wantedBy = [ "multi-user.target" ];
      thinkfan-wakeup.wantedBy = [ "sleep.target" ];
      thinkfan-sleep.wantedBy = [ "sleep.target" ];
    };

    boot.extraModprobeConfig = "options thinkpad_acpi experimental=1 fan_control=1";

  };
}