1 { config, lib, pkgs, ... }:
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 {
11 if isList val then "\"" + (toString val) + "\""
23 description = lib.mdDoc "Whether to enable the TLP power management daemon.";
26 settings = mkOption {type = with types; attrsOf (oneOf [bool int float str (listOf str)]);
29 SATA_LINKPWR_ON_BAT = "med_power_with_dipm";
30 USB_BLACKLIST_PHONE = 1;
32 description = lib.mdDoc ''
33 Options passed to TLP. See https://linrunner.de/tlp for all supported options..
37 extraConfig = mkOption {
40 description = lib.mdDoc ''
41 Verbatim additional configuration variables for TLP.
42 DEPRECATED: use services.tlp.settings instead.
49 config = mkIf cfg.enable {
50 hardware.cpu.x86.msr.enable = true;
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.
57 assertion = cfg.enable -> config.powerManagement.scsiLinkPolicy == null;
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.
65 "tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig;
66 } // optionalAttrs enableRDW {
67 "NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
68 "${tlp}/usr/lib/NetworkManager/dispatcher.d/99tlp-rdw-nm";
71 environment.systemPackages = [ tlp ];
74 services.tlp.settings = let
75 cfg = config.powerManagement;
76 maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val);
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;
86 services.udev.packages = [ tlp ];
89 # use native tlp instead because it can also differentiate between AC/BAT
90 services.cpufreq.enable = false;
93 # XXX: These must always be disabled/masked according to [1].
95 # [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319
96 sockets.systemd-rfkill.enable = false;
97 services.systemd-rfkill.enable = false;
100 # XXX: The service should reload whenever the configuration changes,
101 # otherwise newly set power options remain inactive until reboot (or
102 # manual unit restart.)
103 restartTriggers = [ config.environment.etc."tlp.conf".source ];
104 # XXX: When using systemd.packages (which we do above) the [Install]
105 # section of systemd units does not work (citation needed) so we manually
107 wantedBy = [ "multi-user.target" ];
110 services.tlp-sleep = {
111 # XXX: When using systemd.packages (which we do above) the [Install]
112 # section of systemd units does not work (citation needed) so we manually
114 before = [ "sleep.target" ];
115 wantedBy = [ "sleep.target" ];
116 # XXX: `tlp suspend` requires /var/lib/tlp to exist in order to save
117 # some stuff in there. There is no way, that I know of, to do this in
118 # the package itself, so we do it here instead making sure the unit
119 # won't fail due to the save dir not existing.
120 serviceConfig.StateDirectory = "tlp";