nixpkgs-suyu/nixos/modules/tasks/cpu-freq.nix
pennae 2e751c0772 treewide: automatically md-convert option descriptions
the conversion procedure is simple:

 - find all things that look like options, ie calls to either `mkOption`
   or `lib.mkOption` that take an attrset. remember the attrset as the
   option
 - for all options, find a `description` attribute who's value is not a
   call to `mdDoc` or `lib.mdDoc`
 - textually convert the entire value of the attribute to MD with a few
   simple regexes (the set from mdize-module.sh)
 - if the change produced a change in the manual output, discard
 - if the change kept the manual unchanged, add some text to the
   description to make sure we've actually found an option. if the
   manual changes this time, keep the converted description

this procedure converts 80% of nixos options to markdown. around 2000
options remain to be inspected, but most of those fail the "does not
change the manual output check": currently the MD conversion process
does not faithfully convert docbook tags like <code> and <package>, so
any option using such tags will not be converted at all.
2022-07-30 15:16:34 +02:00

90 lines
2.4 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cpupower = config.boot.kernelPackages.cpupower;
cfg = config.powerManagement;
in
{
###### interface
options.powerManagement = {
# TODO: This should be aliased to powerManagement.cpufreq.governor.
# https://github.com/NixOS/nixpkgs/pull/53041#commitcomment-31825338
cpuFreqGovernor = mkOption {
type = types.nullOr types.str;
default = null;
example = "ondemand";
description = lib.mdDoc ''
Configure the governor used to regulate the frequency of the
available CPUs. By default, the kernel configures the
performance governor, although this may be overwritten in your
hardware-configuration.nix file.
Often used values: "ondemand", "powersave", "performance"
'';
};
cpufreq = {
max = mkOption {
type = types.nullOr types.ints.unsigned;
default = null;
example = 2200000;
description = lib.mdDoc ''
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 = lib.mdDoc ''
The minimum frequency the CPU will use.
'';
};
};
};
###### implementation
config =
let
governorEnable = cfg.cpuFreqGovernor != null;
maxEnable = cfg.cpufreq.max != null;
minEnable = cfg.cpufreq.min != null;
enable =
!config.boot.isContainer &&
(governorEnable || maxEnable || minEnable);
in
mkIf enable {
boot.kernelModules = optional governorEnable "cpufreq_${cfg.cpuFreqGovernor}";
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.cpuFreqGovernor} " +
optionalString maxEnable "--max ${toString cfg.cpufreq.max} " +
optionalString minEnable "--min ${toString cfg.cpufreq.min} ";
SuccessExitStatus = "0 237";
};
};
};
}