If you got linked to the toplevel of this file, something went wrong. The main post is Sway and systemd. This post is intended as technical details and the “why”. The main post has the motivation and for all of this.

SDDM

/etc/sddm.conf.d/autologin.conf is the only config file I have here.

[Autologin]
User=ongy
Session=sway-systemd.desktop

Sway stuff

sway.desktop

This is the .desktop file that should be used for our sway session. It’s placed into /usr/share/wayland-sessions/sway-systemd.desktop

[Desktop Entry]
Name=Sway Service
Comment=Start sway via systemd
Exec=sway-service.sh
Type=Application

It needs a secondary shell script, for me it’s at /usr/local/bin/sway-service.sh

#!/bin/sh

systemctl --user import-environment
exec systemctl --wait --user start sway.service

Services

The individual service files are placed into `$HOME/.config/systemd/user/

swaybg.service

There’s not realy anything interesting to say about this I think

[Unit]
Description=Swaybg to provide the desktop background
Documentation=man:swaybg(1)
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
ExecStart=/usr/bin/swaybg -o * -i /home/ongy/background.png -m fill

[Install]
WantedBy=graphical-session.target

swayidle.service

There’s not realy anything interesting to say about this I think

[Unit]
Description=Swayidle to trigger idle actions
Documentation=man:swayidle(1)
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
ExecStart=/usr/bin/swayidle -w \
	timeout 300 'swaylock -f -c 0000FF' \
	timeout 600 'swaymsg "output * dpms off"' \
	     resume 'swaymsg "output * dpms on"' \
	before-sleep 'swaylock -f -c 0000FF'

[Install]
WantedBy=graphical-session.target

swaybar@.service

This file is slightly more interesting than the others. It needs to know which configuration to start. We use systemd templates for this, and pass in the templated name as --bar_id parameter.

[Unit]
Description=Swaybar to trigger idle actions
Documentation=man:swaybar(1)
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
ExecStart=/usr/bin/swaybar --bar_id %i
Restart=on-failure

[Install]
WantedBy=graphical-session.target

swaybar in sway.config

This is the snippet I use to as swaybar config in my sway config file.

bar {
    position top

    swaybar_command /bin/true
    # When the status_command prints a new line to stdout, swaybar updates.
    # The default just shows the current date and time.
    status_command $HOME/.bin/monky

    colors {
        statusline #ffffff
        background #32323200
        inactive_workspace #fefefe00 #fefefe00 #fefefe
    }
}

sway

We add these two exec statements into our sway configuration file to load sway’s private environment variables into systemd. This allows us to start daemons that depend on them from systemd.

exec systemd --user import-environment
exec systemd-notify --ready

sway.service

The main difference between this, and the version in the sway wiki lies in the Type= field. We use notify here, to delay other services far enough.

We also need NotifyAccess=all, since we just exec the shell tool and don’t patch this into sway.

[Unit]
Description=sway - User service
Documentation=man:sway(5)
BindsTo=graphical-session.target
Wants=graphical-session-pre.target
After=graphical-session-pre.target


[Service]
Type=notify
NotifyAccess=all
EnvironmentFile=-%h/.config/sway/env
ExecStart=/usr/bin/sway
Restart=never
RestartSec=1
TimeoutStopSec=10

Browsers

None of these are particularily impmressive, but I keep them here for backup reasons.

Firefox

[Unit]
Description=Firefox
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
EnvironmentFile=-%h/.config/systemd/user/firefox.env
ExecStart=/usr/bin/firefox
Restart=on-failure

# [Install]
# WantedBy=graphical-session.target

Chromium

[Unit]
Description=Chromium
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
ExecStart=/usr/bin/chromium
Restart=on-failure

# [Install]
# WantedBy=graphical-session.target

qutebrowser

[Unit]
Description=qutebrowser
PartOf=graphical-session.target
After=sway.service

[Service]
Type=simple
ExecStart=/usr/bin/qutebrowser

[Install]
WantedBy=graphical-session.target

Alacritty

This uses another template unit file. The %i here tells alacritty to use the uuid generated by my starter script as the template param as title. There’s no real good reason to do this, but it allows to find them.

[Unit]
Description=Alacritty template session

After=sway.service

[Service]
Type=simple
EnvironmentFile=-%h/.config/sway/env
Environment=WAYLAND_DISPLAY=wayland-0
ExecStart=/usr/bin/alacritty -t %i
Restart=on-failure
RestartSec=1
TimeoutStopSec=10
KillMode=none

This unit is started via systemctl --user start alacrity@$(uuidgen)