at 23.11-pre 6.7 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 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; }; 11 12 # fan-speed and temperature levels 13 levelType = with types; 14 let 15 tuple = ts: mkOptionType { 16 name = "tuple"; 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; 20 }; 21 level = ints.unsigned; 22 special = enum [ "level auto" "level full-speed" "level disengaged" ]; 23 in 24 tuple [ (either level special) level level ]; 25 26 # sensor or fan config 27 sensorType = name: types.submodule { 28 freeformType = types.attrsOf settingsFormat.type; 29 options = { 30 type = mkOption { 31 type = types.enum [ "hwmon" "atasmart" "tpacpi" "nvml" ]; 32 description = lib.mdDoc '' 33 The ${name} type, can be 34 `hwmon` for standard ${name}s, 35 36 `atasmart` to read the temperature via 37 S.M.A.R.T (requires smartSupport to be enabled), 38 39 `tpacpi` for the legacy thinkpac_acpi driver, or 40 41 `nvml` for the (proprietary) nVidia driver. 42 ''; 43 }; 44 query = mkOption { 45 type = types.str; 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). 50 51 ::: {.note} 52 When multiple ${name}s match, the query can be restricted using the 53 {option}`name` or {option}`indices` options. 54 ::: 55 ''; 56 }; 57 indices = mkOption { 58 type = with types; nullOr (listOf ints.unsigned); 59 default = null; 60 description = lib.mdDoc '' 61 A list of ${name}s to pick in case multiple ${name}s match the query. 62 63 ::: {.note} 64 Indices start from 0. 65 ::: 66 ''; 67 }; 68 } // optionalAttrs (name == "sensor") { 69 correction = mkOption { 70 type = with types; nullOr (listOf int); 71 default = null; 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. 75 ''; 76 }; 77 }; 78 }; 79 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; }; 84 85 syntaxNote = name: '' 86 ::: {.note} 87 This section slightly departs from the thinkfan.conf syntax. 88 The type and path must be specified like this: 89 ``` 90 type = "tpacpi"; 91 query = "/proc/acpi/ibm/${name}"; 92 ``` 93 instead of a single declaration like: 94 ``` 95 - tpacpi: /proc/acpi/ibm/${name} 96 ``` 97 ::: 98 ''; 99 100in { 101 102 options = { 103 104 services.thinkfan = { 105 106 enable = mkOption { 107 type = types.bool; 108 default = false; 109 description = lib.mdDoc '' 110 Whether to enable thinkfan, a fan control program. 111 112 ::: {.note} 113 This module targets IBM/Lenovo thinkpads by default, for 114 other hardware you will have configure it more carefully. 115 ::: 116 ''; 117 relatedPackages = [ "thinkfan" ]; 118 }; 119 120 smartSupport = mkOption { 121 type = types.bool; 122 default = false; 123 description = lib.mdDoc '' 124 Whether to build thinkfan with S.M.A.R.T. support to read temperatures 125 directly from hard disks. 126 ''; 127 }; 128 129 sensors = mkOption { 130 type = types.listOf (sensorType "sensor"); 131 default = [ 132 { type = "tpacpi"; 133 query = "/proc/acpi/ibm/thermal"; 134 } 135 ]; 136 description = lib.mdDoc '' 137 List of temperature sensors thinkfan will monitor. 138 139 ${syntaxNote "thermal"} 140 ''; 141 }; 142 143 fans = mkOption { 144 type = types.listOf (sensorType "fan"); 145 default = [ 146 { type = "tpacpi"; 147 query = "/proc/acpi/ibm/fan"; 148 } 149 ]; 150 description = lib.mdDoc '' 151 List of fans thinkfan will control. 152 153 ${syntaxNote "fan"} 154 ''; 155 }; 156 157 levels = mkOption { 158 type = types.listOf levelType; 159 default = [ 160 [0 0 55] 161 [1 48 60] 162 [2 50 61] 163 [3 52 63] 164 [6 56 65] 165 [7 60 85] 166 ["level auto" 80 32767] 167 ]; 168 description = lib.mdDoc '' 169 [LEVEL LOW HIGH] 170 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. 177 ''; 178 }; 179 180 extraArgs = mkOption { 181 type = types.listOf types.str; 182 default = [ ]; 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. 187 ''; 188 }; 189 190 settings = mkOption { 191 type = types.attrsOf settingsFormat.type; 192 default = { }; 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> 199 ''; 200 }; 201 202 }; 203 204 }; 205 206 config = mkIf cfg.enable { 207 208 environment.systemPackages = [ thinkfan ]; 209 210 services.thinkfan.settings = mapAttrs (k: v: mkDefault v) { 211 sensors = map sensorToConf cfg.sensors; 212 fans = map sensorToConf cfg.fans; 213 levels = cfg.levels; 214 }; 215 216 systemd.packages = [ thinkfan ]; 217 218 systemd.services = { 219 thinkfan.environment.THINKFAN_ARGS = escapeShellArgs ([ "-c" configFile ] ++ cfg.extraArgs); 220 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" ]; 225 }; 226 227 boot.extraModprobeConfig = "options thinkpad_acpi experimental=1 fan_control=1"; 228 229 }; 230}