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.str;
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 systemd.services = {
58 tlp = {
59 description = "TLP system startup/shutdown";
60
61 after = [ "multi-user.target" ];
62 wantedBy = [ "multi-user.target" ];
63 before = [ "shutdown.target" ];
64
65 serviceConfig = {
66 Type = "oneshot";
67 RemainAfterExit = true;
68 ExecStart = "${tlp}/bin/tlp init start";
69 ExecStop = "${tlp}/bin/tlp init stop";
70 };
71
72 environment.MODULE_DIR="/run/current-system/kernel-modules/lib/modules/";
73 };
74
75 tlp-sleep = {
76 description = "TLP suspend/resume";
77
78 wantedBy = [ "sleep.target" ];
79 before = [ "sleep.target" ];
80
81 unitConfig = {
82 StopWhenUnneeded = true;
83 };
84
85 serviceConfig = {
86 Type = "oneshot";
87 RemainAfterExit = true;
88 ExecStart = "${tlp}/bin/tlp suspend";
89 ExecStop = "${tlp}/bin/tlp resume";
90 };
91
92 environment.MODULE_DIR="/run/current-system/kernel-modules/lib/modules/";
93 };
94 };
95
96 services.udev.packages = [ tlp ];
97
98 environment.etc = [{ source = confFile;
99 target = "default/tlp";
100 }
101 ] ++ optional enableRDW {
102 source = "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
103 target = "NetworkManager/dispatcher.d/99tlp-rdw-nm";
104 };
105
106 environment.systemPackages = [ tlp ];
107
108 boot.kernelModules = [ "msr" ];
109
110 };
111
112}