summary refs log tree commit diff
path: root/modules/config/power-management.nix
blob: cb7047a2ba80ab55d7ff86d756ad8cc92cd8d60b (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.powerManagement;

  sleepHook = pkgs.writeScript "sleep-hook.sh"
    ''
      #! ${pkgs.stdenv.shell}
      action="$1"
      case "$action" in
          hibernate|suspend)
              ${cfg.powerDownCommands}
              ;;
          thaw|resume)
              ${cfg.resumeCommands}
              ${cfg.powerUpCommands}
              ;;
      esac
    '';

in

{

  ###### interface

  options = {

    powerManagement = {

      enable = mkOption {
        default = false;
        description =
          ''
            Whether to enable power management.  This includes support
            for suspend-to-RAM and powersave features on laptops.
          '';
      };

      resumeCommands = mkOption {
        default = "";
        description = "Commands executed after the system resumes from suspend-to-RAM.";
      };

      powerUpCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers up.  That is,
            they're executed both when the system first boots and when
            it resumes from suspend or hibernation.
          '';
      };

      powerDownCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers down.  That is,
            they're executed both when the system shuts down and when
            it goes to suspend or hibernation.
          '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    # Enable the ACPI daemon.  Not sure whether this is essential.
    services.acpid.enable = true;

    environment.systemPackages = [ pkgs.pmutils ];

    environment.etc = singleton
      { source = sleepHook;
        target = "pm/sleep.d/00sleep-hook";
      };

    boot.kernelModules =
      [ "acpi_cpufreq" "cpufreq_performance" "cpufreq_powersave" "cpufreq_ondemand"
        "p4_clockmod"
      ];

  };

}