summary refs log tree commit diff
path: root/nixos/modules/tasks/cpu-freq.nix
blob: 8f441aa134d4f50cea8b66bafcdb8fc48f19463a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{ config, pkgs, ... }:

with pkgs.lib;

{
  ###### interface

  options = {

    powerManagement.cpuFreqGovernor = mkOption {
      default = "";
      example = "ondemand";
      type = types.str;
      description = ''
        Configure the governor used to regulate the frequence of the
        available CPUs. By default, the kernel configures the
        on-demand governor.
      '';
    };

  };


  ###### implementation

  config = mkIf (config.powerManagement.cpuFreqGovernor != "") {

    environment.systemPackages = [ pkgs.cpufrequtils ];

    jobs.cpufreq =
      { description = "CPU Frequency Governor Setup";

        after = [ "systemd-modules-load.service" ];
        wantedBy = [ "multi-user.target" ];

        path = [ pkgs.cpufrequtils ];

        preStart = ''
          for i in $(seq 0 $(($(nproc) - 1))); do
            for gov in $(cpufreq-info -c $i -g); do
              if [ "$gov" = ${config.powerManagement.cpuFreqGovernor} ]; then
                echo "<6>setting governor on CPU $i to ‘$gov’"
                cpufreq-set -c $i -g $gov
              fi
            done
          done
        '';
      };
  };

}