about summary refs log tree commit diff
path: root/nixos/modules/tasks/cpu-freq.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nixos/modules/tasks/cpu-freq.nix')
-rw-r--r--nixos/modules/tasks/cpu-freq.nix71
1 files changed, 52 insertions, 19 deletions
diff --git a/nixos/modules/tasks/cpu-freq.nix b/nixos/modules/tasks/cpu-freq.nix
index 5f8b5df52acf..684c43a1e903 100644
--- a/nixos/modules/tasks/cpu-freq.nix
+++ b/nixos/modules/tasks/cpu-freq.nix
@@ -4,22 +4,43 @@ with lib;
 
 let
   cpupower = config.boot.kernelPackages.cpupower;
-  cfg = config.powerManagement;
+  cfg = config.powerManagement.cpufreq;
 in
 
 {
   ###### interface
 
-  options = {
+  options.powerManagement.cpufreq = {
 
-    powerManagement.cpuFreqGovernor = mkOption {
+    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.
+        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.
       '';
     };
 
@@ -28,25 +49,37 @@ in
 
   ###### implementation
 
-  config = mkIf (!config.boot.isContainer && config.powerManagement.cpuFreqGovernor != null) {
+  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 = [ "cpufreq_${cfg.cpuFreqGovernor}" ];
+      boot.kernelModules = optional governorEnable "cpufreq_${cfg.governor}";
 
-    environment.systemPackages = [ cpupower ];
+      environment.systemPackages = [ cpupower ];
 
-    systemd.services.cpufreq = {
-      description = "CPU Frequency Governor 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 -g ${cfg.cpuFreqGovernor}";
-        SuccessExitStatus = "0 237";
+      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";
+        };
       };
-    };
 
   };
 }