2020-04-24 23:36:52 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.undervolt;
|
2020-11-03 02:18:15 +00:00
|
|
|
|
|
|
|
mkPLimit = limit: window:
|
2023-03-24 00:07:29 +00:00
|
|
|
if (limit == null && window == null) then null
|
2024-09-19 14:19:46 +00:00
|
|
|
else assert lib.asserts.assertMsg (limit != null && window != null) "Both power limit and window must be set";
|
2020-11-03 02:18:15 +00:00
|
|
|
"${toString limit} ${toString window}";
|
|
|
|
cliArgs = lib.cli.toGNUCommandLine {} {
|
2020-06-15 15:56:04 +00:00
|
|
|
inherit (cfg)
|
|
|
|
verbose
|
|
|
|
temp
|
2024-02-29 20:09:43 +00:00
|
|
|
turbo
|
2020-06-15 15:56:04 +00:00
|
|
|
;
|
|
|
|
# `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.
|
|
|
|
core = cfg.coreOffset;
|
|
|
|
cache = cfg.coreOffset;
|
|
|
|
gpu = cfg.gpuOffset;
|
|
|
|
uncore = cfg.uncoreOffset;
|
|
|
|
analogio = cfg.analogioOffset;
|
|
|
|
|
|
|
|
temp-bat = cfg.tempBat;
|
|
|
|
temp-ac = cfg.tempAc;
|
2020-11-03 02:18:15 +00:00
|
|
|
|
|
|
|
power-limit-long = mkPLimit cfg.p1.limit cfg.p1.window;
|
|
|
|
power-limit-short = mkPLimit cfg.p2.limit cfg.p2.window;
|
2020-06-15 15:56:04 +00:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
2020-04-24 23:36:52 +00:00
|
|
|
options.services.undervolt = {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption ''
|
2020-07-18 16:06:22 +00:00
|
|
|
Undervolting service for Intel CPUs.
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
Warning: This service is not endorsed by Intel and may permanently damage your hardware. Use at your own risk
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
verbose = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether to enable verbose logging.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
package = lib.mkPackageOption pkgs "undervolt" { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
coreOffset = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The amount of voltage in mV to offset the CPU cores by.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
gpuOffset = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The amount of voltage in mV to offset the GPU by.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
uncoreOffset = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The amount of voltage in mV to offset uncore by.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
analogioOffset = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The amount of voltage in mV to offset analogio by.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
temp = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The temperature target in Celsius degrees.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
tempAc = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The temperature target on AC power in Celsius degrees.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
tempBat = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-06-15 15:56:04 +00:00
|
|
|
The temperature target on battery power in Celsius degrees.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
turbo = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.int;
|
2024-02-29 20:09:43 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2024-02-29 20:09:43 +00:00
|
|
|
Changes the Intel Turbo feature status (1 is disabled and 0 is enabled).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
p1.limit = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr int;
|
2020-11-03 02:18:15 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-11-03 02:18:15 +00:00
|
|
|
The P1 Power Limit in Watts.
|
|
|
|
Both limit and window must be set.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
p1.window = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr (oneOf [ float int ]);
|
2020-11-03 02:18:15 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-11-03 02:18:15 +00:00
|
|
|
The P1 Time Window in seconds.
|
|
|
|
Both limit and window must be set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
p2.limit = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr int;
|
2020-11-03 02:18:15 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-11-03 02:18:15 +00:00
|
|
|
The P2 Power Limit in Watts.
|
|
|
|
Both limit and window must be set.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
p2.window = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr (oneOf [ float int ]);
|
2020-11-03 02:18:15 +00:00
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-11-03 02:18:15 +00:00
|
|
|
The P2 Time Window in seconds.
|
|
|
|
Both limit and window must be set.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
useTimer = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-09-25 04:45:31 +00:00
|
|
|
default = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Whether to set a timer that applies the undervolt settings every 30s.
|
|
|
|
This will cause spam in the journal but might be required for some
|
|
|
|
hardware under specific conditions.
|
|
|
|
Enable this if your undervolt settings don't hold.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2023-11-16 04:20:00 +00:00
|
|
|
hardware.cpu.x86.msr.enable = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
|
|
|
systemd.services.undervolt = {
|
|
|
|
description = "Intel Undervolting Service";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
# Apply undervolt on boot, nixos generation switch and resume
|
|
|
|
wantedBy = [ "multi-user.target" "post-resume.target" ];
|
|
|
|
after = [ "post-resume.target" ]; # Not sure why but it won't work without this
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
Restart = "no";
|
2022-02-20 05:27:41 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/undervolt ${toString cliArgs}";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
systemd.timers.undervolt = lib.mkIf cfg.useTimer {
|
2020-04-24 23:36:52 +00:00
|
|
|
description = "Undervolt timer to ensure voltage settings are always applied";
|
|
|
|
partOf = [ "undervolt.service" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnBootSec = "2min";
|
|
|
|
OnUnitActiveSec = "30";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|