From eeee8333b5e302683f26dcb20db2afee3652b18c Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Thu, 24 Sep 2026 16:24:10 -0400 Subject: [PATCH] feat(systemd): auto-start tmux main session at login and add dotfiles restore unit --- .../user/default.target.wants/tmux-main.service | 1 + .../systemd/user/home-dotfiles-restore.service | 6 ++++++ config/systemd/user/tmux-main.service | 15 +++++++++++++++ script/tmux-start.sh | 17 +++++++++++++++++ 4 files changed, 39 insertions(+) create mode 120000 config/systemd/user/default.target.wants/tmux-main.service create mode 100644 config/systemd/user/home-dotfiles-restore.service create mode 100644 config/systemd/user/tmux-main.service create mode 100755 script/tmux-start.sh diff --git a/config/systemd/user/default.target.wants/tmux-main.service b/config/systemd/user/default.target.wants/tmux-main.service new file mode 120000 index 0000000..0fbf2df --- /dev/null +++ b/config/systemd/user/default.target.wants/tmux-main.service @@ -0,0 +1 @@ +../tmux-main.service \ No newline at end of file diff --git a/config/systemd/user/home-dotfiles-restore.service b/config/systemd/user/home-dotfiles-restore.service new file mode 100644 index 0000000..7d3b500 --- /dev/null +++ b/config/systemd/user/home-dotfiles-restore.service @@ -0,0 +1,6 @@ +[Unit] +Description=Restore dotfiles into home directory + +[Service] +Type=oneshot +ExecStart=/usr/bin/stow --dotfiles --dir=/home/sjb/.local/share/dotfiles --restow packages apps config diff --git a/config/systemd/user/tmux-main.service b/config/systemd/user/tmux-main.service new file mode 100644 index 0000000..3dbfa44 --- /dev/null +++ b/config/systemd/user/tmux-main.service @@ -0,0 +1,15 @@ +[Unit] +Description=Ensure tmux session "main" is running +After=graphical-session.target +PartOf=graphical-session.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=%h/.local/share/dotfiles/script/tmux-start.sh +# If the session check fails (no server / lost session), retry until login ends. +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=default.target diff --git a/script/tmux-start.sh b/script/tmux-start.sh new file mode 100755 index 0000000..46ef07f --- /dev/null +++ b/script/tmux-start.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Start (or reuse) the tmux server with a session named "main". +# Used by the tmux-main systemd user service at graphical login. +# +# Designed to survive Restart=on-failure: if a session already exists this +# script reattaches nothing and exits 0; if the server dies systemd restarts +# us and we recreate the session. +set -u + +session=main + +if ! tmux ls 2>/dev/null | grep -q "^${session}:"; then + tmux new-session -d -s "$session" +fi + +# Keep the unit "active" only briefly; the session lives in the tmux server. +exit 0