tlp: use structured config to fix cpu governor

Previously this module just disabled them.
Now tlp merges system defaults in
This commit is contained in:
Jörg Thalheim 2020-07-27 15:28:29 +01:00
parent 98ebda65a1
commit 4d0077addd
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92

View File

@ -8,12 +8,8 @@ let
mkTlpConfig = tlpConfig: generators.toKeyValue { mkTlpConfig = tlpConfig: generators.toKeyValue {
mkKeyValue = generators.mkKeyValueDefault { mkKeyValue = generators.mkKeyValueDefault {
mkValueString = val: mkValueString = val:
if isInt val then toString val if isList val then "\"" + (toString val) + "\""
else if isString val then val else toString val;
else if true == val then "1"
else if false == val then "0"
else if isList val then "\"" + (concatStringsSep " " val) + "\""
else err "invalid value provided to mkTlpConfig:" (toString val);
} "="; } "=";
} tlpConfig; } tlpConfig;
in in
@ -27,10 +23,24 @@ in
description = "Whether to enable the TLP power management daemon."; description = "Whether to enable the TLP power management daemon.";
}; };
settings = mkOption {type = with types; attrsOf (oneOf [bool int float str (listOf str)]);
default = {};
example = {
SATA_LINKPWR_ON_BAT = "med_power_with_dipm";
USB_BLACKLIST_PHONE = 1;
};
description = ''
Options passed to TLP. See https://linrunner.de/tlp for all supported options..
'';
};
extraConfig = mkOption { extraConfig = mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
description = "Additional configuration variables for TLP"; description = ''
Verbatim additional configuration variables for TLP.
DEPRECATED: use services.tlp.config instead.
'';
}; };
}; };
}; };
@ -39,8 +49,20 @@ in
config = mkIf cfg.enable { config = mkIf cfg.enable {
boot.kernelModules = [ "msr" ]; boot.kernelModules = [ "msr" ];
warnings = optional (cfg.extraConfig != "") ''
Using config.services.tlp.extraConfig is deprecated and will become unsupported in a future release. Use config.services.tlp.settings instead.
'';
assertions = [{
assertion = cfg.enable -> config.powerManagement.scsiLinkPolicy == null;
message = ''
`services.tlp.enable` and `config.powerManagement.scsiLinkPolicy` cannot be set both.
Set `services.tlp.settings.SATA_LINKPWR_ON_AC` and `services.tlp.settings.SATA_LINKPWR_ON_BAT` instead.
'';
}];
environment.etc = { environment.etc = {
"tlp.conf".text = cfg.extraConfig; "tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig;
} // optionalAttrs enableRDW { } // optionalAttrs enableRDW {
"NetworkManager/dispatcher.d/99tlp-rdw-nm".source = "NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
"${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm"; "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
@ -48,18 +70,25 @@ in
environment.systemPackages = [ tlp ]; environment.systemPackages = [ tlp ];
# FIXME: When the config is parametrized we need to move these into a
# conditional on the relevant options being enabled. services.tlp.settings = let
powerManagement = { cfg = config.powerManagement;
scsiLinkPolicy = null; maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val);
cpuFreqGovernor = null; in {
cpufreq.max = null; CPU_SCALING_GOVERNOR_ON_AC = maybeDefault cfg.cpuFreqGovernor;
cpufreq.min = null; CPU_SCALING_GOVERNOR_ON_BAT = maybeDefault cfg.cpuFreqGovernor;
CPU_SCALING_MIN_FREQ_ON_AC = maybeDefault cfg.cpufreq.min;
CPU_SCALING_MAX_FREQ_ON_AC = maybeDefault cfg.cpufreq.max;
CPU_SCALING_MIN_FREQ_ON_BAT = maybeDefault cfg.cpufreq.min;
CPU_SCALING_MAX_FREQ_ON_BAT = maybeDefault cfg.cpufreq.max;
}; };
services.udev.packages = [ tlp ]; services.udev.packages = [ tlp ];
systemd = { systemd = {
# use native tlp instead because it can also differentiate between AC/BAT
services.cpufreq.enable = false;
packages = [ tlp ]; packages = [ tlp ];
# XXX: These must always be disabled/masked according to [1]. # XXX: These must always be disabled/masked according to [1].
# #