Thinkfan NixOS

Info
Just gonna put here for future reference.

I have a Thinkpad P51 and T470 but noticed that it is always running hot even on a cold day or when it is at idle. The good things is there is a solution already by vmature/thinkfan. This is a lightweight fan control program that can be used to control the fan speed of your Thinkpad.

Table of Contents

Installation

Add thinkfan to packages.

1 environment.systemPackages = with pkgs; [
2   thinkfan
3 ];

Configuration

Verify the fan acpi module.

1~ cat /proc/acpi/ibm/fan 
2status:		enabled
3speed:		4291
4level:		5
5commands:	level <level> (<level> is 0-7, auto, disengaged, full-speed)
6commands:	enable, disable
7commands:	watchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))

Find sensors, for my setup I only monitor the CPU temperature. It is usually the coretemp sensor.

 1➜  ~ find /sys -name "temp*_input"
 2find: ‘/sys/kernel/tracing’: Permission denied
 3find: ‘/sys/kernel/debug’: Permission denied
 4/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp6_input
 5/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp3_input
 6/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp7_input
 7/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp4_input
 8/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp8_input
 9/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp1_input
10/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp5_input
11/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon6/temp2_input
12/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp3_input
13/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp4_input
14/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp1_input
15/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp5_input
16/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp2_input
17/sys/devices/pci0000:00/0000:00:1b.0/0000:02:00.0/nvme/nvme1/hwmon1/temp1_input
18/sys/devices/pci0000:00/0000:00:1d.0/0000:3e:00.0/nvme/nvme0/hwmon0/temp1_input
19/sys/devices/pci0000:00/0000:00:1d.0/0000:3e:00.0/nvme/nvme0/hwmon0/temp2_input
20/sys/devices/virtual/thermal/thermal_zone0/hwmon2/temp1_input
21/sys/devices/virtual/thermal/thermal_zone3/hwmon8/temp1_input
22/sys/devices/virtual/thermal/thermal_zone1/hwmon3/temp1_input

Identify the CPU sensor property, confirm the labels.

1~ for i in /sys/devices/platform/coretemp.0/hwmon/hwmon7/temp*_label; do
2  echo "$i: $(cat $i)"
3done
4/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp1_label: Package id 0
5/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp2_label: Core 0
6/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp3_label: Core 1
7/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp4_label: Core 2
8/sys/devices/platform/coretemp.0/hwmon/hwmon7/temp5_label: Core 3

The important one for thinkfan is usually:

1Package id 0

Service

Add service, test temperature increment and setting. I’ve use the default configuration, but you can change it to your liking.

 1{
 2  services.thinkfan = {
 3    enable = true;
 4
 5    sensors = [
 6      {
 7        name = "cpu";
 8        type = "hwmon";
 9        query = "/sys/class/hwmon/hwmon*/temp1_input";
10        hwmonName = "coretemp";
11      }
12    ];
13
14    fans = [
15      {
16        type = "tpacpi";
17        query = "/proc/acpi/ibm/fan";
18      }
19    ];
20
21    levels = [
22      [0 0 45]
23      [1 43 50]
24      [2 48 55]
25      [3 53 60]
26      [4 58 65]
27      [5 63 70]
28      [7 68 80]
29      [127 75 32767]
30    ];
31  };
32}

If you want to add more sensor to monitor use this template.

 1{
 2  services.thinkfan = {
 3    enable = true;
 4
 5    sensors = [
 6      {
 7        name = "cpu";
 8        type = "hwmon";
 9        query = "/sys/class/hwmon/hwmon*/temp1_input";
10      }
11
12      {
13        name = "nvme0";
14        type = "hwmon";
15        query = "/sys/devices/pci0000:00/0000:00:1b.0/0000:02:00.0/nvme/nvme0/hwmon1/temp1_input";
16      }
17    ];
18
19    fans = [
20      {
21        type = "tpacpi";
22        query = "/proc/acpi/ibm/fan";
23      }
24    ];
25
26    levels = [
27      [0 0 45]
28      [1 43 50]
29      [2 48 55]
30      [3 53 60]
31      [4 58 65]
32      [5 63 70]
33      [7 68 80]
34      [127 75 32767]
35    ];
36  };
37}

Boot Parameter

1  boot.kernelModules = [ "thinkpad_acpi" ];
2
3  boot.extraModprobeConfig = ''
4    options thinkpad_acpi fan_control=1
5  '';

Rebuild NixOS

Restart your laptop after rebuild.

1nixos-rebuild swithch

Monitoring and Testing

Verify if the service is running and if the fan setting is changing based on the temperature.

 1~ journalctl -u thinkfan -b
 2Mar 08 16:36:55 tags-p51 systemd[1]: Starting thinkfan 2.0.0...
 3Mar 08 16:36:55 tags-p51 systemd[1]: Started thinkfan 2.0.0.
 4Mar 08 16:36:55 tags-p51 thinkfan[1508]: Temperatures(bias): 76(0) -> Fans: level 7
 5Mar 08 16:37:00 tags-p51 thinkfan[1508]: Temperatures(bias): 82(0) -> Fans: level 127
 6Mar 08 16:37:02 tags-p51 thinkfan[1508]: Temperatures(bias): 56(0) -> Fans: level 3
 7Mar 08 16:37:07 tags-p51 thinkfan[1508]: Temperatures(bias): 72(0) -> Fans: level 6
 8Mar 08 16:37:09 tags-p51 thinkfan[1508]: Temperatures(bias): 64(0) -> Fans: level 5
 9Mar 08 16:37:14 tags-p51 thinkfan[1508]: Temperatures(bias): 56(0) -> Fans: level 3
10Mar 08 16:37:34 tags-p51 thinkfan[1508]: Temperatures(bias): 67(0) -> Fans: level 5
11Mar 08 16:37:36 tags-p51 thinkfan[1508]: Temperatures(bias): 56(0) -> Fans: level 3
12Mar 08 16:39:06 tags-p51 thinkfan[1508]: Temperatures(bias): 72(0) -> Fans: level 6