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

@ -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";
})
];
};