#!/usr/bin/env bash # Set up headless streaming mode: install prep-cmd hook scripts into a stable # location and ensure they're executable. The actual sunshine.conf entries # (capture=wlr, output_name=HEADLESS-1, global_prep_cmd=[...]) are written # by lib/config.sh. # # Two compositor backends supported: # - hyprland (default on Omarchy/Arch): bin/sunshine-stream-{do,undo}.sh # - sway (default on Debian/Ubuntu): bin/sunshine-stream-{do,undo}-sway.sh # detect_compositor (lib/detect.sh) decides which to install. The script names # at the install target are *always* sunshine-stream-do.sh / -undo.sh, so the # rest of the installer (config.sh, verify.sh) doesn't have to branch. HEADLESS_BIN_DIR="$HOME/.local/share/omarchy-moonlight/bin" DO_SCRIPT="$HEADLESS_BIN_DIR/sunshine-stream-do.sh" UNDO_SCRIPT="$HEADLESS_BIN_DIR/sunshine-stream-undo.sh" PRESTART_SCRIPT="$HEADLESS_BIN_DIR/sunshine-prestart.sh" export DO_SCRIPT UNDO_SCRIPT PRESTART_SCRIPT # Resolve which hook source files to install based on the detected compositor. _headless_hook_sources() { case "${COMPOSITOR:-hyprland}" in sway) echo "$SCRIPT_DIR/bin/sunshine-stream-do-sway.sh" echo "$SCRIPT_DIR/bin/sunshine-stream-undo-sway.sh" echo "$SCRIPT_DIR/bin/sunshine-prestart-sway.sh" ;; hyprland|*) echo "$SCRIPT_DIR/bin/sunshine-stream-do.sh" echo "$SCRIPT_DIR/bin/sunshine-stream-undo.sh" echo "$SCRIPT_DIR/bin/sunshine-prestart.sh" ;; esac } # The X11/NVENC headless backend (capture = x11, headless Xorg on :0) isn't # selected by --headless/--mirror — those pick the *Wayland* capture method. # It's identified by an existing `capture = x11` in sunshine.conf, the # hand-built path documented in FOLLOWUPS.md P3. When that backend is in play, # the headless Xorg needs a window manager rendering on :0 or Sunshine # captures a black screen. capture_backend_is_x11() { local conf="$HOME/.config/sunshine/sunshine.conf" [[ -f "$conf" ]] || return 1 grep -qE '^[[:space:]]*capture[[:space:]]*=[[:space:]]*x11' "$conf" } HEADLESS_DESKTOP_UNIT="headless-desktop.service" # Which desktop to render on the headless Xorg :0. Default is a full GNOME # session (the familiar Ubuntu desktop). Set HEADLESS_DESKTOP=openbox for a # lightweight bare WM instead — lower overhead, better for a truly minimal or # low-power host, but no panel/launcher out of the box (right-click menu only). : "${HEADLESS_DESKTOP:=gnome}" # Install the packages the chosen desktop needs. Idempotent — pkg_install # skips what's already present. ensure_headless_desktop_packages() { case "$HEADLESS_DESKTOP" in gnome) case "$DISTRO" in debian) pkg_install gnome-session gnome-shell ubuntu-session ;; arch) pkg_install gnome-shell gnome-session ;; esac ;; openbox) command -v openbox-session >/dev/null 2>&1 || pkg_install openbox # xsetroot paints a solid root so the bare desktop is visibly non-black. if ! command -v xsetroot >/dev/null 2>&1; then case "$DISTRO" in debian) pkg_install x11-xserver-utils ;; arch) pkg_install xorg-xsetroot ;; esac fi ;; *) err "Unknown HEADLESS_DESKTOP='$HEADLESS_DESKTOP' (expected: gnome | openbox)" return 1 ;; esac } # Install + enable a desktop session that renders onto the headless Xorg # display (:0), so the X11/NVENC capture path shows a real desktop instead of a # black root window. Picks the GNOME or Openbox unit template per # $HEADLESS_DESKTOP. Idempotent: safe to re-run. Bound to xorg-headless.service # and to default.target (the lingering-user manager reaches it without a login). install_headless_desktop() { local src="$SCRIPT_DIR/files/headless-desktop-${HEADLESS_DESKTOP}.service" if [[ ! -f "$src" ]]; then err "Desktop unit source missing: $src (HEADLESS_DESKTOP=$HEADLESS_DESKTOP)" return 1 fi ensure_headless_desktop_packages || return 1 local unit_dir="$HOME/.config/systemd/user" mkdir -p "$unit_dir" install -m 0644 "$src" "$unit_dir/$HEADLESS_DESKTOP_UNIT" systemctl --user daemon-reload # enable --now starts it immediately when a user manager is live; on a fresh # headless box with no session yet, fall back to enable-only so it comes up # on next boot via default.target. if systemctl --user enable --now "$HEADLESS_DESKTOP_UNIT" >/dev/null 2>&1; then ok "Enabled $HEADLESS_DESKTOP_UNIT ($HEADLESS_DESKTOP session on :0)" else systemctl --user enable "$HEADLESS_DESKTOP_UNIT" >/dev/null 2>&1 || true warn "Installed $HEADLESS_DESKTOP_UNIT but couldn't start it now — it will start on next login/boot." fi } install_headless_hooks() { mkdir -p "$HEADLESS_BIN_DIR" mapfile -t srcs < <(_headless_hook_sources) local do_src="${srcs[0]}" undo_src="${srcs[1]}" pre_src="${srcs[2]}" install -m 0755 "$do_src" "$DO_SCRIPT" install -m 0755 "$undo_src" "$UNDO_SCRIPT" install -m 0755 "$pre_src" "$PRESTART_SCRIPT" ok "Installed prep-cmd + prestart hooks ($COMPOSITOR) to $HEADLESS_BIN_DIR" } # Install a systemd-user drop-in that pre-creates HEADLESS-1 before Sunshine # starts. Without this, Sunshine reports a fatal "Unable to find display or # encoder during startup" on every restart, even though streaming works once # a client connects. install_headless_prestart_dropin() { local dropin_src="$SCRIPT_DIR/files/headless-prestart.conf" if [[ ! -f "$dropin_src" ]]; then err "Drop-in source missing: $dropin_src" return 1 fi # Resolve the actual unit name. Prefer sunshine.service when present (alias, # sunshine-bin, or the .deb on Ubuntu); fall back to the AUR source pkg's # reverse-DNS name. local unit="" for u in sunshine.service app-dev.lizardbyte.app.Sunshine.service; do if systemctl --user list-unit-files "$u" >/dev/null 2>&1 \ && systemctl --user cat "$u" >/dev/null 2>&1; then unit="$u" break fi done if [[ -z "$unit" ]]; then warn "Could not resolve a sunshine unit to attach the drop-in to; skipping." return 0 fi local dropin_dir="$HOME/.config/systemd/user/${unit}.d" mkdir -p "$dropin_dir" install -m 0644 "$dropin_src" "$dropin_dir/headless-prestart.conf" systemctl --user daemon-reload ok "Installed headless prestart drop-in at $dropin_dir/headless-prestart.conf" }