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