8 cfg = config.services.undervolt;
12 if (limit == null && window == null) then
15 assert lib.asserts.assertMsg (
16 limit != null && window != null
17 ) "Both power limit and window must be set";
18 "${toString limit} ${toString window}";
19 cliArgs = lib.cli.toGNUCommandLine { } {
25 # `core` and `cache` are both intentionally set to `cfg.coreOffset` as according to the undervolt docs:
27 # Core or Cache offsets have no effect. It is not possible to set different offsets for
28 # CPU Core and Cache. The CPU will take the smaller of the two offsets, and apply that to
29 # both CPU and Cache. A warning message will be displayed if you attempt to set different offsets.
30 core = cfg.coreOffset;
31 cache = cfg.coreOffset;
33 uncore = cfg.uncoreOffset;
34 analogio = cfg.analogioOffset;
36 temp-bat = cfg.tempBat;
39 power-limit-long = mkPLimit cfg.p1.limit cfg.p1.window;
40 power-limit-short = mkPLimit cfg.p2.limit cfg.p2.window;
44 options.services.undervolt = {
45 enable = lib.mkEnableOption ''
46 Undervolting service for Intel CPUs.
48 Warning: This service is not endorsed by Intel and may permanently damage your hardware. Use at your own risk
51 verbose = lib.mkOption {
52 type = lib.types.bool;
55 Whether to enable verbose logging.
59 package = lib.mkPackageOption pkgs "undervolt" { };
61 coreOffset = lib.mkOption {
62 type = lib.types.nullOr lib.types.int;
65 The amount of voltage in mV to offset the CPU cores by.
69 gpuOffset = lib.mkOption {
70 type = lib.types.nullOr lib.types.int;
73 The amount of voltage in mV to offset the GPU by.
77 uncoreOffset = lib.mkOption {
78 type = lib.types.nullOr lib.types.int;
81 The amount of voltage in mV to offset uncore by.
85 analogioOffset = lib.mkOption {
86 type = lib.types.nullOr lib.types.int;
89 The amount of voltage in mV to offset analogio by.
94 type = lib.types.nullOr lib.types.int;
97 The temperature target in Celsius degrees.
101 tempAc = lib.mkOption {
102 type = lib.types.nullOr lib.types.int;
105 The temperature target on AC power in Celsius degrees.
109 tempBat = lib.mkOption {
110 type = lib.types.nullOr lib.types.int;
113 The temperature target on battery power in Celsius degrees.
117 turbo = lib.mkOption {
118 type = lib.types.nullOr lib.types.int;
121 Changes the Intel Turbo feature status (1 is disabled and 0 is enabled).
125 p1.limit = lib.mkOption {
126 type = with lib.types; nullOr int;
129 The P1 Power Limit in Watts.
130 Both limit and window must be set.
133 p1.window = lib.mkOption {
142 The P1 Time Window in seconds.
143 Both limit and window must be set.
147 p2.limit = lib.mkOption {
148 type = with lib.types; nullOr int;
151 The P2 Power Limit in Watts.
152 Both limit and window must be set.
155 p2.window = lib.mkOption {
164 The P2 Time Window in seconds.
165 Both limit and window must be set.
169 useTimer = lib.mkOption {
170 type = lib.types.bool;
173 Whether to set a timer that applies the undervolt settings every 30s.
174 This will cause spam in the journal but might be required for some
175 hardware under specific conditions.
176 Enable this if your undervolt settings don't hold.
181 config = lib.mkIf cfg.enable {
182 hardware.cpu.x86.msr.enable = true;
184 environment.systemPackages = [ cfg.package ];
186 systemd.services.undervolt = {
187 description = "Intel Undervolting Service";
189 # Apply undervolt on boot, nixos generation switch and resume
194 after = [ "post-resume.target" ]; # Not sure why but it won't work without this
199 ExecStart = "${cfg.package}/bin/undervolt ${toString cliArgs}";
203 systemd.timers.undervolt = lib.mkIf cfg.useTimer {
204 description = "Undervolt timer to ensure voltage settings are always applied";
205 partOf = [ "undervolt.service" ];
206 wantedBy = [ "multi-user.target" ];
209 OnUnitActiveSec = "30";