Merge pull request #94064 from Mic92/tlp

tlp: use structured config to fix cpu governor

authored by

Jörg Thalheim and committed by
GitHub
63395873 aa37dc8a

+44 -15
+44 -15
nixos/modules/services/hardware/tlp.nix
··· 8 8 mkTlpConfig = tlpConfig: generators.toKeyValue { 9 9 mkKeyValue = generators.mkKeyValueDefault { 10 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); 11 + if isList val then "\"" + (toString val) + "\"" 12 + else toString val; 17 13 } "="; 18 14 } tlpConfig; 19 15 in ··· 27 23 description = "Whether to enable the TLP power management daemon."; 28 24 }; 29 25 26 + settings = mkOption {type = with types; attrsOf (oneOf [bool int float str (listOf str)]); 27 + default = {}; 28 + example = { 29 + SATA_LINKPWR_ON_BAT = "med_power_with_dipm"; 30 + USB_BLACKLIST_PHONE = 1; 31 + }; 32 + description = '' 33 + Options passed to TLP. See https://linrunner.de/tlp for all supported options.. 34 + ''; 35 + }; 36 + 30 37 extraConfig = mkOption { 31 38 type = types.lines; 32 39 default = ""; 33 - description = "Additional configuration variables for TLP"; 40 + description = '' 41 + Verbatim additional configuration variables for TLP. 42 + DEPRECATED: use services.tlp.config instead. 43 + ''; 34 44 }; 35 45 }; 36 46 }; ··· 39 49 config = mkIf cfg.enable { 40 50 boot.kernelModules = [ "msr" ]; 41 51 52 + warnings = optional (cfg.extraConfig != "") '' 53 + Using config.services.tlp.extraConfig is deprecated and will become unsupported in a future release. Use config.services.tlp.settings instead. 54 + ''; 55 + 56 + assertions = [{ 57 + assertion = cfg.enable -> config.powerManagement.scsiLinkPolicy == null; 58 + message = '' 59 + `services.tlp.enable` and `config.powerManagement.scsiLinkPolicy` cannot be set both. 60 + Set `services.tlp.settings.SATA_LINKPWR_ON_AC` and `services.tlp.settings.SATA_LINKPWR_ON_BAT` instead. 61 + ''; 62 + }]; 63 + 42 64 environment.etc = { 43 - "tlp.conf".text = cfg.extraConfig; 65 + "tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig; 44 66 } // optionalAttrs enableRDW { 45 67 "NetworkManager/dispatcher.d/99tlp-rdw-nm".source = 46 68 "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm"; ··· 48 70 49 71 environment.systemPackages = [ tlp ]; 50 72 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; 73 + 74 + services.tlp.settings = let 75 + cfg = config.powerManagement; 76 + maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val); 77 + in { 78 + CPU_SCALING_GOVERNOR_ON_AC = maybeDefault cfg.cpuFreqGovernor; 79 + CPU_SCALING_GOVERNOR_ON_BAT = maybeDefault cfg.cpuFreqGovernor; 80 + CPU_SCALING_MIN_FREQ_ON_AC = maybeDefault cfg.cpufreq.min; 81 + CPU_SCALING_MAX_FREQ_ON_AC = maybeDefault cfg.cpufreq.max; 82 + CPU_SCALING_MIN_FREQ_ON_BAT = maybeDefault cfg.cpufreq.min; 83 + CPU_SCALING_MAX_FREQ_ON_BAT = maybeDefault cfg.cpufreq.max; 58 84 }; 59 85 60 86 services.udev.packages = [ tlp ]; 61 87 62 88 systemd = { 89 + # use native tlp instead because it can also differentiate between AC/BAT 90 + services.cpufreq.enable = false; 91 + 63 92 packages = [ tlp ]; 64 93 # XXX: These must always be disabled/masked according to [1]. 65 94 #