2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.tlp;
|
|
|
|
enableRDW = config.networking.networkmanager.enable;
|
|
|
|
tlp = pkgs.tlp.override { inherit enableRDW; };
|
|
|
|
# TODO: Use this for having proper parameters in the future
|
2024-09-19 14:19:46 +00:00
|
|
|
mkTlpConfig = tlpConfig: lib.generators.toKeyValue {
|
|
|
|
mkKeyValue = lib.generators.mkKeyValueDefault {
|
2020-04-24 23:36:52 +00:00
|
|
|
mkValueString = val:
|
2024-09-19 14:19:46 +00:00
|
|
|
if lib.isList val then "\"" + (toString val) + "\""
|
2020-08-20 17:08:02 +00:00
|
|
|
else toString val;
|
2020-04-24 23:36:52 +00:00
|
|
|
} "=";
|
|
|
|
} tlpConfig;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
options = {
|
|
|
|
services.tlp = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Whether to enable the TLP power management daemon.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
settings = lib.mkOption {type = with lib.types; attrsOf (oneOf [bool int float str (listOf str)]);
|
2020-08-20 17:08:02 +00:00
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
SATA_LINKPWR_ON_BAT = "med_power_with_dipm";
|
|
|
|
USB_BLACKLIST_PHONE = 1;
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Options passed to TLP. See https://linrunner.de/tlp for all supported options..
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
extraConfig = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Verbatim additional configuration variables for TLP.
|
2020-11-03 02:18:15 +00:00
|
|
|
DEPRECATED: use services.tlp.settings instead.
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2023-11-16 04:20:00 +00:00
|
|
|
hardware.cpu.x86.msr.enable = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
warnings = lib.optional (cfg.extraConfig != "") ''
|
2020-08-20 17:08:02 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
}];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
environment.etc = {
|
2020-08-20 17:08:02 +00:00
|
|
|
"tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig;
|
2024-09-19 14:19:46 +00:00
|
|
|
} // lib.optionalAttrs enableRDW {
|
2020-04-24 23:36:52 +00:00
|
|
|
"NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
|
2023-10-09 19:29:22 +00:00
|
|
|
"${tlp}/usr/lib/NetworkManager/dispatcher.d/99tlp-rdw-nm";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages = [ tlp ];
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
|
|
|
|
services.tlp.settings = let
|
|
|
|
cfg = config.powerManagement;
|
|
|
|
maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val);
|
|
|
|
in {
|
|
|
|
CPU_SCALING_GOVERNOR_ON_AC = maybeDefault cfg.cpuFreqGovernor;
|
|
|
|
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;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
services.udev.packages = [ tlp ];
|
|
|
|
|
|
|
|
systemd = {
|
2020-08-20 17:08:02 +00:00
|
|
|
# use native tlp instead because it can also differentiate between AC/BAT
|
|
|
|
services.cpufreq.enable = false;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
packages = [ tlp ];
|
|
|
|
# XXX: These must always be disabled/masked according to [1].
|
|
|
|
#
|
|
|
|
# [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319
|
|
|
|
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" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.tlp-sleep = {
|
|
|
|
# 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.
|
|
|
|
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";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|