4 cfg = config.powerManagement;
16 enable = lib.mkOption {
17 type = lib.types.bool;
20 Whether to enable power management. This includes support
21 for suspend-to-RAM and powersave features on laptops.
25 resumeCommands = lib.mkOption {
26 type = lib.types.lines;
28 description = "Commands executed after the system resumes from suspend-to-RAM.";
31 powerUpCommands = lib.mkOption {
32 type = lib.types.lines;
34 example = lib.literalExpression ''
35 "''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda"
38 Commands executed when the machine powers up. That is,
39 they're executed both when the system first boots and when
40 it resumes from suspend or hibernation.
44 powerDownCommands = lib.mkOption {
45 type = lib.types.lines;
47 example = lib.literalExpression ''
48 "''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda"
51 Commands executed when the machine powers down. That is,
52 they're executed both when the system shuts down and when
53 it goes to suspend or hibernation.
64 config = lib.mkIf cfg.enable {
66 systemd.targets.post-resume = {
67 description = "Post-Resume Actions";
68 requires = [ "post-resume.service" ];
69 after = [ "post-resume.service" ];
70 wantedBy = [ "sleep.target" ];
71 unitConfig.StopWhenUnneeded = true;
74 # Service executed before suspending/hibernating.
75 systemd.services.pre-sleep =
76 { description = "Pre-Sleep Actions";
77 wantedBy = [ "sleep.target" ];
78 before = [ "sleep.target" ];
81 ${cfg.powerDownCommands}
83 serviceConfig.Type = "oneshot";
86 systemd.services.post-resume =
87 { description = "Post-Resume Actions";
88 after = [ "suspend.target" "hibernate.target" "hybrid-sleep.target" "suspend-then-hibernate.target" ];
91 /run/current-system/systemd/bin/systemctl try-restart --no-block post-resume.target
93 ${cfg.powerUpCommands}
95 serviceConfig.Type = "oneshot";