From 259c75204d29d82ef0c9697c07d14c324f64591b Mon Sep 17 00:00:00 2001 From: alejandro-angulo Date: Sat, 21 Feb 2026 08:07:34 -0800 Subject: [PATCH] Add ability to inhibit sleep when plugged in --- .../x86_64-linux/alejandro@carbon/default.nix | 2 +- modules/home/services/hypridle/default.nix | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/homes/x86_64-linux/alejandro@carbon/default.nix b/homes/x86_64-linux/alejandro@carbon/default.nix index 91972ed..1356707 100644 --- a/homes/x86_64-linux/alejandro@carbon/default.nix +++ b/homes/x86_64-linux/alejandro@carbon/default.nix @@ -4,7 +4,6 @@ let internal_display_settings = "eDP-1,preferred,auto,1.6"; clamshell_script = pkgs.writeShellScriptBin "clamshell" '' if ${pkgs.hyprland}/bin/hyprctl monitors | ${pkgs.ripgrep}/bin/rg -q '\sDP-'; then - echo "Detected external monitor..." if [[ "$1" == "open" ]]; then ${pkgs.hyprland}/bin/hyprctl keyword monitor ${internal_display_settings} else @@ -25,6 +24,7 @@ in ",preferred,auto,1" ]; }; + aa.services.hypridle.suspendInhibitWhenPluggedIn = true; aa.windowManagers.sway.enable = lib.mkForce false; wayland.windowManager.hyprland.settings.bindl = [ diff --git a/modules/home/services/hypridle/default.nix b/modules/home/services/hypridle/default.nix index 50e792d..6e7dfe9 100644 --- a/modules/home/services/hypridle/default.nix +++ b/modules/home/services/hypridle/default.nix @@ -14,6 +14,18 @@ let ; cfg = config.${namespace}.services.hypridle; + + # Script that suspends only when on battery power. + # When plugged in, uses systemd-inhibit to block suspend. + suspendScript = pkgs.writeShellScript "hypridle-suspend" '' + if [ "$(${pkgs.coreutils}/bin/cat /sys/class/power_supply/AC/online)" = "1" ]; then + # Plugged in - inhibit suspend + ${pkgs.systemd}/bin/systemd-inhibit --what=sleep --who=hypridle --why="AC power connected" --mode=block ${pkgs.coreutils}/bin/sleep infinity & + else + # On battery - suspend + ${pkgs.systemd}/bin/systemctl suspend + fi + ''; in { options.${namespace}.services.hypridle = { @@ -49,6 +61,14 @@ in Whether or not to automatically suspend ''; }; + suspendInhibitWhenPluggedIn = mkOption { + type = types.bool; + default = false; + description = '' + Whether to inhibit suspend when AC power is connected. + Useful for laptops that should only suspend on battery. + ''; + }; brightnessTimeout = mkOption { type = types.int; @@ -108,7 +128,11 @@ in # Suspend system (lib.mkIf cfg.suspendEnable { timeout = cfg.suspendTimeout; - on-timeout = "systemctl suspend"; + on-timeout = + if cfg.suspendInhibitWhenPluggedIn then + "${suspendScript}" + else + "${pkgs.systemd}/bin/systemctl suspend"; }) ]; };