Merge pull request #81087 from lovesegfault/tlp-1.3.1

tlp: 1.2.2 -> 1.3.1

authored by

worldofpeace and committed by
GitHub
76f4f6b9 29b495d4

+301 -141
+58 -84
nixos/modules/services/hardware/tlp.nix
··· 1 1 { config, lib, pkgs, ... }: 2 - 3 2 with lib; 4 - 5 3 let 6 - 7 - cfg = config.services.tlp; 8 - 9 - enableRDW = config.networking.networkmanager.enable; 10 - 11 - tlp = pkgs.tlp.override { 12 - inherit enableRDW; 13 - }; 14 - 15 - # XXX: We can't use writeTextFile + readFile here because it triggers 16 - # TLP build to get the .drv (even on --dry-run). 17 - confFile = pkgs.runCommand "tlp" 18 - { config = cfg.extraConfig; 19 - passAsFile = [ "config" ]; 20 - preferLocalBuild = true; 21 - } 22 - '' 23 - cat ${tlp}/etc/default/tlp > $out 24 - cat $configPath >> $out 25 - ''; 26 - 4 + cfg = config.services.tlp; 5 + enableRDW = config.networking.networkmanager.enable; 6 + tlp = pkgs.tlp.override { inherit enableRDW; }; 7 + # TODO: Use this for having proper parameters in the future 8 + mkTlpConfig = tlpConfig: generators.toKeyValue { 9 + mkKeyValue = generators.mkKeyValueDefault { 10 + mkValueString = val: 11 + if isInt val then toString val 12 + else if isString val then val 13 + else if true == val then "1" 14 + else if false == val then "0" 15 + else if isList val then "\"" + (concatStringsSep " " val) + "\"" 16 + else err "invalid value provided to mkTlpConfig:" (toString val); 17 + } "="; 18 + } tlpConfig; 27 19 in 28 - 29 20 { 30 - 31 21 ###### interface 32 - 33 22 options = { 34 - 35 23 services.tlp = { 36 - 37 24 enable = mkOption { 38 25 type = types.bool; 39 26 default = false; ··· 45 32 default = ""; 46 33 description = "Additional configuration variables for TLP"; 47 34 }; 48 - 49 35 }; 50 - 51 36 }; 52 37 53 - 54 38 ###### implementation 55 - 56 39 config = mkIf cfg.enable { 40 + boot.kernelModules = [ "msr" ]; 57 41 58 - powerManagement.scsiLinkPolicy = null; 59 - powerManagement.cpuFreqGovernor = null; 60 - powerManagement.cpufreq.max = null; 61 - powerManagement.cpufreq.min = null; 42 + environment.etc = { 43 + "tlp.conf".text = cfg.extraConfig; 44 + } // optionalAttrs enableRDW { 45 + "NetworkManager/dispatcher.d/99tlp-rdw-nm".source = 46 + "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm"; 47 + }; 62 48 63 - systemd.sockets.systemd-rfkill.enable = false; 49 + environment.systemPackages = [ tlp ]; 64 50 65 - systemd.services = { 66 - "systemd-rfkill@".enable = false; 67 - systemd-rfkill.enable = false; 51 + # FIXME: When the config is parametrized we need to move these into a 52 + # conditional on the relevant options being enabled. 53 + powerManagement = { 54 + scsiLinkPolicy = null; 55 + cpuFreqGovernor = null; 56 + cpufreq.max = null; 57 + cpufreq.min = null; 58 + }; 68 59 69 - tlp = { 70 - description = "TLP system startup/shutdown"; 60 + services.udev.packages = [ tlp ]; 71 61 72 - after = [ "multi-user.target" ]; 73 - wantedBy = [ "multi-user.target" ]; 74 - before = [ "shutdown.target" ]; 75 - restartTriggers = [ confFile ]; 62 + systemd = { 63 + packages = [ tlp ]; 64 + # XXX: These must always be disabled/masked according to [1]. 65 + # 66 + # [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319 67 + sockets.systemd-rfkill.enable = false; 68 + services.systemd-rfkill.enable = false; 76 69 77 - serviceConfig = { 78 - Type = "oneshot"; 79 - RemainAfterExit = true; 80 - ExecStart = "${tlp}/bin/tlp init start"; 81 - ExecStop = "${tlp}/bin/tlp init stop"; 82 - }; 70 + services.tlp = { 71 + # XXX: The service should reload whenever the configuration changes, 72 + # otherwise newly set power options remain inactive until reboot (or 73 + # manual unit restart.) 74 + restartTriggers = [ config.environment.etc."tlp.conf".source ]; 75 + # XXX: When using systemd.packages (which we do above) the [Install] 76 + # section of systemd units does not work (citation needed) so we manually 77 + # enforce it here. 78 + wantedBy = [ "multi-user.target" ]; 83 79 }; 84 80 85 - tlp-sleep = { 86 - description = "TLP suspend/resume"; 87 - 81 + services.tlp-sleep = { 82 + # XXX: When using systemd.packages (which we do above) the [Install] 83 + # section of systemd units does not work (citation needed) so we manually 84 + # enforce it here. 85 + before = [ "sleep.target" ]; 88 86 wantedBy = [ "sleep.target" ]; 89 - before = [ "sleep.target" ]; 90 - 91 - unitConfig = { 92 - StopWhenUnneeded = true; 93 - }; 94 - 95 - serviceConfig = { 96 - Type = "oneshot"; 97 - RemainAfterExit = true; 98 - ExecStart = "${tlp}/bin/tlp suspend"; 99 - ExecStop = "${tlp}/bin/tlp resume"; 100 - }; 87 + # XXX: `tlp suspend` requires /var/lib/tlp to exist in order to save 88 + # some stuff in there. There is no way, that I know of, to do this in 89 + # the package itself, so we do it here instead making sure the unit 90 + # won't fail due to the save dir not existing. 91 + serviceConfig.StateDirectory = "tlp"; 101 92 }; 102 93 }; 103 - 104 - services.udev.packages = [ tlp ]; 105 - 106 - environment.etc = 107 - { 108 - "default/tlp".source = confFile; 109 - } // optionalAttrs enableRDW { 110 - "NetworkManager/dispatcher.d/99tlp-rdw-nm" = { 111 - source = "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm"; 112 - }; 113 - }; 114 - 115 - environment.systemPackages = [ tlp ]; 116 - 117 - boot.kernelModules = [ "msr" ]; 118 - 119 94 }; 120 - 121 95 }
+102 -57
pkgs/tools/misc/tlp/default.nix
··· 1 - { stdenv, lib, fetchFromGitHub, perl, makeWrapper, file, systemd, iw, rfkill 2 - , hdparm, ethtool, inetutils , kmod, pciutils, smartmontools 3 - , x86_energy_perf_policy, gawk, gnugrep, coreutils, utillinux 4 - , checkbashisms, shellcheck 5 - , enableRDW ? false, networkmanager 6 - }: 7 - 8 - let 9 - paths = lib.makeBinPath 10 - ([ iw rfkill hdparm ethtool inetutils systemd kmod pciutils smartmontools 11 - x86_energy_perf_policy gawk gnugrep coreutils utillinux 12 - ] 13 - ++ lib.optional enableRDW networkmanager 14 - ); 15 - 16 - in stdenv.mkDerivation rec { 1 + { stdenv 2 + , lib 3 + , checkbashisms 4 + , coreutils 5 + , ethtool 6 + , fetchFromGitHub 7 + , gawk 8 + , gnugrep 9 + , gnused 10 + , hdparm 11 + , iw 12 + , kmod 13 + , makeWrapper 14 + , pciutils 15 + , perl 16 + , shellcheck 17 + , smartmontools 18 + , systemd 19 + , utillinux 20 + , x86_energy_perf_policy 21 + # RDW only works with NetworkManager, and thus is optional with default off 22 + , enableRDW ? false 23 + , networkmanager 24 + }: stdenv.mkDerivation rec { 17 25 pname = "tlp"; 18 - version = "1.2.2"; 26 + version = "1.3.1"; 19 27 20 28 src = fetchFromGitHub { 21 29 owner = "linrunner"; 22 30 repo = "TLP"; 23 31 rev = version; 24 - sha256 = "0vm31ca6kdak9xzwskz7a8hvdp67drfh2zcdwlz3260r8r2ypgg1"; 32 + sha256 = "14fcnaz9pw534v4d8dddqq4wcvpf1kghr8zlrk62r5lrl46sp1p5"; 25 33 }; 26 34 27 - outRef = placeholder "out"; 35 + # XXX: See patch files for relevant explanations. 36 + patches = [ ./patches/fix-makefile-sed.patch ./patches/tlp-sleep-service.patch ]; 37 + 38 + buildInputs = [ perl ]; 39 + nativeBuildInputs = [ makeWrapper gnused ]; 28 40 41 + # XXX: While [1] states that DESTDIR should not be used, and that the correct 42 + # variable to set is, in fact, PREFIX, tlp thinks otherwise. The Makefile for 43 + # tlp concerns itself only with DESTDIR [2] (possibly incorrectly) and so we set 44 + # that as opposed to PREFIX, despite what [1] says. 45 + # 46 + # [1]: https://github.com/NixOS/nixpkgs/issues/65718 47 + # [2]: https://github.com/linrunner/TLP/blob/ab788abf4936dfb44fbb408afc34af834230a64d/Makefile#L4-L46 29 48 makeFlags = [ 30 - "DESTDIR=${outRef}" 31 - "TLP_SBIN=${outRef}/bin" 32 - "TLP_BIN=${outRef}/bin" 33 - "TLP_TLIB=${outRef}/share/tlp" 34 - "TLP_FLIB=${outRef}/share/tlp/func.d" 35 - "TLP_ULIB=${outRef}/lib/udev" 36 - "TLP_NMDSP=${outRef}/etc/NetworkManager/dispatcher.d" 37 - "TLP_SHCPL=${outRef}/share/bash-completion/completions" 38 - "TLP_MAN=${outRef}/share/man" 39 - "TLP_META=${outRef}/share/metainfo" 49 + "DESTDIR=${placeholder "out"}" 40 50 41 51 "TLP_NO_INIT=1" 52 + "TLP_WITH_ELOGIND=0" 53 + "TLP_WITH_SYSTEMD=1" 54 + 55 + "TLP_BIN=/bin" 56 + "TLP_CONFDEF=/share/tlp/defaults.conf" 57 + "TLP_FLIB=/share/tlp/func.d" 58 + "TLP_MAN=/share/man" 59 + "TLP_META=/share/metainfo" 60 + "TLP_SBIN=/sbin" 61 + "TLP_SHCPL=/share/bash-completion/completions" 62 + "TLP_TLIB=/share/tlp" 42 63 ]; 43 64 44 - nativeBuildInputs = [ makeWrapper file ]; 45 - 46 - buildInputs = [ perl ]; 47 - 48 - installTargets = [ "install-tlp" "install-man" ] ++ stdenv.lib.optional enableRDW "install-rdw"; 49 - 50 - checkInputs = [ 51 - checkbashisms 52 - shellcheck 53 - ]; 65 + installTargets = [ "install-tlp" "install-man" ] 66 + ++ lib.optionals enableRDW [ "install-rdw" "install-man-rdw" ]; 54 67 55 - doCheck = true; 68 + # XXX: This is disabled because it's basically just noise since upstream 69 + # itself does not seem to care about the zillion shellcheck errors. 70 + doCheck = false; 71 + checkInputs = [ checkbashisms shellcheck ]; 56 72 checkTarget = [ "checkall" ]; 57 73 58 - postInstall = '' 59 - cp -r $out/$out/* $out 60 - rm -rf $out/$(echo "$NIX_STORE" | cut -d "/" -f2) 74 + postInstall = let 75 + paths = lib.makeBinPath ( 76 + [ 77 + coreutils 78 + ethtool 79 + gawk 80 + gnugrep 81 + gnused 82 + hdparm 83 + iw 84 + kmod 85 + pciutils 86 + perl 87 + smartmontools 88 + systemd 89 + utillinux 90 + x86_energy_perf_policy 91 + ] ++ lib.optional enableRDW networkmanager 92 + ); 93 + in 94 + '' 95 + fixup_perl=( 96 + $out/share/tlp/tlp-pcilist 97 + $out/share/tlp/tlp-readconfs 98 + $out/share/tlp/tlp-usblist 99 + $out/share/tlp/tpacpi-bat 100 + ) 101 + for f in "''${fixup_perl[@]}"; do 102 + wrapProgram "$f" --prefix PATH : "${paths}" 103 + done 61 104 62 - for i in $out/bin/* $out/lib/udev/tlp-* ${lib.optionalString enableRDW "$out/etc/NetworkManager/dispatcher.d/*"}; do 63 - if file "$i" | grep -q Perl; then 64 - # Perl script; use wrapProgram 65 - wrapProgram "$i" \ 66 - --prefix PATH : "${paths}" 67 - else 68 - # Bash script 69 - sed -i '2iexport PATH=${paths}:$PATH' "$i" 70 - fi 71 - done 72 - ''; 105 + fixup_bash=( 106 + $out/bin/* 107 + $out/etc/NetworkManager/dispatcher.d/* 108 + $out/lib/udev/tlp-* 109 + $out/sbin/* 110 + $out/share/tlp/func.d/* 111 + $out/share/tlp/tlp-func-base 112 + ) 113 + for f in "''${fixup_bash[@]}"; do 114 + sed -i '2iexport PATH=${paths}:$PATH' "$f" 115 + done 116 + ''; 73 117 74 - meta = with stdenv.lib; { 118 + meta = with lib; { 75 119 description = "Advanced Power Management for Linux"; 76 - homepage = https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-management.html; 120 + homepage = 121 + "https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-management.html"; 77 122 platforms = platforms.linux; 78 - maintainers = with maintainers; [ abbradar ]; 123 + maintainers = with maintainers; [ abbradar lovesegfault ]; 79 124 license = licenses.gpl2Plus; 80 125 }; 81 126 }
+46
pkgs/tools/misc/tlp/patches/fix-makefile-sed.patch
··· 1 + commit c44347b3b813e209fff537b4d46d23430727a5e2 2 + Author: Bernardo Meurer <meurerbernardo@gmail.com> 3 + Date: Tue Feb 25 21:27:39 2020 -0800 4 + 5 + makefile: correctly sed paths 6 + 7 + The default Makefile for tlp makes a mess with catenating `DESTDIR` to 8 + everything, but then not actualy using the catenated (_ prefixed) 9 + variables to sed it's `.in` files. 10 + 11 + This patch makes sure that it correctly sets the paths, taking `DESTDIR` 12 + in account where it makes sense (e.g. /bin where we want $out/bin) but 13 + not where it doesn't (/etc/tlp.conf should be just that). 14 + 15 + The reason DESTDIR is used at all, as opposed to the more appropriate 16 + PREFIX, is covered in the nix formula, and is (also) due to the Makefile 17 + being a bit "different." 18 + 19 + diff --git a/Makefile b/Makefile 20 + index b5af74e..95122df 100644 21 + --- a/Makefile 22 + +++ b/Makefile 23 + @@ -47,17 +47,17 @@ _TPACPIBAT = $(DESTDIR)$(TPACPIBAT) 24 + 25 + SED = sed \ 26 + -e "s|@TLPVER@|$(TLPVER)|g" \ 27 + - -e "s|@TLP_SBIN@|$(TLP_SBIN)|g" \ 28 + - -e "s|@TLP_TLIB@|$(TLP_TLIB)|g" \ 29 + - -e "s|@TLP_FLIB@|$(TLP_FLIB)|g" \ 30 + - -e "s|@TLP_ULIB@|$(TLP_ULIB)|g" \ 31 + + -e "s|@TLP_SBIN@|$(_SBIN)|g" \ 32 + + -e "s|@TLP_TLIB@|$(_TLIB)|g" \ 33 + + -e "s|@TLP_FLIB@|$(_FLIB)|g" \ 34 + + -e "s|@TLP_ULIB@|$(_ULIB)|g" \ 35 + -e "s|@TLP_CONFUSR@|$(TLP_CONFUSR)|g" \ 36 + -e "s|@TLP_CONFDIR@|$(TLP_CONFDIR)|g" \ 37 + - -e "s|@TLP_CONFDEF@|$(TLP_CONFDEF)|g" \ 38 + + -e "s|@TLP_CONFDEF@|$(_CONFDEF)|g" \ 39 + -e "s|@TLP_CONF@|$(TLP_CONF)|g" \ 40 + -e "s|@TLP_RUN@|$(TLP_RUN)|g" \ 41 + -e "s|@TLP_VAR@|$(TLP_VAR)|g" \ 42 + - -e "s|@TPACPIBAT@|$(TPACPIBAT)|g" 43 + + -e "s|@TPACPIBAT@|$(_TPACPIBAT)|g" 44 + 45 + INFILES = \ 46 + tlp \
+95
pkgs/tools/misc/tlp/patches/tlp-sleep-service.patch
··· 1 + commit ca94cd56210067e2a55c1f413bd7713f7d338f9f 2 + Author: Bernardo Meurer <meurerbernardo@gmail.com> 3 + Date: Wed Feb 26 10:46:23 2020 -0800 4 + 5 + tlp-sleep.service: reintroduce 6 + 7 + This patch reintroduces tlp-sleep as a systemd unit as opposed to a 8 + systemd system-sleep hook script. This is due to the recommendation by 9 + systemd itself to not use the hook scripts. As per the manual: 10 + 11 + > Note that scripts or binaries dropped in /usr/lib/systemd/system-sleep/ 12 + > are intended for local use only and should be considered hacks. If 13 + > applications want to react to system suspend/hibernation and resume, 14 + > they should rather use the Inhibitor interface[1]. 15 + 16 + diff --git a/Makefile b/Makefile 17 + index 95122df..0e9230a 100644 18 + --- a/Makefile 19 + +++ b/Makefile 20 + @@ -70,6 +70,7 @@ INFILES = \ 21 + tlp.rules \ 22 + tlp-readconfs \ 23 + tlp-run-on \ 24 + + tlp-sleep.service \ 25 + tlp.service \ 26 + tlp-stat \ 27 + tlp.upstart \ 28 + @@ -99,7 +100,6 @@ SHFILES = \ 29 + tlp-rdw-udev.in \ 30 + tlp-rf.in \ 31 + tlp-run-on.in \ 32 + - tlp-sleep \ 33 + tlp-sleep.elogind \ 34 + tlp-stat.in \ 35 + tlp-usb-udev.in 36 + @@ -147,7 +147,7 @@ ifneq ($(TLP_NO_INIT),1) 37 + endif 38 + ifneq ($(TLP_WITH_SYSTEMD),0) 39 + install -D -m 644 tlp.service $(_SYSD)/tlp.service 40 + - install -D -m 755 tlp-sleep $(_SDSL)/tlp 41 + + install -D -m 644 tlp-sleep.service $(_SYSD)/tlp-sleep.service 42 + endif 43 + ifneq ($(TLP_WITH_ELOGIND),0) 44 + install -D -m 755 tlp-sleep.elogind $(_ELOD)/49-tlp-sleep 45 + @@ -204,7 +204,7 @@ uninstall-tlp: 46 + rm $(_ULIB)/rules.d/85-tlp.rules 47 + rm -f $(_SYSV)/tlp 48 + rm -f $(_SYSD)/tlp.service 49 + - rm -f $(_SDSL)/tlp-sleep 50 + + rm -f $(_SYSD)/tlp-sleep.service 51 + rm -f $(_ELOD)/49-tlp-sleep 52 + rm -f $(_SHCPL)/tlp-stat 53 + rm -f $(_SHCPL)/bluetooth 54 + diff --git a/tlp-sleep b/tlp-sleep 55 + deleted file mode 100644 56 + index 3de85ce..0000000 57 + --- a/tlp-sleep 58 + +++ /dev/null 59 + @@ -1,11 +0,0 @@ 60 + -#!/bin/sh 61 + - 62 + -# tlp - systemd suspend/resume hook 63 + -# 64 + -# Copyright (c) 2020 Thomas Koch <linrunner at gmx.net> and others. 65 + -# This software is licensed under the GPL v2 or later. 66 + - 67 + -case $1 in 68 + - pre) tlp suspend ;; 69 + - post) tlp resume ;; 70 + -esac 71 + diff --git a/tlp-sleep.service.in b/tlp-sleep.service.in 72 + new file mode 100644 73 + index 0000000..4ac17bd 74 + --- /dev/null 75 + +++ b/tlp-sleep.service.in 76 + @@ -0,0 +1,19 @@ 77 + +# tlp - systemd suspend/resume service 78 + +# 79 + +# Copyright (c) 2020 Thomas Koch <linrunner at gmx.net> and others. 80 + +# This software is licensed under the GPL v2 or later. 81 + + 82 + +[Unit] 83 + +Description=TLP suspend/resume 84 + +Before=sleep.target 85 + +StopWhenUnneeded=yes 86 + +Documentation=https://linrunner.de/tlp 87 + + 88 + +[Service] 89 + +Type=oneshot 90 + +RemainAfterExit=yes 91 + +ExecStart=@TLP_SBIN@/tlp suspend 92 + +ExecStop=@TLP_SBIN@/tlp resume 93 + + 94 + +[Install] 95 + +WantedBy=sleep.target