sleep-then-hibernate on an Arch Laptop in combination with swayidle
The system is designed to turn off the display within 30 sec, then within another 10 sec it locks with swaylock. In 5 minutes time of total delay (so in 4m20s) it should put the system to sleep. And within another 5 minutes the system must hibernate. That way I can achieve MacBook-like UX.
Let’s create a drop-in configuration for systemd-logind
to handle lid close events and integrate it with the swayidle
configuration.
Step 1: Create a Drop-in Configuration for Logind
Create the directory for the drop-in configuration:
sudo mkdir -p /etc/systemd/logind.conf.d
Create a new drop-in file:
sudo nano /etc/systemd/logind.conf.d/10-lid-switch.conf
Add the following content to the drop-in file:
[Login] HandleLidSwitch=suspend-then-hibernate HandleLidSwitchDocked=suspend-then-hibernate HandleLidSwitchExternalPower=suspend-then-hibernate HandleLidSwitchBattery=suspend-then-hibernate
Save and Close the File:
Save the file (
Ctrl + O
, thenEnter
in nano) and exit (Ctrl + X
).Restart systemd-logind Service:
sudo systemctl restart systemd-logind
Step 2: Update swayidle Configuration
Ensure your swayidle
configuration works in harmony with the systemd-logind
settings. Your sway configuration should look something like this:
Open Your Sway Configuration File:
nano ~/.config/sway/config
Add the
swayidle
configuration:exec swayidle -w \ timeout 300 'swaymsg "output * dpms off"' \ timeout 305 'systemctl suspend-then-hibernate' \ resume 'swaymsg "output * dpms on"' \ before-sleep 'swaylock'
This configuration means:
timeout 300
: After 300 seconds (5 minutes) of inactivity, turn off the display (dpms off
).timeout 305
: After 305 seconds (5 minutes and 5 seconds) of inactivity, put the system into suspend-then-hibernate (systemctl suspend-then-hibernate
).resume
: When resuming from suspend or hibernate, turn the display back on (dpms on
).before-sleep
: Before going to sleep, lock the screen withswaylock
.
Save and Apply Changes:
Save the configuration file (
Ctrl + O
, thenEnter
in nano) and exit (Ctrl + X
). Then, reload Sway to apply the changes:swaymsg reload
Step 3: Create Drop-in Configuration for Sleep Settings
To handle the hibernation delay, create a drop-in configuration for sleep.conf
.
Create the directory for the drop-in configuration:
sudo mkdir -p /etc/systemd/sleep.conf.d
Create a new drop-in file:
sudo nano /etc/systemd/sleep.conf.d/10-hibernate.conf
Add the following content to the drop-in file:
[Sleep] HibernateDelaySec=300
Save and Close the File:
Save the file (
Ctrl + O
, thenEnter
in nano) and exit (Ctrl + X
).
Summary
Lid Close Event: When you close the lid,
systemd-logind
will triggersuspend-then-hibernate
due to the drop-in configuration in/etc/systemd/logind.conf.d/10-lid-switch.conf
.Swayidle Timer: Independently of the lid close,
swayidle
will monitor inactivity and triggersystemctl suspend-then-hibernate
after the specified timeouts.Hibernate Delay: The delay for hibernation after suspend is configured in the drop-in file
/etc/systemd/sleep.conf.d/10-hibernate.conf
.
By using drop-in files, you ensure that your custom configurations are cleanly separated from the default systemd configurations, making them easier to manage and maintain.