at 23.05-pre 2.5 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cpupower = config.boot.kernelPackages.cpupower; 7 cfg = config.powerManagement; 8in 9 10{ 11 ###### interface 12 13 options.powerManagement = { 14 15 # TODO: This should be aliased to powerManagement.cpufreq.governor. 16 # https://github.com/NixOS/nixpkgs/pull/53041#commitcomment-31825338 17 cpuFreqGovernor = mkOption { 18 type = types.nullOr types.str; 19 default = null; 20 example = "ondemand"; 21 description = lib.mdDoc '' 22 Configure the governor used to regulate the frequency of the 23 available CPUs. By default, the kernel configures the 24 performance governor, although this may be overwritten in your 25 hardware-configuration.nix file. 26 27 Often used values: "ondemand", "powersave", "performance" 28 ''; 29 }; 30 31 cpufreq = { 32 33 max = mkOption { 34 type = types.nullOr types.ints.unsigned; 35 default = null; 36 example = 2200000; 37 description = lib.mdDoc '' 38 The maximum frequency the CPU will use. Defaults to the maximum possible. 39 ''; 40 }; 41 42 min = mkOption { 43 type = types.nullOr types.ints.unsigned; 44 default = null; 45 example = 800000; 46 description = lib.mdDoc '' 47 The minimum frequency the CPU will use. 48 ''; 49 }; 50 }; 51 52 }; 53 54 55 ###### implementation 56 57 config = 58 let 59 governorEnable = cfg.cpuFreqGovernor != null; 60 maxEnable = cfg.cpufreq.max != null; 61 minEnable = cfg.cpufreq.min != null; 62 enable = 63 !config.boot.isContainer && 64 (governorEnable || maxEnable || minEnable); 65 in 66 mkIf enable { 67 68 boot.kernelModules = optional governorEnable "cpufreq_${cfg.cpuFreqGovernor}"; 69 70 environment.systemPackages = [ cpupower ]; 71 72 systemd.services.cpufreq = { 73 description = "CPU Frequency Setup"; 74 after = [ "systemd-modules-load.service" ]; 75 wantedBy = [ "multi-user.target" ]; 76 path = [ cpupower pkgs.kmod ]; 77 unitConfig.ConditionVirtualization = false; 78 serviceConfig = { 79 Type = "oneshot"; 80 RemainAfterExit = "yes"; 81 ExecStart = "${cpupower}/bin/cpupower frequency-set " + 82 optionalString governorEnable "--governor ${cfg.cpuFreqGovernor} " + 83 optionalString maxEnable "--max ${toString cfg.cpufreq.max} " + 84 optionalString minEnable "--min ${toString cfg.cpufreq.min} "; 85 SuccessExitStatus = "0 237"; 86 }; 87 }; 88 89 }; 90}