summary refs log tree commit diff
path: root/nixos/modules/services/hardware/tlp.nix
blob: 4f8af7978286a79f7abc3ef0d79eb517eb2bcb63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{ config, lib, pkgs, ... }:

with lib;

let

cfg = config.services.tlp;

enableRDW = config.networking.networkmanager.enable;

tlp = pkgs.tlp.override {
  inherit enableRDW;
};

# XXX: We can't use writeTextFile + readFile here because it triggers
# TLP build to get the .drv (even on --dry-run).
confFile = pkgs.runCommand "tlp"
  { config = cfg.extraConfig;
    passAsFile = [ "config" ];
    preferLocalBuild = true;
  }
  ''
    cat ${tlp}/etc/default/tlp > $out
    cat $configPath >> $out
  '';

in

{

  ###### interface

  options = {

    services.tlp = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable the TLP daemon.";
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Additional configuration variables for TLP";
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    powerManagement.scsiLinkPolicy = null;
    powerManagement.cpuFreqGovernor = null;
    powerManagement.cpufreq.max = null;
    powerManagement.cpufreq.min = null;

    systemd.sockets.systemd-rfkill.enable = false;

    systemd.services = {
      "systemd-rfkill@".enable = false;
      systemd-rfkill.enable = false;

      tlp = {
        description = "TLP system startup/shutdown";

        after = [ "multi-user.target" ];
        wantedBy = [ "multi-user.target" ];
        before = [ "shutdown.target" ];
        restartTriggers = [ confFile ];

        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = "${tlp}/bin/tlp init start";
          ExecStop = "${tlp}/bin/tlp init stop";
        };
      };

      tlp-sleep = {
        description = "TLP suspend/resume";

        wantedBy = [ "sleep.target" ];
        before = [ "sleep.target" ];

        unitConfig = {
          StopWhenUnneeded = true;
        };

        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = true;
          ExecStart = "${tlp}/bin/tlp suspend";
          ExecStop = "${tlp}/bin/tlp resume";
        };
      };
    };

    services.udev.packages = [ tlp ];

    environment.etc = [{ source = confFile;
                         target = "default/tlp";
                       }
                      ] ++ optional enableRDW {
                        source = "${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
                        target = "NetworkManager/dispatcher.d/99tlp-rdw-nm";
                      };

    environment.systemPackages = [ tlp ];

    boot.kernelModules = [ "msr" ];

  };

}