main systemd.sh
Eric Bower  ·  2026-09-02
 1#!/usr/bin/env bash
 2set -euo pipefail
 3
 4# ==============================================================================
 5# user units -> enabled and started now
 6# ==============================================================================
 7USER_UNITS=(
 8  # mailsync.timer
 9  # imapnotify.service
10  # vdirsyncer.timer
11)
12
13# ==============================================================================
14# wayland session units -> enabled only (started with wayland-session.target)
15# ==============================================================================
16WAYLAND_UNITS=(
17  kanshi.service
18  mako.service
19  swaybg.service
20  swayidle.service
21  swaylock.service
22  wlsunset.service
23  figbar.service
24)
25
26SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
27DOTFILES_UNITS_DIR="$SCRIPT_DIR/dot_config/systemd/user"
28
29is_tracked() {
30  local item="$1"
31  shift
32  for elem in "$@"; do
33    [[ "$elem" == "$item" ]] && return 0
34  done
35  return 1
36}
37
38case "${1:-enable}" in
39enable|link|up)
40  echo "==> Step 1: Reloading systemd user daemon"
41  systemctl --user daemon-reload
42
43  # Auto-disable any dotfile units that were removed/commented out from the arrays
44  echo "==> Step 2: Pruning untracked dotfile units"
45  if [[ -d "$DOTFILES_UNITS_DIR" ]]; then
46    for file in "$DOTFILES_UNITS_DIR"/*; do
47      [[ -f "$file" ]] || continue
48      unit="$(basename "$file")"
49      [[ "$unit" == *.target ]] && continue
50
51      if ! is_tracked "$unit" "${USER_UNITS[@]}" "${WAYLAND_UNITS[@]}"; then
52        if systemctl --user is-enabled "$unit" &>/dev/null; then
53          echo "  [PRUNE] $unit is enabled but not in tracked list -> disabling and stopping"
54          systemctl --user disable --now "$unit" 2>/dev/null || true
55        fi
56      fi
57    done
58  fi
59
60  echo "==> Step 3: Enabling and starting user services & timers"
61  for unit in "${USER_UNITS[@]}"; do
62    echo "  [USER] systemctl --user enable --now $unit"
63    systemctl --user enable --now "$unit" || echo "  [WARN] Failed to start $unit" >&2
64  done
65
66  echo "==> Step 4: Enabling Wayland session services"
67  for unit in "${WAYLAND_UNITS[@]}"; do
68    echo "  [WAYLAND] systemctl --user enable $unit"
69    systemctl --user enable "$unit" 2>/dev/null || true
70  done
71  ;;
72
73disable|stop|down)
74  echo "==> Disabling and stopping all dotfile units"
75  for unit in "${USER_UNITS[@]}" "${WAYLAND_UNITS[@]}"; do
76    echo "  Disabling: $unit"
77    systemctl --user disable --now "$unit" 2>/dev/null || true
78  done
79
80  echo "==> Reloading systemd user daemon"
81  systemctl --user daemon-reload
82  systemctl --user reset-failed 2>/dev/null || true
83  ;;
84
85*)
86  echo "Usage: $0 [enable|disable]" >&2
87  exit 1
88  ;;
89esac