summary refs log tree commit diff
path: root/nixos/modules/tasks/cpu-freq.nix
blob: 5f8b5df52acf03979ee450cb93342cc2944b6ffb (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
52
{ config, lib, pkgs, ... }:

with lib;

let
  cpupower = config.boot.kernelPackages.cpupower;
  cfg = config.powerManagement;
in

{
  ###### interface

  options = {

    powerManagement.cpuFreqGovernor = 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.
      '';
    };

  };


  ###### implementation

  config = mkIf (!config.boot.isContainer && config.powerManagement.cpuFreqGovernor != null) {

    boot.kernelModules = [ "cpufreq_${cfg.cpuFreqGovernor}" ];

    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";
      };
    };

  };
}