nixos-config/modules/common/energy.nix

43 lines
1.4 KiB
Nix
Raw Normal View History

2025-10-13 13:44:38 +02:00
{ config, pkgs, ... }: {
2025-09-04 10:21:17 +02:00
services.logind.settings.Login = {
IdleAction = "suspend";
IdleActionSec = "5min";
HandleLidSwitch = "suspend";
HandleLidSwitchDocked = "ignore";
};
2025-10-13 13:44:38 +02:00
systemd.services.ssh-suspend-inhibitor = {
description = "Prevent suspend when SSH sessions are active";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
Restart = "always";
ExecStart = pkgs.writeShellScript "ssh-inhibitor" ''
while true; do
# Vérifie s'il y a des sessions SSH actives
if ${pkgs.procps}/bin/pgrep -x sshd >/dev/null && \
[ $(${pkgs.procps}/bin/pgrep -P $(${pkgs.procps}/bin/pgrep -x sshd) | wc -l) -gt 0 ]; then
# Il y a des sessions SSH, on crée un inhibitor s'il n'existe pas
if [ ! -f /tmp/ssh-inhibitor.lock ]; then
${pkgs.systemd}/bin/systemd-inhibit --what=idle:sleep \
--who="SSH Session" \
--why="SSH session active" \
--mode=block \
sleep infinity &
echo $! > /tmp/ssh-inhibitor.lock
fi
else
# Pas de sessions SSH, on retire l'inhibitor
if [ -f /tmp/ssh-inhibitor.lock ]; then
kill $(cat /tmp/ssh-inhibitor.lock) 2>/dev/null || true
rm /tmp/ssh-inhibitor.lock
fi
fi
sleep 10
done
'';
};
};
2025-09-04 10:21:17 +02:00
}