nixpkgs/nixos/modules/tasks/cpu-freq.nix
(cdep)illabout b0f10d2d53
cpufreq: add option for setting the cpu max and min frequencies
This adds a NixOS option for setting the CPU max and min frequencies
with `cpufreq`.  The two options that have been added are:

- `powerManagement.cpufreq.max`
- `powerManagement.cpufreq.min`

It also adds an alias to the `powerManagement.cpuFreqGovernor` option as
`powerManagement.cpufreq.governor`.  This updates the installer to use
the new option name.  It also updates the manual with a note about
the new name.
2019-01-01 19:18:12 +09:00

86 lines
2.2 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cpupower = config.boot.kernelPackages.cpupower;
cfg = config.powerManagement.cpufreq;
in
{
###### interface
options.powerManagement.cpufreq = {
governor = mkOption {
type = types.nullOr types.str;
default = null;
example = "ondemand";
description = ''
Configure the governor used to regulate the frequence of the
available CPUs. By default, the kernel configures the
performance governor, although this may be overwriten in your
hardware-configuration.nix file.
Often used values: "ondemand", "powersave", "performance"
'';
};
max = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
example = 2200000;
description = ''
The maximum frequency the CPU will use. Defaults to the maximum possible.
'';
};
min = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
example = 800000;
description = ''
The minimum frequency the CPU will use.
'';
};
};
###### implementation
config =
let
governorEnable = cfg.governor != null;
maxEnable = cfg.max != null;
minEnable = cfg.min != null;
enable =
!config.boot.isContainer &&
(governorEnable || maxEnable || minEnable);
in
mkIf enable {
boot.kernelModules = optional governorEnable "cpufreq_${cfg.governor}";
environment.systemPackages = [ cpupower ];
systemd.services.cpufreq = {
description = "CPU Frequency Setup";
after = [ "systemd-modules-load.service" ];
wantedBy = [ "multi-user.target" ];
path = [ cpupower pkgs.kmod ];
unitConfig.ConditionVirtualization = false;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
ExecStart = "${cpupower}/bin/cpupower frequency-set " +
optionalString governorEnable "--governor ${cfg.governor} " +
optionalString maxEnable "--max ${toString cfg.max} " +
optionalString minEnable "--min ${toString cfg.min} ";
SuccessExitStatus = "0 237";
};
};
};
}