# Nix-Vibe task runner (see AGENTS.md for the underlying workflows)

set shell := ["zsh", "-c"]

# Apply a host's NixOS config, then relaunch QuickShell so it loads the freshly
# built shell.qml (deployed to ~/.config/quickshell as a store symlink — the
# live-reload watcher misses the new path, so a manual restart is required).
# Usage: just [hostname]  — defaults to x1carbon.
default host='x1carbon':
    #!/usr/bin/env bash
    set -euo pipefail
    sudo nixos-rebuild switch --flake ".#{{host}}"
    if pgrep -f "[H]yprland" >/dev/null 2>&1; then
      # comm name is ".quickshell-wra" (kernel-truncated from .quickshell-wrapped);
      # -x avoids matching this recipe's own argv.
      pkill -x .quickshell-wra || true
      sleep 1
      QS=quic"kshell"
      command "$QS" >/dev/null 2>&1 &
      disown || true
      echo "quickshell relaunched"
    else
      echo "no Hyprland session on this host; skipped quickshell restart"
    fi

# ===== pre-commit workflow =====

# Format the tree (nixfmt, as declared by the flake formatter).
format:
    nix fmt

# Stage new files (flake only evaluates git-tracked files), then flake check.
check:
    git add -A
    nix flake check

# Push to BOTH remotes:
#   origin (full history, ssh://git@homeserver:2222)
#   gitea  (public snapshot mirror: the current tree as ONE root commit, so no
#           old history / historical secrets are ever visible on gitea.edley.me)
# NOTE: gitea push authenticates via ~/.git-credentials (the gitea-mcp-token).
# Run from a clean tree; commit first.
push-all:
    #!/usr/bin/env bash
    set -euo pipefail
    git push origin main
    MSG="Nix-Vibe public snapshot (squashed history)"
    ROOT_SHA=$(git commit-tree HEAD^{tree} -m "$MSG")
    git push --force gitea "$ROOT_SHA:refs/heads/main"
    echo "pushed main -> origin (full) and snapshot -> gitea"

# ===== deployment helpers (no quickshell restart) =====

# Build and test a config without activating it.
# Usage: just dry-build [hostname]
dry-build host='x1carbon':
    sudo nixos-rebuild dry-build --flake .#{{host}}

# Apply a config without switching (for servers / remote hosts).
# Usage: just test [hostname]
test host='x1carbon':
    sudo nixos-rebuild test --flake .#{{host}}

# Relaunch QuickShell on the current Hyprland session (picks up a rebuilt
# shell.qml without a full deploy).
restart-qs:
    #!/usr/bin/env bash
    set -euo pipefail
    if ! pgrep -f "[H]yprland" >/dev/null 2>&1; then
      echo "not in a Hyprland session; nothing to restart" >&2
      exit 1
    fi
    # comm name is ".quickshell-wra" (kernel-truncated from .quickshell-wrapped);
    # -x avoids matching this recipe's own argv.
    pkill -x .quickshell-wra || true
    sleep 1
    QS=quic"kshell"
    command "$QS" >/dev/null 2>&1 &
    disown || true
    echo "quickshell relaunched"