#!/bin/bash set -euo pipefail # WSL Systemd Fix Script # Run this once per WSL restart to enable systemd user sessions # Detect if running on WSL if [[ ! -f /proc/version ]] || ! grep -qi microsoft /proc/version; then echo "Not running on WSL - nothing to do" exit 0 fi USER_ID=$(id -u) USER_NAME=$(id -un) USER_GROUP=$(id -gn) RUNTIME_DIR="/run/user/${USER_ID}" echo "=== WSL Systemd User Session Setup ===" echo "User: ${USER_NAME} (UID: ${USER_ID})" echo "Runtime directory: ${RUNTIME_DIR}" echo "" # Check if already set up if [[ -d "${RUNTIME_DIR}" ]] && [[ -S "${RUNTIME_DIR}/bus" ]]; then echo "Runtime directory already exists and dbus socket is present." echo "Setting environment variables..." export XDG_RUNTIME_DIR="${RUNTIME_DIR}" export DBUS_SESSION_BUS_ADDRESS="unix:path=${RUNTIME_DIR}/bus" echo "" echo "Environment set. You can now use: make status, make backup-now, etc." exit 0 fi # Create runtime directory (requires sudo) if [[ ! -d "${RUNTIME_DIR}" ]]; then echo "Creating runtime directory (requires sudo)..." sudo mkdir -p "${RUNTIME_DIR}" sudo chmod 700 "${RUNTIME_DIR}" sudo chown "${USER_NAME}:${USER_GROUP}" "${RUNTIME_DIR}" echo "✓ Runtime directory created" fi # Start user systemd service (requires sudo) echo "Starting systemd user session (requires sudo)..." sudo systemctl start "user@${USER_ID}.service" 2>/dev/null || true echo "✓ Systemd user session started" # Wait a moment for dbus socket to be created echo "Waiting for dbus socket..." for i in {1..10}; do if [[ -S "${RUNTIME_DIR}/bus" ]]; then break fi sleep 0.5 done if [[ ! -S "${RUNTIME_DIR}/bus" ]]; then echo "Warning: dbus socket not found, systemd may not be fully initialized" fi # Set environment variables export XDG_RUNTIME_DIR="${RUNTIME_DIR}" export DBUS_SESSION_BUS_ADDRESS="unix:path=${RUNTIME_DIR}/bus" # Reload systemd daemon systemctl --user daemon-reexec 2>/dev/null || true echo "" echo "=== Setup Complete ===" echo "Environment variables set for current session:" echo " XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR}" echo " DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}" echo "" echo "You can now use: make status, make backup-now, make check-now, etc." echo "" echo "Note: Run 'make fix-wsl' again after each WSL restart."