at 24.11-pre 2.6 kB view raw
1{ config, lib, ... }: 2 3with lib; 4 5let 6 7 cfg = config.powerManagement; 8 9in 10 11{ 12 13 ###### interface 14 15 options = { 16 17 powerManagement = { 18 19 enable = mkOption { 20 type = types.bool; 21 default = true; 22 description = '' 23 Whether to enable power management. This includes support 24 for suspend-to-RAM and powersave features on laptops. 25 ''; 26 }; 27 28 resumeCommands = mkOption { 29 type = types.lines; 30 default = ""; 31 description = "Commands executed after the system resumes from suspend-to-RAM."; 32 }; 33 34 powerUpCommands = mkOption { 35 type = types.lines; 36 default = ""; 37 example = literalExpression '' 38 "''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda" 39 ''; 40 description = '' 41 Commands executed when the machine powers up. That is, 42 they're executed both when the system first boots and when 43 it resumes from suspend or hibernation. 44 ''; 45 }; 46 47 powerDownCommands = mkOption { 48 type = types.lines; 49 default = ""; 50 example = literalExpression '' 51 "''${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda" 52 ''; 53 description = '' 54 Commands executed when the machine powers down. That is, 55 they're executed both when the system shuts down and when 56 it goes to suspend or hibernation. 57 ''; 58 }; 59 60 }; 61 62 }; 63 64 65 ###### implementation 66 67 config = mkIf cfg.enable { 68 69 systemd.targets.post-resume = { 70 description = "Post-Resume Actions"; 71 requires = [ "post-resume.service" ]; 72 after = [ "post-resume.service" ]; 73 wantedBy = [ "sleep.target" ]; 74 unitConfig.StopWhenUnneeded = true; 75 }; 76 77 # Service executed before suspending/hibernating. 78 systemd.services.pre-sleep = 79 { description = "Pre-Sleep Actions"; 80 wantedBy = [ "sleep.target" ]; 81 before = [ "sleep.target" ]; 82 script = 83 '' 84 ${cfg.powerDownCommands} 85 ''; 86 serviceConfig.Type = "oneshot"; 87 }; 88 89 systemd.services.post-resume = 90 { description = "Post-Resume Actions"; 91 after = [ "suspend.target" "hibernate.target" "hybrid-sleep.target" "suspend-then-hibernate.target" ]; 92 script = 93 '' 94 /run/current-system/systemd/bin/systemctl try-restart --no-block post-resume.target 95 ${cfg.resumeCommands} 96 ${cfg.powerUpCommands} 97 ''; 98 serviceConfig.Type = "oneshot"; 99 }; 100 101 }; 102 103}