#!/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. # Resolves the actual unit file to operate on. Exports: # SUNSHINE_ENABLE_NAME - the name to pass to `systemctl --user enable`. May # be sunshine.service or app-dev.lizardbyte....service # depending on how the package shipped the unit. # SUNSHINE_SERVICE - the short name we use everywhere else (start, restart, # status, drop-ins). Always sunshine.service — systemd # resolves it via Alias= or our installed unit. ensure_sunshine_unit_present() { SUNSHINE_SERVICE="sunshine.service" # Clean up a broken symlink from older runs that pointed sunshine.service # at the FQDN unit. The upstream .deb already declares Alias=sunshine.service # in [Install], so the symlink we used to create conflicts with systemd's # enable path ("Refusing to operate on alias name"). local user_unit="$HOME/.config/systemd/user/sunshine.service" if [[ -L "$user_unit" ]] && readlink "$user_unit" 2>/dev/null \ | grep -q 'app-dev.lizardbyte.app.Sunshine.service$'; then info "Removing stale alias symlink at $user_unit (upstream unit declares Alias=)" rm -f "$user_unit" fi # Case 1: a real sunshine.service unit ships in a system path (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/.local/share/systemd/user/sunshine.service" do if [[ -f "$p" && ! -L "$p" ]]; then SUNSHINE_ENABLE_NAME="sunshine.service" export SUNSHINE_ENABLE_NAME SUNSHINE_SERVICE return 0 fi done # Case 2: the LizardByte .deb (Ubuntu) and the AUR source package ship the # unit under a reverse-DNS FQDN with Alias=sunshine.service in [Install]. # Do NOT symlink — `systemctl --user enable` on the FQDN name creates the # alias symlink itself in ~/.config/systemd/user/. 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 (enables via alias)" SUNSHINE_ENABLE_NAME="app-dev.lizardbyte.app.Sunshine.service" export SUNSHINE_ENABLE_NAME 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" SUNSHINE_ENABLE_NAME="sunshine.service" export SUNSHINE_ENABLE_NAME 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_ENABLE_NAME" >/dev/null 2>&1; then err "$SUNSHINE_ENABLE_NAME not found after fallback. Inspect: find /usr /lib ~/.config -name '*[Ss]unshine*'" 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_ENABLE_NAME} (user)" systemctl --user enable "$SUNSHINE_ENABLE_NAME" >/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 }