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

  1. Create the directory for the drop-in configuration:

    sudo mkdir -p /etc/systemd/logind.conf.d
    
  2. Create a new drop-in file:

    sudo nano /etc/systemd/logind.conf.d/10-lid-switch.conf
    
  3. 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
    
  4. Save and Close the File:

    Save the file (Ctrl + O, then Enter in nano) and exit (Ctrl + X).

  5. 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:

  1. Open Your Sway Configuration File:

    nano ~/.config/sway/config
    
  2. 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 with swaylock.
  3. Save and Apply Changes:

    Save the configuration file (Ctrl + O, then Enter 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.

  1. Create the directory for the drop-in configuration:

    sudo mkdir -p /etc/systemd/sleep.conf.d
    
  2. Create a new drop-in file:

    sudo nano /etc/systemd/sleep.conf.d/10-hibernate.conf
    
  3. Add the following content to the drop-in file:

    [Sleep]
    HibernateDelaySec=300
    
  4. Save and Close the File:

    Save the file (Ctrl + O, then Enter in nano) and exit (Ctrl + X).

Summary

  • Lid Close Event: When you close the lid, systemd-logind will trigger suspend-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 trigger systemctl 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.