1 { config, lib, pkgs, ... }:
7 cfg = config.services.thinkfan;
8 settingsFormat = pkgs.formats.yaml { };
9 configFile = settingsFormat.generate "thinkfan.yaml" cfg.settings;
10 thinkfan = pkgs.thinkfan.override { inherit (cfg) smartSupport; };
12 # fan-speed and temperature levels
13 levelType = with types;
15 tuple = ts: mkOptionType {
17 merge = mergeOneOption;
18 check = xs: all id (zipListsWith (t: x: t.check x) ts xs);
19 description = "tuple of" + concatMapStrings (t: " (${t.description})") ts;
21 level = ints.unsigned;
22 special = enum [ "level auto" "level full-speed" "level disengaged" ];
24 tuple [ (either level special) level level ];
26 # sensor or fan config
27 sensorType = name: types.submodule {
28 freeformType = types.attrsOf settingsFormat.type;
31 type = types.enum [ "hwmon" "atasmart" "tpacpi" "nvml" ];
32 description = lib.mdDoc ''
33 The ${name} type, can be
34 `hwmon` for standard ${name}s,
36 `atasmart` to read the temperature via
37 S.M.A.R.T (requires smartSupport to be enabled),
39 `tpacpi` for the legacy thinkpac_acpi driver, or
41 `nvml` for the (proprietary) nVidia driver.
46 description = lib.mdDoc ''
47 The query string used to match one or more ${name}s: can be
48 a fullpath to the temperature file (single ${name}) or a fullpath
49 to a driver directory (multiple ${name}s).
52 When multiple ${name}s match, the query can be restricted using the
53 {option}`name` or {option}`indices` options.
58 type = with types; nullOr (listOf ints.unsigned);
60 description = lib.mdDoc ''
61 A list of ${name}s to pick in case multiple ${name}s match the query.
68 } // optionalAttrs (name == "sensor") {
69 correction = mkOption {
70 type = with types; nullOr (listOf int);
72 description = lib.mdDoc ''
73 A list of values to be added to the temperature of each sensor,
74 can be used to equalize small discrepancies in temperature ratings.
80 # removes NixOS special and unused attributes
81 sensorToConf = { type, query, ... }@args:
82 (filterAttrs (k: v: v != null && !(elem k ["type" "query"])) args)
83 // { "${type}" = query; };
87 This section slightly departs from the thinkfan.conf syntax.
88 The type and path must be specified like this:
91 query = "/proc/acpi/ibm/${name}";
93 instead of a single declaration like:
95 - tpacpi: /proc/acpi/ibm/${name}
104 services.thinkfan = {
109 description = lib.mdDoc ''
110 Whether to enable thinkfan, a fan control program.
113 This module targets IBM/Lenovo thinkpads by default, for
114 other hardware you will have configure it more carefully.
117 relatedPackages = [ "thinkfan" ];
120 smartSupport = mkOption {
123 description = lib.mdDoc ''
124 Whether to build thinkfan with S.M.A.R.T. support to read temperatures
125 directly from hard disks.
130 type = types.listOf (sensorType "sensor");
133 query = "/proc/acpi/ibm/thermal";
136 description = lib.mdDoc ''
137 List of temperature sensors thinkfan will monitor.
139 ${syntaxNote "thermal"}
144 type = types.listOf (sensorType "fan");
147 query = "/proc/acpi/ibm/fan";
150 description = lib.mdDoc ''
151 List of fans thinkfan will control.
158 type = types.listOf levelType;
166 ["level auto" 80 32767]
168 description = lib.mdDoc ''
171 LEVEL is the fan level to use: it can be an integer (0-7 with thinkpad_acpi),
172 "level auto" (to keep the default firmware behavior), "level full-speed" or
173 "level disengaged" (to run the fan as fast as possible).
174 LOW is the temperature at which to step down to the previous level.
175 HIGH is the temperature at which to step up to the next level.
176 All numbers are integers.
180 extraArgs = mkOption {
181 type = types.listOf types.str;
183 example = [ "-b" "0" ];
184 description = lib.mdDoc ''
185 A list of extra command line arguments to pass to thinkfan.
186 Check the thinkfan(1) manpage for available arguments.
190 settings = mkOption {
191 type = types.attrsOf settingsFormat.type;
193 description = lib.mdDoc ''
194 Thinkfan settings. Use this option to configure thinkfan
195 settings not exposed in a NixOS option or to bypass one.
196 Before changing this, read the `thinkfan.conf(5)`
197 manpage and take a look at the example config file at
198 <https://github.com/vmatare/thinkfan/blob/master/examples/thinkfan.yaml>
206 config = mkIf cfg.enable {
208 environment.systemPackages = [ thinkfan ];
210 services.thinkfan.settings = mapAttrs (k: v: mkDefault v) {
211 sensors = map sensorToConf cfg.sensors;
212 fans = map sensorToConf cfg.fans;
216 systemd.packages = [ thinkfan ];
219 thinkfan.environment.THINKFAN_ARGS = escapeShellArgs ([ "-c" configFile ] ++ cfg.extraArgs);
221 # must be added manually, see issue #81138
222 thinkfan.wantedBy = [ "multi-user.target" ];
223 thinkfan-wakeup.wantedBy = [ "sleep.target" ];
224 thinkfan-sleep.wantedBy = [ "sleep.target" ];
227 boot.extraModprobeConfig = "options thinkpad_acpi experimental=1 fan_control=1";