Add ability to inhibit sleep when plugged in
All checks were successful
Buill NixOS Configurations / build (carbon) (push) Successful in 1m21s
Buill NixOS Configurations / build (git) (push) Successful in 1m13s
Buill NixOS Configurations / build (gospel) (push) Successful in 1m28s
Buill NixOS Configurations / build (node) (push) Successful in 1m18s

This commit is contained in:
alejandro-angulo 2026-02-21 08:07:34 -08:00
parent 7ba4796758
commit 259c75204d
2 changed files with 26 additions and 2 deletions

View file

@ -4,7 +4,6 @@ let
internal_display_settings = "eDP-1,preferred,auto,1.6"; internal_display_settings = "eDP-1,preferred,auto,1.6";
clamshell_script = pkgs.writeShellScriptBin "clamshell" '' clamshell_script = pkgs.writeShellScriptBin "clamshell" ''
if ${pkgs.hyprland}/bin/hyprctl monitors | ${pkgs.ripgrep}/bin/rg -q '\sDP-'; then if ${pkgs.hyprland}/bin/hyprctl monitors | ${pkgs.ripgrep}/bin/rg -q '\sDP-'; then
echo "Detected external monitor..."
if [[ "$1" == "open" ]]; then if [[ "$1" == "open" ]]; then
${pkgs.hyprland}/bin/hyprctl keyword monitor ${internal_display_settings} ${pkgs.hyprland}/bin/hyprctl keyword monitor ${internal_display_settings}
else else
@ -25,6 +24,7 @@ in
",preferred,auto,1" ",preferred,auto,1"
]; ];
}; };
aa.services.hypridle.suspendInhibitWhenPluggedIn = true;
aa.windowManagers.sway.enable = lib.mkForce false; aa.windowManagers.sway.enable = lib.mkForce false;
wayland.windowManager.hyprland.settings.bindl = [ wayland.windowManager.hyprland.settings.bindl = [

View file

@ -14,6 +14,18 @@ let
; ;
cfg = config.${namespace}.services.hypridle; 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 in
{ {
options.${namespace}.services.hypridle = { options.${namespace}.services.hypridle = {
@ -49,6 +61,14 @@ in
Whether or not to automatically suspend 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 { brightnessTimeout = mkOption {
type = types.int; type = types.int;
@ -108,7 +128,11 @@ in
# Suspend system # Suspend system
(lib.mkIf cfg.suspendEnable { (lib.mkIf cfg.suspendEnable {
timeout = cfg.suspendTimeout; timeout = cfg.suspendTimeout;
on-timeout = "systemctl suspend"; on-timeout =
if cfg.suspendInhibitWhenPluggedIn then
"${suspendScript}"
else
"${pkgs.systemd}/bin/systemctl suspend";
}) })
]; ];
}; };