summaryrefslogtreecommitdiffstats
path: root/nixos/modules/tasks/powertop.nix
blob: c8a41471217df898aeb75e1a193bad5add5ea4a6 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;

let
  cfg = config.powerManagement.powertop;
in
{
  ###### interface

  options.powerManagement.powertop = {
    enable = mkEnableOption "powertop auto tuning on startup";

    preStart = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Shell commands executed before `powertop` is started.
      '';
    };

    postStart = mkOption {
      type = types.lines;
      default = "";
      example = ''
        ''${lib.getExe' config.systemd.package "udevadm"} trigger -c bind -s usb -a idVendor=046d -a idProduct=c08c
      '';
      description = ''
        Shell commands executed after `powertop` is started.

        This can be used to workaround problematic configurations. For example,
        you can retrigger an `udev` rule to disable power saving on unsupported
        USB devices:
        ```
        services.udev.extraRules = ''''
          # disable USB auto suspend for Logitech, Inc. G PRO Gaming Mouse
          ACTION=="bind", SUBSYSTEM=="usb", ATTR{idVendor}=="046d", ATTR{idProduct}=="c08c", TEST=="power/control", ATTR{power/control}="on"
        '''';
        ```
      '';
    };
  };

  ###### implementation

  config = mkIf (cfg.enable) {
    systemd.services = {
      powertop = {
        documentation = [ "man:powertop(8)" ];
        wantedBy = [ "multi-user.target" ];
        after = [ "multi-user.target" ];
        description = "Powertop tunings";
        path = [ pkgs.kmod ];
        preStart = cfg.preStart;
        postStart = cfg.postStart;
        serviceConfig = {
          Type = "oneshot";
          RemainAfterExit = "yes";
          ExecStart = "${pkgs.powertop}/bin/powertop --auto-tune";
        };
      };
    };
  };
}