1 { config, lib, pkgs, ... }:
3 cfg = config.services.tlp;
4 enableRDW = config.networking.networkmanager.enable;
5 tlp = pkgs.tlp.override { inherit enableRDW; };
6 # TODO: Use this for having proper parameters in the future
7 mkTlpConfig = tlpConfig: lib.generators.toKeyValue {
8 mkKeyValue = lib.generators.mkKeyValueDefault {
10 if lib.isList val then "\"" + (toString val) + "\""
19 enable = lib.mkOption {
20 type = lib.types.bool;
22 description = "Whether to enable the TLP power management daemon.";
25 settings = lib.mkOption {type = with lib.types; attrsOf (oneOf [bool int float str (listOf str)]);
28 SATA_LINKPWR_ON_BAT = "med_power_with_dipm";
29 USB_BLACKLIST_PHONE = 1;
32 Options passed to TLP. See https://linrunner.de/tlp for all supported options..
36 extraConfig = lib.mkOption {
37 type = lib.types.lines;
40 Verbatim additional configuration variables for TLP.
41 DEPRECATED: use services.tlp.settings instead.
48 config = lib.mkIf cfg.enable {
49 hardware.cpu.x86.msr.enable = true;
51 warnings = lib.optional (cfg.extraConfig != "") ''
52 Using config.services.tlp.extraConfig is deprecated and will become unsupported in a future release. Use config.services.tlp.settings instead.
56 assertion = cfg.enable -> config.powerManagement.scsiLinkPolicy == null;
58 `services.tlp.enable` and `config.powerManagement.scsiLinkPolicy` cannot be set both.
59 Set `services.tlp.settings.SATA_LINKPWR_ON_AC` and `services.tlp.settings.SATA_LINKPWR_ON_BAT` instead.
64 "tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig;
65 } // lib.optionalAttrs enableRDW {
66 "NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
67 "${tlp}/usr/lib/NetworkManager/dispatcher.d/99tlp-rdw-nm";
70 environment.systemPackages = [ tlp ];
73 services.tlp.settings = let
74 cfg = config.powerManagement;
75 maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val);
77 CPU_SCALING_GOVERNOR_ON_AC = maybeDefault cfg.cpuFreqGovernor;
78 CPU_SCALING_GOVERNOR_ON_BAT = maybeDefault cfg.cpuFreqGovernor;
79 CPU_SCALING_MIN_FREQ_ON_AC = maybeDefault cfg.cpufreq.min;
80 CPU_SCALING_MAX_FREQ_ON_AC = maybeDefault cfg.cpufreq.max;
81 CPU_SCALING_MIN_FREQ_ON_BAT = maybeDefault cfg.cpufreq.min;
82 CPU_SCALING_MAX_FREQ_ON_BAT = maybeDefault cfg.cpufreq.max;
85 services.udev.packages = [ tlp ];
88 # use native tlp instead because it can also differentiate between AC/BAT
89 services.cpufreq.enable = false;
92 # XXX: These must always be disabled/masked according to [1].
94 # [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319
95 sockets.systemd-rfkill.enable = false;
96 services.systemd-rfkill.enable = false;
99 # XXX: The service should reload whenever the configuration changes,
100 # otherwise newly set power options remain inactive until reboot (or
101 # manual unit restart.)
102 restartTriggers = [ config.environment.etc."tlp.conf".source ];
103 # XXX: When using systemd.packages (which we do above) the [Install]
104 # section of systemd units does not work (citation needed) so we manually
106 wantedBy = [ "multi-user.target" ];
109 services.tlp-sleep = {
110 # XXX: When using systemd.packages (which we do above) the [Install]
111 # section of systemd units does not work (citation needed) so we manually
113 before = [ "sleep.target" ];
114 wantedBy = [ "sleep.target" ];
115 # XXX: `tlp suspend` requires /var/lib/tlp to exist in order to save
116 # some stuff in there. There is no way, that I know of, to do this in
117 # the package itself, so we do it here instead making sure the unit
118 # won't fail due to the save dir not existing.
119 serviceConfig.StateDirectory = "tlp";