1{ config, lib, pkgs, ... }:
2with lib;
3let
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 {
10 mkValueString = val:
11 if isList val then "\"" + (toString val) + "\""
12 else toString val;
13 } "=";
14 } tlpConfig;
15in
16{
17 ###### interface
18 options = {
19 services.tlp = {
20 enable = mkOption {
21 type = types.bool;
22 default = false;
23 description = lib.mdDoc "Whether to enable the TLP power management daemon.";
24 };
25
26 settings = mkOption {type = with types; attrsOf (oneOf [bool int float str (listOf str)]);
27 default = {};
28 example = {
29 SATA_LINKPWR_ON_BAT = "med_power_with_dipm";
30 USB_BLACKLIST_PHONE = 1;
31 };
32 description = lib.mdDoc ''
33 Options passed to TLP. See https://linrunner.de/tlp for all supported options..
34 '';
35 };
36
37 extraConfig = mkOption {
38 type = types.lines;
39 default = "";
40 description = lib.mdDoc ''
41 Verbatim additional configuration variables for TLP.
42 DEPRECATED: use services.tlp.settings instead.
43 '';
44 };
45 };
46 };
47
48 ###### implementation
49 config = mkIf cfg.enable {
50 boot.kernelModules = [ "msr" ];
51
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.
54 '';
55
56 assertions = [{
57 assertion = cfg.enable -> config.powerManagement.scsiLinkPolicy == null;
58 message = ''
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.
61 '';
62 }];
63
64 environment.etc = {
65 "tlp.conf".text = (mkTlpConfig cfg.settings) + cfg.extraConfig;
66 } // optionalAttrs enableRDW {
67 "NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
68 "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
69 };
70
71 environment.systemPackages = [ tlp ];
72
73
74 services.tlp.settings = let
75 cfg = config.powerManagement;
76 maybeDefault = val: lib.mkIf (val != null) (lib.mkDefault val);
77 in {
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;
84 };
85
86 services.udev.packages = [ tlp ];
87
88 systemd = {
89 # use native tlp instead because it can also differentiate between AC/BAT
90 services.cpufreq.enable = false;
91
92 packages = [ tlp ];
93 # XXX: These must always be disabled/masked according to [1].
94 #
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;
98
99 services.tlp = {
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
106 # enforce it here.
107 wantedBy = [ "multi-user.target" ];
108 };
109
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
113 # enforce it here.
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";
121 };
122 };
123 };
124}