1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.services.thinkfan;
8 configFile = pkgs.writeText "thinkfan.conf" ''
9 # ATTENTION: There is only very basic sanity checking on the configuration.
10 # That means you can set your temperature limits as insane as you like. You
11 # can do anything stupid, e.g. turn off your fan when your CPU reaches 70°C.
12 #
13 # That's why this program is called THINKfan: You gotta think for yourself.
14 #
15 ######################################################################
16 #
17 # IBM/Lenovo Thinkpads (thinkpad_acpi, /proc/acpi/ibm)
18 # ====================================================
19 #
20 # IMPORTANT:
21 #
22 # To keep your HD from overheating, you have to specify a correction value for
23 # the sensor that has the HD's temperature. You need to do this because
24 # thinkfan uses only the highest temperature it can find in the system, and
25 # that'll most likely never be your HD, as most HDs are already out of spec
26 # when they reach 55 °C.
27 # Correction values are applied from left to right in the same order as the
28 # temperatures are read from the file.
29 #
30 # For example:
31 # tp_thermal /proc/acpi/ibm/thermal (0, 0, 10)
32 # will add a fixed value of 10 °C the 3rd value read from that file. Check out
33 # http://www.thinkwiki.org/wiki/Thermal_Sensors to find out how much you may
34 # want to add to certain temperatures.
35
36 ${cfg.fan}
37 ${cfg.sensors}
38
39 # Syntax:
40 # (LEVEL, LOW, HIGH)
41 # LEVEL is the fan level to use (0-7 with thinkpad_acpi)
42 # LOW is the temperature at which to step down to the previous level
43 # HIGH is the temperature at which to step up to the next level
44 # All numbers are integers.
45 #
46
47 ${cfg.levels}
48 '';
49
50in {
51
52 options = {
53
54 services.thinkfan = {
55
56 enable = mkOption {
57 type = types.bool;
58 default = false;
59 description = ''
60 Whether to enable thinkfan, fan controller for IBM/Lenovo ThinkPads.
61 '';
62 };
63
64 sensors = mkOption {
65 type = types.lines;
66 default = ''
67 tp_thermal /proc/acpi/ibm/thermal (0,0,10)
68 '';
69 description =''
70 thinkfan can read temperatures from three possible sources:
71
72 /proc/acpi/ibm/thermal
73 Which is provided by the thinkpad_acpi kernel
74 module (keyword tp_thermal)
75
76 /sys/class/hwmon/*/temp*_input
77 Which may be provided by any hwmon drivers (keyword
78 hwmon)
79
80 S.M.A.R.T. (since 0.9 and requires the USE_ATASMART compilation flag)
81 Which reads the temperature directly from the hard
82 disk using libatasmart (keyword atasmart)
83
84 Multiple sensors may be added, in which case they will be
85 numbered in their order of appearance.
86 '';
87 };
88
89 fan = mkOption {
90 type = types.str;
91 default = "tp_fan /proc/acpi/ibm/fan";
92 description =''
93 Specifies the fan we want to use.
94 On anything other than a Thinkpad you'll probably
95 use some PWM control file in /sys/class/hwmon.
96 A sysfs fan would be specified like this:
97 pwm_fan /sys/class/hwmon/hwmon2/device/pwm1
98 '';
99 };
100
101 levels = mkOption {
102 type = types.lines;
103 default = ''
104 (0, 0, 55)
105 (1, 48, 60)
106 (2, 50, 61)
107 (3, 52, 63)
108 (6, 56, 65)
109 (7, 60, 85)
110 (127, 80, 32767)
111 '';
112 description = ''
113 (LEVEL, LOW, HIGH)
114 LEVEL is the fan level to use (0-7 with thinkpad_acpi).
115 LOW is the temperature at which to step down to the previous level.
116 HIGH is the temperature at which to step up to the next level.
117 All numbers are integers.
118 '';
119 };
120
121
122 };
123
124 };
125
126 config = mkIf cfg.enable {
127
128 environment.systemPackages = [ pkgs.thinkfan ];
129
130 systemd.services.thinkfan = {
131 description = "Thinkfan";
132 after = [ "basic.target" ];
133 wantedBy = [ "multi-user.target" ];
134 path = [ pkgs.thinkfan ];
135 serviceConfig.ExecStart = "${pkgs.thinkfan}/bin/thinkfan -n -c ${configFile}";
136 };
137
138 boot.extraModprobeConfig = "options thinkpad_acpi experimental=1 fan_control=1";
139
140 };
141
142}