1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7cfg = config.services.tlp;
8
9enableRDW = config.networking.networkmanager.enable;
10
11tlp = pkgs.tlp.override {
12 inherit enableRDW;
13};
14
15# XXX: We can't use writeTextFile + readFile here because it triggers
16# TLP build to get the .drv (even on --dry-run).
17confFile = pkgs.runCommand "tlp"
18 { config = cfg.extraConfig;
19 passAsFile = [ "config" ];
20 }
21 ''
22 cat ${tlp}/etc/default/tlp > $out
23 cat $configPath >> $out
24 '';
25
26in
27
28{
29
30 ###### interface
31
32 options = {
33
34 services.tlp = {
35
36 enable = mkOption {
37 type = types.bool;
38 default = false;
39 description = "Whether to enable the TLP daemon.";
40 };
41
42 extraConfig = mkOption {
43 type = types.lines;
44 default = "";
45 description = "Additional configuration variables for TLP";
46 };
47
48 };
49
50 };
51
52
53 ###### implementation
54
55 config = mkIf cfg.enable {
56
57 powerManagement.scsiLinkPolicy = null;
58 powerManagement.cpuFreqGovernor = null;
59
60 systemd.sockets."systemd-rfkill".enable = false;
61
62 systemd.services = {
63 "systemd-rfkill@".enable = false;
64 "systemd-rfkill".enable = false;
65
66 tlp = {
67 description = "TLP system startup/shutdown";
68
69 after = [ "multi-user.target" ];
70 wantedBy = [ "multi-user.target" ];
71 before = [ "shutdown.target" ];
72 restartTriggers = [ confFile ];
73
74 serviceConfig = {
75 Type = "oneshot";
76 RemainAfterExit = true;
77 ExecStart = "${tlp}/bin/tlp init start";
78 ExecStop = "${tlp}/bin/tlp init stop";
79 };
80 };
81
82 tlp-sleep = {
83 description = "TLP suspend/resume";
84
85 wantedBy = [ "sleep.target" ];
86 before = [ "sleep.target" ];
87
88 unitConfig = {
89 StopWhenUnneeded = true;
90 };
91
92 serviceConfig = {
93 Type = "oneshot";
94 RemainAfterExit = true;
95 ExecStart = "${tlp}/bin/tlp suspend";
96 ExecStop = "${tlp}/bin/tlp resume";
97 };
98 };
99 };
100
101 services.udev.packages = [ tlp ];
102
103 environment.etc = [{ source = confFile;
104 target = "default/tlp";
105 }
106 ] ++ optional enableRDW {
107 source = "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
108 target = "NetworkManager/dispatcher.d/99tlp-rdw-nm";
109 };
110
111 environment.systemPackages = [ tlp ];
112
113 boot.kernelModules = [ "msr" ];
114
115 };
116
117}