name: nix-eval

on:
  push:
    branches: [dev]
  pull_request:
    branches: [dev]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  nix-eval:
    runs-on: blacksmith-4vcpu-ubuntu-2404
    timeout-minutes: 15
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Nix
        uses: nixbuild/nix-quick-install-action@v34

      - name: Evaluate flake outputs (all systems)
        run: |
          set -euo pipefail
          nix --version

          echo "=== Flake metadata ==="
          nix flake metadata

          echo ""
          echo "=== Flake structure ==="
          nix flake show --all-systems

          SYSTEMS="x86_64-linux aarch64-linux x86_64-darwin aarch64-darwin"
          PACKAGES="opencode"
          # TODO: move 'desktop' to PACKAGES when #11755 is fixed
          OPTIONAL_PACKAGES="desktop"

          echo ""
          echo "=== Evaluating packages for all systems ==="
          for system in $SYSTEMS; do
            echo ""
            echo "--- $system ---"
            for pkg in $PACKAGES; do
              printf "  %s: " "$pkg"
              if output=$(nix eval ".#packages.$system.$pkg.drvPath" --raw 2>&1); then
                echo "✓"
              else
                echo "✗"
                echo "::error::Evaluation failed for packages.$system.$pkg"
                echo "$output"
                exit 1
              fi
            done
          done

          echo ""
          echo "=== Evaluating optional packages ==="
          for system in $SYSTEMS; do
            echo ""
            echo "--- $system ---"
            for pkg in $OPTIONAL_PACKAGES; do
              printf "  %s: " "$pkg"
              if output=$(nix eval ".#packages.$system.$pkg.drvPath" --raw 2>&1); then
                echo "✓"
              else
                echo "✗"
                echo "::warning::Evaluation failed for packages.$system.$pkg"
                echo "$output"
              fi
            done
          done

          echo ""
          echo "=== Evaluating devShells for all systems ==="
          for system in $SYSTEMS; do
            printf "%s: " "$system"
            if output=$(nix eval ".#devShells.$system.default.drvPath" --raw 2>&1); then
              echo "✓"
            else
              echo "✗"
              echo "::error::Evaluation failed for devShells.$system.default"
              echo "$output"
              exit 1
            fi
          done

          echo ""
          echo "=== All evaluations passed ==="
