#!/usr/bin/env bash # Enable Sunshine as a systemd --user service and turn on lingering so it # runs at boot without a graphical login. On Ubuntu installs that use the # Sway-headless capture path, also installs + enables sway-headless.service # and wires sunshine.service to depend on it. ensure_sunshine_unit_present() { # Case 1: a sunshine.service unit already exists in any path systemd-user # scans. The LizardByte .deb on Ubuntu drops it at /lib/systemd/user/. # sunshine-bin on Arch drops it at /usr/lib/systemd/user/. for p in \ /lib/systemd/user/sunshine.service \ /usr/lib/systemd/user/sunshine.service \ /etc/systemd/user/sunshine.service \ "$HOME/.config/systemd/user/sunshine.service" \ "$HOME/.local/share/systemd/user/sunshine.service" do [[ -e "$p" ]] && return 0 done # Case 2: the AUR source 'sunshine' package ships the unit under a # Flatpak-style reverse-DNS name. Symlink it as sunshine.service so the rest # of our tooling can keep using the short name. local fqdn_unit="" for p in \ /usr/lib/systemd/user/app-dev.lizardbyte.app.Sunshine.service \ /lib/systemd/user/app-dev.lizardbyte.app.Sunshine.service \ /etc/systemd/user/app-dev.lizardbyte.app.Sunshine.service do [[ -f "$p" ]] && { fqdn_unit="$p"; break; } done if [[ -n "$fqdn_unit" ]]; then info "Found packaged unit at $fqdn_unit" info "Aliasing it as sunshine.service in $HOME/.config/systemd/user/" mkdir -p "$HOME/.config/systemd/user" ln -sf "$fqdn_unit" "$HOME/.config/systemd/user/sunshine.service" return 0 fi # Case 3: no unit shipped at all — drop our own. local fallback="$SCRIPT_DIR/files/sunshine.service" if [[ ! -f "$fallback" ]]; then err "No sunshine.service shipped by the package, and no fallback found at $fallback" return 1 fi info "No packaged sunshine.service found; installing repo fallback unit" mkdir -p "$HOME/.config/systemd/user" install -m 0644 "$fallback" "$HOME/.config/systemd/user/sunshine.service" ok "Installed $HOME/.config/systemd/user/sunshine.service" } # Install and enable the headless sway compositor unit + config (Debian/Ubuntu # headless path only). sunshine.service gets a drop-in making it depend on # sway-headless.service so the wlr capture has something to talk to. ensure_sway_headless_unit() { [[ "$DISTRO" == "debian" ]] || return 0 [[ "${STREAM_MODE:-}" == "headless" ]] || return 0 [[ "${COMPOSITOR:-}" == "sway" ]] || return 0 local cfg_src="$SCRIPT_DIR/files/sway-headless.config" local svc_src="$SCRIPT_DIR/files/sway-headless.service" if [[ ! -f "$cfg_src" || ! -f "$svc_src" ]]; then err "Missing sway-headless source files in $SCRIPT_DIR/files/" return 1 fi mkdir -p "$HOME/.config/sway" "$HOME/.config/systemd/user" install -m 0644 "$cfg_src" "$HOME/.config/sway/config-headless" install -m 0644 "$svc_src" "$HOME/.config/systemd/user/sway-headless.service" # Wire sunshine.service to wait for sway-headless.service. Done via a # drop-in so we don't overwrite the upstream unit shipped by the .deb. local sun_dropin_dir="$HOME/.config/systemd/user/sunshine.service.d" mkdir -p "$sun_dropin_dir" cat >"$sun_dropin_dir/sway-headless.conf" <<'EOF' # Installed by omarchy-moonlight. Sunshine's wlr capture needs a running # wlroots compositor; sway-headless provides one on headless servers. [Unit] After=sway-headless.service Requires=sway-headless.service [Service] # Inherit the sway IPC socket location so hooks can talk to swaymsg. Environment=XDG_SESSION_TYPE=wayland Environment=WAYLAND_DISPLAY=wayland-1 EOF systemctl --user daemon-reload systemctl --user enable sway-headless.service >/dev/null if ! systemctl --user is-active --quiet sway-headless.service; then info "Starting sway-headless.service" systemctl --user restart sway-headless.service || { err "sway-headless.service failed to start. Inspect: journalctl --user -u sway-headless" return 1 } # Give sway a beat to create its IPC socket. sleep 1 fi ok "sway-headless.service active" } enable_sunshine_service() { ensure_sunshine_unit_present systemctl --user daemon-reload # If we're on the Debian+Sway headless path, install the sway-headless unit # before sunshine so the dependency chain is satisfied when we start it. ensure_sway_headless_unit # In headless mode, install a drop-in that pre-creates HEADLESS-1 before # Sunshine starts. Done here because the drop-in target name depends on # which unit ensure_sunshine_unit_present resolved. if [[ "${STREAM_MODE:-}" == "headless" ]] && declare -F install_headless_prestart_dropin >/dev/null; then install_headless_prestart_dropin fi if ! systemctl --user list-unit-files sunshine.service >/dev/null 2>&1; then err "sunshine.service still not found after fallback. Inspect: find /usr /lib ~/.config -name sunshine.service" return 1 fi if ! loginctl show-user "$USER" -p Linger --value 2>/dev/null | grep -qx yes; then info "Enabling user lingering (loginctl enable-linger $USER)" as_root loginctl enable-linger "$USER" else ok "User lingering already enabled" fi info "Enabling sunshine.service (user)" systemctl --user enable sunshine.service >/dev/null # Clear any prior start-limit state from a failed run so this attempt isn't # immediately rejected with "Start request repeated too quickly." systemctl --user reset-failed sunshine.service 2>/dev/null || true info "Starting sunshine.service (user)" systemctl --user restart sunshine.service || systemctl --user start sunshine.service || { err "Failed to start sunshine.service. Check: journalctl --user -u sunshine" return 1 } sleep 1 if systemctl --user is-active --quiet sunshine.service; then ok "sunshine.service is active" else warn "sunshine.service did not stay active. Inspect: journalctl --user -u sunshine -n 50" fi }