From 55b97f7ce62eb93e353b1d8af332f4c9323b5f8f Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 2 Feb 2021 02:47:36 +0100 Subject: [PATCH 1/5] thinkfan: cleanup - Remove the custom installPhase: upstream has added an install target and it does a better job than we do - Improve meta: description -> longDescription and add a short one - Fix license: GPL3 -> GPL3+ --- pkgs/tools/system/thinkfan/default.nix | 43 ++++++++++++-------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/pkgs/tools/system/thinkfan/default.nix b/pkgs/tools/system/thinkfan/default.nix index c9af119fb73..4f9e1e28998 100644 --- a/pkgs/tools/system/thinkfan/default.nix +++ b/pkgs/tools/system/thinkfan/default.nix @@ -1,5 +1,13 @@ -{ lib, stdenv, fetchFromGitHub, cmake, libyamlcpp, pkg-config -, smartSupport ? false, libatasmart }: +{ lib +, stdenv +, fetchFromGitHub +, cmake +, libyamlcpp +, pkg-config +, procps +, coreutils +, smartSupport ? false, libatasmart +}: stdenv.mkDerivation rec { pname = "thinkfan"; @@ -21,27 +29,16 @@ stdenv.mkDerivation rec { buildInputs = [ libyamlcpp ] ++ lib.optional smartSupport libatasmart; - installPhase = '' - runHook preInstall - - install -Dm755 {.,$out/bin}/thinkfan - - cd "$NIX_BUILD_TOP"; cd "$sourceRoot" # attempt to be a bit robust - install -Dm644 {.,$out/share/doc/thinkfan}/README.md - cp -R examples $out/share/doc/thinkfan - install -Dm644 {src,$out/share/man/man1}/thinkfan.1 - - runHook postInstall - ''; - - meta = with lib; { - description = "A minimalist fan control program"; - longDescription = "A minimalist fan control program. Originally designed -specifically for IBM/Lenovo Thinkpads, it now supports any kind of system via -the sysfs hwmon interface (/sys/class/hwmon)."; - license = licenses.gpl3; + meta = { + description = "A simple, lightweight fan control program"; + longDescription = '' + Thinkfan is a minimalist fan control program. Originally designed + specifically for IBM/Lenovo Thinkpads, it now supports any kind of + system via the sysfs hwmon interface (/sys/class/hwmon). + ''; + license = lib.licenses.gpl3Plus; homepage = "https://github.com/vmatare/thinkfan"; - maintainers = with maintainers; [ domenkozar ]; - platforms = platforms.linux; + maintainers = with lib.maintainers; [ domenkozar ]; + platforms = lib.platforms.linux; }; } From f5461e2654a4817aad199b747f488ab67e5ef04f Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Wed, 3 Feb 2021 11:37:45 +0100 Subject: [PATCH 2/5] thinkfan: install systemd unit files --- pkgs/tools/system/thinkfan/default.nix | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkgs/tools/system/thinkfan/default.nix b/pkgs/tools/system/thinkfan/default.nix index 4f9e1e28998..ea9deb8af66 100644 --- a/pkgs/tools/system/thinkfan/default.nix +++ b/pkgs/tools/system/thinkfan/default.nix @@ -20,9 +20,24 @@ stdenv.mkDerivation rec { sha256 = "18vgm5w5pjnpipa34j4x87q10695w2jnqwvc2f027afy7mnzw7kz"; }; + postPatch = '' + # fix hardcoded install path + substituteInPlace CMakeLists.txt --replace /etc $out/etc + + # fix command paths in unit files + for unit in rcscripts/systemd/*; do + substituteInPlace "$unit" \ + --replace /bin/kill ${procps}/bin/kill \ + --replace /usr/bin/pkill ${procps}/bin/pkill \ + --replace /usr/bin/sleep ${coreutils}/bin/sleep + done + ''; + cmakeFlags = [ "-DCMAKE_INSTALL_DOCDIR=share/doc/${pname}" "-DUSE_NVML=OFF" + # force install unit files + "-DSYSTEMD_FOUND=ON" ] ++ lib.optional smartSupport "-DUSE_ATASMART=ON"; nativeBuildInputs = [ cmake pkg-config ]; From 32e1eb37422b0aa3c19790e1d5be72948c0899c7 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Wed, 3 Feb 2021 11:29:34 +0100 Subject: [PATCH 3/5] thinkfan: add rnhmjoj as maintainer --- pkgs/tools/system/thinkfan/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/tools/system/thinkfan/default.nix b/pkgs/tools/system/thinkfan/default.nix index ea9deb8af66..97c61d2da0e 100644 --- a/pkgs/tools/system/thinkfan/default.nix +++ b/pkgs/tools/system/thinkfan/default.nix @@ -53,7 +53,7 @@ stdenv.mkDerivation rec { ''; license = lib.licenses.gpl3Plus; homepage = "https://github.com/vmatare/thinkfan"; - maintainers = with lib.maintainers; [ domenkozar ]; + maintainers = with lib.maintainers; [ domenkozar rnhmjoj ]; platforms = lib.platforms.linux; }; } From 02b872310d6a6503639f5a71a14f00441f961bc9 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 2 Feb 2021 02:13:15 +0100 Subject: [PATCH 4/5] nixos/thinkfan: rewrite for 1.2 update Thinkfan underwent some major changes and the config file is now based on YAML. This commit contains a number of changes: - rewrite the module to output the new format; - add a `settings` option, following RFC 0042[1]; - add fancy type-checking for the most critical options - use upstream systemd units (which fix the resume issue) [1]: https://github.com/NixOS/rfcs/blob/master/rfcs/0042-config-option.md --- nixos/modules/services/hardware/thinkfan.nix | 254 ++++++++++++------- 1 file changed, 163 insertions(+), 91 deletions(-) diff --git a/nixos/modules/services/hardware/thinkfan.nix b/nixos/modules/services/hardware/thinkfan.nix index 3bda61ed1a9..7a5a7e1c41c 100644 --- a/nixos/modules/services/hardware/thinkfan.nix +++ b/nixos/modules/services/hardware/thinkfan.nix @@ -5,50 +5,96 @@ with lib; let cfg = config.services.thinkfan; - configFile = pkgs.writeText "thinkfan.conf" '' - # ATTENTION: There is only very basic sanity checking on the configuration. - # That means you can set your temperature limits as insane as you like. You - # can do anything stupid, e.g. turn off your fan when your CPU reaches 70°C. - # - # That's why this program is called THINKfan: You gotta think for yourself. - # - ###################################################################### - # - # IBM/Lenovo Thinkpads (thinkpad_acpi, /proc/acpi/ibm) - # ==================================================== - # - # IMPORTANT: - # - # To keep your HD from overheating, you have to specify a correction value for - # the sensor that has the HD's temperature. You need to do this because - # thinkfan uses only the highest temperature it can find in the system, and - # that'll most likely never be your HD, as most HDs are already out of spec - # when they reach 55 °C. - # Correction values are applied from left to right in the same order as the - # temperatures are read from the file. - # - # For example: - # tp_thermal /proc/acpi/ibm/thermal (0, 0, 10) - # will add a fixed value of 10 °C the 3rd value read from that file. Check out - # http://www.thinkwiki.org/wiki/Thermal_Sensors to find out how much you may - # want to add to certain temperatures. + settingsFormat = pkgs.formats.yaml { }; + configFile = settingsFormat.generate "thinkfan.yaml" cfg.settings; + thinkfan = pkgs.thinkfan.override { inherit (cfg) smartSupport; }; - ${cfg.fan} - ${cfg.sensors} + # fan-speed and temperature levels + levelType = with types; + let + tuple = ts: mkOptionType { + name = "tuple"; + merge = mergeOneOption; + check = xs: all id (zipListsWith (t: x: t.check x) ts xs); + description = "tuple of" + concatMapStrings (t: " (${t.description})") ts; + }; + level = ints.unsigned; + special = enum [ "level auto" "level full-speed" "level disengage" ]; + in + tuple [ (either level special) level level ]; - # Syntax: - # (LEVEL, LOW, HIGH) - # LEVEL is the fan level to use (0-7 with thinkpad_acpi) - # 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. - # + # sensor or fan config + sensorType = name: types.submodule { + freeformType = types.attrsOf settingsFormat.type; + options = { + type = mkOption { + type = types.enum [ "hwmon" "atasmart" "tpacpi" "nvml" ]; + description = '' + The ${name} type, can be + hwmon for standard ${name}s, - ${cfg.levels} + 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 = mkOption { + type = 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). + + + When multiple ${name}s match, the query can be restricted using the + or options. + + ''; + }; + indices = mkOption { + type = with types; nullOr (listOf ints.unsigned); + default = null; + description = '' + A list of ${name}s to pick in case multiple ${name}s match the query. + + Indices start from 0. + ''; + }; + } // optionalAttrs (name == "sensor") { + correction = mkOption { + type = with 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: + (filterAttrs (k: v: v != null && !(elem k ["type" "query"])) args) + // { "${type}" = query; }; + + syntaxNote = name: '' + + 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} + + ''; - thinkfan = pkgs.thinkfan.override { smartSupport = cfg.smartSupport; }; - in { options = { @@ -59,76 +105,93 @@ in { type = types.bool; default = false; description = '' - Whether to enable thinkfan, fan controller for IBM/Lenovo ThinkPads. + Whether to enable thinkfan, a fan control program. + + + This module targets IBM/Lenovo thinkpads by default, for + other hardware you will have configure it more carefully. + ''; + relatedPackages = [ "thinkfan" ]; }; smartSupport = mkOption { type = types.bool; default = false; description = '' - Whether to build thinkfan with SMART support to read temperatures + Whether to build thinkfan with S.M.A.R.T. support to read temperatures directly from hard disks. ''; }; sensors = mkOption { - type = types.lines; - default = '' - tp_thermal /proc/acpi/ibm/thermal (0,0,10) - ''; - description ='' - thinkfan can read temperatures from three possible sources: - - /proc/acpi/ibm/thermal - Which is provided by the thinkpad_acpi kernel - module (keyword tp_thermal) - - /sys/class/hwmon/*/temp*_input - Which may be provided by any hwmon drivers (keyword - hwmon) - - S.M.A.R.T. (requires smartSupport to be enabled) - Which reads the temperature directly from the hard - disk using libatasmart (keyword atasmart) - - Multiple sensors may be added, in which case they will be - numbered in their order of appearance. - ''; + type = types.listOf (sensorType "sensor"); + default = [ + { type = "tpacpi"; + query = "/proc/acpi/ibm/thermal"; + } + ]; + description = '' + List of temperature sensors thinkfan will monitor. + '' + syntaxNote "thermal"; }; - fan = mkOption { - type = types.str; - default = "tp_fan /proc/acpi/ibm/fan"; - description ='' - Specifies the fan we want to use. - On anything other than a Thinkpad you'll probably - use some PWM control file in /sys/class/hwmon. - A sysfs fan would be specified like this: - pwm_fan /sys/class/hwmon/hwmon2/device/pwm1 - ''; + fans = mkOption { + type = types.listOf (sensorType "fan"); + default = [ + { type = "tpacpi"; + query = "/proc/acpi/ibm/fan"; + } + ]; + description = '' + List of fans thinkfan will control. + '' + syntaxNote "fan"; }; levels = mkOption { - type = types.lines; - default = '' - (0, 0, 55) - (1, 48, 60) - (2, 50, 61) - (3, 52, 63) - (6, 56, 65) - (7, 60, 85) - (127, 80, 32767) - ''; + type = 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 (0-7 with thinkpad_acpi). + [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 disengage" (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 = mkOption { + type = types.listOf types.str; + default = [ ]; + example = [ "-b" "0" ]; + description = '' + A list of extra command line arguments to pass to thinkfan. + Check the thinkfan(1) manpage for available arguments. + ''; + }; + + settings = mkOption { + type = 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 thinkfan.conf(5) + manpage and take a look at the example config file at + + ''; + }; }; @@ -138,12 +201,21 @@ in { environment.systemPackages = [ thinkfan ]; - systemd.services.thinkfan = { - description = "Thinkfan"; - after = [ "basic.target" ]; - wantedBy = [ "multi-user.target" ]; - path = [ thinkfan ]; - serviceConfig.ExecStart = "${thinkfan}/bin/thinkfan -n -c ${configFile}"; + services.thinkfan.settings = mapAttrs (k: v: mkDefault v) { + sensors = map sensorToConf cfg.sensors; + fans = map sensorToConf cfg.fans; + levels = cfg.levels; + }; + + systemd.packages = [ thinkfan ]; + + systemd.services = { + thinkfan.environment.THINKFAN_ARGS = escapeShellArgs ([ "-c" configFile ] ++ cfg.extraArgs); + + # 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"; From c753910d987b393acb14feab88daa0e38af3ba36 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 2 Feb 2021 03:15:51 +0100 Subject: [PATCH 5/5] nixos/release-notes: mention thinkfan 1.2 update --- nixos/doc/manual/release-notes/rl-2103.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/nixos/doc/manual/release-notes/rl-2103.xml b/nixos/doc/manual/release-notes/rl-2103.xml index 24a0281310c..9b2035d0440 100644 --- a/nixos/doc/manual/release-notes/rl-2103.xml +++ b/nixos/doc/manual/release-notes/rl-2103.xml @@ -466,6 +466,19 @@ self: super: ALSA OSS emulation (sound.enableOSSEmulation) is now disabled by default. + + + Thinkfan as been updated to 1.2.x, which comes with a + new YAML based configuration format. For this reason, several NixOS options + of the thinkfan module have been changed to non-backward compatible types. + In addition, a new option has + been added. + + + Please read the + thinkfan documentation before updating. + +