All you really need is this one line somewhere in your Sway config:
exec swayidle idlehint 1
You need to log out and log back in after adding this line since it is exec
(instead of exec_always
).
The two lines after this one in the community repo are for triggering swaylock before it goes to suspend, in case you want to set that up. Also, if you still have the default “suspend” script in your Sway config (timeout 600 'swaymsg "output * dpms off"'
, etc, etc), you can comment it out or delete it if you want.
After sending an idlehint
event, systemd will take over suspend events automatically using the configuration specified in /etc/systemd/logind.conf
. However, the default logind.conf
configuration doesn’t actually do anything by default for an idle config; the defaults are like this:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file (or a copy of it placed in
# /etc/ if the original file is shipped in /usr/), or by creating "drop-ins" in
# the /etc/systemd/logind.conf.d/ directory. The latter is generally
# recommended. Defaults can be restored by simply deleting the main
# configuration file and all drop-ins located in /etc/.
#
# Use 'systemd-analyze cat-config systemd/logind.conf' to display the full config.
#
# See logind.conf(5) for details.
[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
#UserStopDelaySec=10
#SleepOperation=suspend-then-hibernate suspend
#HandlePowerKey=poweroff
#HandlePowerKeyLongPress=ignore
#HandleRebootKey=reboot
#HandleRebootKeyLongPress=poweroff
#HandleSuspendKey=suspend
#HandleSuspendKeyLongPress=hibernate
#HandleHibernateKey=hibernate
#HandleHibernateKeyLongPress=ignore
#HandleLidSwitch=suspend
#HandleLidSwitchExternalPower=suspend
#HandleLidSwitchDocked=ignore
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#RebootKeyIgnoreInhibited=no
#HoldoffTimeoutSec=30s
#IdleAction=ignore
#IdleActionSec=30min
#RuntimeDirectorySize=10%
#RuntimeDirectoryInodesMax=
#RemoveIPC=yes
#InhibitorsMax=8192
#SessionsMax=8192
#StopIdleSessionSec=infinity
All those commented lines define the default behavior. You can either uncomment a line and change the value it is set to (easy method), or leave this file alone altogether and create a drop-in file in /etc/systemd/logind.conf.d/
with whatever settings you want to override (recommended method).
Notice these two lines in the default config:
#IdleAction=ignore
#IdleActionSec=30min
The default idle action is triggered after thirty minutes of idle time, but the idle action is actually set to “ignore” by default (so after thirty minutes, nothing happens). To get it to actually do something after the idle time, you need to set it to one of the other supported actions (see man logind.conf
for the full list of possible settings).
That’s where the other file in the repo comes in. If you want to use that as a starting point, create a new file in /etc/systemd/logind.conf.d/
with an editor, for example nano
.
sudo nano /etc/systemd/logind.conf.d/suspend.conf
It doesn’t have to be called suspend.conf
, it can be called anything you want as long as it ends in “.conf
”.
Add whatever settings from logind.conf
you want to override.
[Login]
IdleAction=suspend
IdleActionSec=10min
This will suspend the computer after 10 minutes of idle time. Don’t forget the bracketed [Login]
line at the top; it won’t work without that.
Save and exit out of the file, then restart the systemd-logind
service to apply the changes.
sudo systemctl restart systemd-logind
After ten minutes, the computer should go to suspend.
If you want to set IdleActionSec=1min
to test (so you don’t have to wait ten minutes to see if it works or not), that’s fine but just make sure you restart the systemd-logind
service again after every time you make an edit to the service files to apply the changes.