summary refs log tree commit diff
path: root/nixos/modules/services/hardware/undervolt.nix
blob: e5ef0601de3cd2ff5da0567190e898e87af8a990 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.services.undervolt;
in {
  options.services.undervolt = {
    enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to undervolt intel cpus.
      '';
    };

    verbose = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Whether to enable verbose logging.
      '';
    };

    package = mkOption {
      type = types.package;
      default = pkgs.undervolt;
      defaultText = "pkgs.undervolt";
      description = ''
        undervolt derivation to use.
      '';
    };

    coreOffset = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The amount of voltage to offset the CPU cores by. Accepts a floating point number.
      '';
    };

    gpuOffset = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The amount of voltage to offset the GPU by. Accepts a floating point number.
      '';
    };

    uncoreOffset = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The amount of voltage to offset uncore by. Accepts a floating point number.
      '';
    };

    analogioOffset = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The amount of voltage to offset analogio by. Accepts a floating point number.
      '';
    };

    temp = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The temperature target. Accepts a floating point number.
      '';
    };

    tempAc = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The temperature target on AC power. Accepts a floating point number.
      '';
    };

    tempBat = mkOption {
      type = types.nullOr types.str;
      default = null;
      description = ''
        The temperature target on battery power. Accepts a floating point number.
      '';
    };
  };

  config = mkIf cfg.enable {
    boot.kernelModules = [ "msr" ];

    environment.systemPackages = [ cfg.package ];

    systemd.services.undervolt = {
      path = [ pkgs.undervolt ];

      description = "Intel Undervolting Service";
      serviceConfig = {
        Type = "oneshot";
        Restart = "no";

        # `core` and `cache` are both intentionally set to `cfg.coreOffset` as according to the undervolt docs:
        #
        #     Core or Cache offsets have no effect. It is not possible to set different offsets for
        #     CPU Core and Cache. The CPU will take the smaller of the two offsets, and apply that to
        #     both CPU and Cache. A warning message will be displayed if you attempt to set different offsets.
        ExecStart = ''
          ${pkgs.undervolt}/bin/undervolt \
            ${optionalString cfg.verbose "--verbose"} \
            ${optionalString (cfg.coreOffset != null) "--core ${cfg.coreOffset}"} \
            ${optionalString (cfg.coreOffset != null) "--cache ${cfg.coreOffset}"} \
            ${optionalString (cfg.gpuOffset != null) "--gpu ${cfg.gpuOffset}"} \
            ${optionalString (cfg.uncoreOffset != null) "--uncore ${cfg.uncoreOffset}"} \
            ${optionalString (cfg.analogioOffset != null) "--analogio ${cfg.analogioOffset}"} \
            ${optionalString (cfg.temp != null) "--temp ${cfg.temp}"} \
            ${optionalString (cfg.tempAc != null) "--temp-ac ${cfg.tempAc}"} \
            ${optionalString (cfg.tempBat != null) "--temp-bat ${cfg.tempBat}"}
        '';
      };
    };

    systemd.timers.undervolt = {
      description = "Undervolt timer to ensure voltage settings are always applied";
      partOf = [ "undervolt.service" ];
      wantedBy = [ "multi-user.target" ];
      timerConfig = {
        OnBootSec = "2min";
        OnUnitActiveSec = "30";
      };
    };
  };
}