nixos/tlp: revamp

This commit is contained in:
Bernardo Meurer 2020-02-25 20:19:27 -08:00
parent 07cc033524
commit ee7becd918
No known key found for this signature in database
GPG Key ID: E421C74191EA186C

View File

@ -1,39 +1,26 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib; with lib;
let let
cfg = config.services.tlp;
cfg = config.services.tlp; enableRDW = config.networking.networkmanager.enable;
tlp = pkgs.tlp.override { inherit enableRDW; };
enableRDW = config.networking.networkmanager.enable; # TODO: Use this for having proper parameters in the future
mkTlpConfig = tlpConfig: generators.toKeyValue {
tlp = pkgs.tlp.override { mkKeyValue = generators.mkKeyValueDefault {
inherit enableRDW; mkValueString = val:
}; if isInt val then toString val
else if isString val then val
# XXX: We can't use writeTextFile + readFile here because it triggers else if true == val then "1"
# TLP build to get the .drv (even on --dry-run). else if false == val then "0"
confFile = pkgs.runCommand "tlp" else if isList val then "\"" + (concatStringsSep " " val) + "\""
{ config = cfg.extraConfig; else err "invalid value provided to mkTlpConfig:" (toString val);
passAsFile = [ "config" ]; } "=";
preferLocalBuild = true; } tlpConfig;
}
''
cat ${tlp}/etc/default/tlp > $out
cat $configPath >> $out
'';
in in
{ {
###### interface ###### interface
options = { options = {
services.tlp = { services.tlp = {
enable = mkOption { enable = mkOption {
type = types.bool; type = types.bool;
default = false; default = false;
@ -45,77 +32,64 @@ in
default = ""; default = "";
description = "Additional configuration variables for TLP"; description = "Additional configuration variables for TLP";
}; };
}; };
}; };
###### implementation ###### implementation
config = mkIf cfg.enable { config = mkIf cfg.enable {
boot.kernelModules = [ "msr" ];
powerManagement.scsiLinkPolicy = null; environment.etc = {
powerManagement.cpuFreqGovernor = null; "tlp.conf".text = cfg.extraConfig;
powerManagement.cpufreq.max = null; } // optionalAttrs enableRDW {
powerManagement.cpufreq.min = null; "NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
"${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
};
systemd.sockets.systemd-rfkill.enable = false; environment.systemPackages = [ tlp ];
systemd.services = { # FIXME: When the config is parametrized we need to move these into a
"systemd-rfkill@".enable = false; # conditional on the relevant options being enabled.
systemd-rfkill.enable = false; powerManagement = {
scsiLinkPolicy = null;
tlp = { cpuFreqGovernor = null;
description = "TLP system startup/shutdown"; cpufreq.max = null;
cpufreq.min = null;
after = [ "multi-user.target" ];
wantedBy = [ "multi-user.target" ];
before = [ "shutdown.target" ];
restartTriggers = [ confFile ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${tlp}/bin/tlp init start";
ExecStop = "${tlp}/bin/tlp init stop";
};
};
tlp-sleep = {
description = "TLP suspend/resume";
wantedBy = [ "sleep.target" ];
before = [ "sleep.target" ];
unitConfig = {
StopWhenUnneeded = true;
};
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${tlp}/bin/tlp suspend";
ExecStop = "${tlp}/bin/tlp resume";
};
};
}; };
services.udev.packages = [ tlp ]; services.udev.packages = [ tlp ];
environment.etc = systemd = {
{ packages = [ tlp ];
"default/tlp".source = confFile; # XXX: These must always be disabled/masked according to [1].
} // optionalAttrs enableRDW { #
"NetworkManager/dispatcher.d/99tlp-rdw-nm" = { # [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319
source = "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm"; sockets.systemd-rfkill.enable = false;
}; services.systemd-rfkill.enable = false;
services.tlp = {
# XXX: The service should reload whenever the configuration changes,
# otherwise newly set power options remain inactive until reboot (or
# manual unit restart.)
restartTriggers = [ config.environment.etc."tlp.conf".source ];
# XXX: When using systemd.packages (which we do above) the [Install]
# section of systemd units does not work (citation needed) so we manually
# enforce it here.
wantedBy = [ "multi-user.target" ];
}; };
environment.systemPackages = [ tlp ]; services.tlp-sleep = {
# XXX: When using systemd.packages (which we do above) the [Install]
boot.kernelModules = [ "msr" ]; # section of systemd units does not work (citation needed) so we manually
# enforce it here.
before = [ "sleep.target" ];
wantedBy = [ "sleep.target" ];
# XXX: `tlp suspend` requires /var/lib/tlp to exist in order to save
# some stuff in there. There is no way, that I know of, to do this in
# the package itself, so we do it here instead making sure the unit
# won't fail due to the save dir not existing.
serviceConfig.StateDirectory = "tlp";
};
};
}; };
} }