on: [push, pull_request]
name: Nix

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name != 'main' && github.ref || github.run_id }}
  cancel-in-progress: true

jobs:
  required:
    name: "Required Checks: Nix"
    runs-on: namespace-profile-ghostty-sm
    needs:
      - check-zig-cache-hash
    steps:
      - id: status
        name: Determine status
        run: |
          results=$(tr -d '\n' <<< '${{ toJSON(needs.*.result) }}')
          if ! grep -q -v -E '(failure|cancelled)' <<< "$results"; then
            result="failed"
          else
            result="success"
          fi
          {
            echo "result=${result}"
            echo "results=${results}"
          } | tee -a "$GITHUB_OUTPUT"
      - if: always() && steps.status.outputs.result != 'success'
        name: Check for failed status
        run: |
          echo "One or more required build workflows failed: ${{ steps.status.outputs.results }}"
          exit 1

  check-zig-cache-hash:
    if: github.repository == 'ghostty-org/ghostty'
    runs-on: namespace-profile-ghostty-sm
    env:
      ZIG_LOCAL_CACHE_DIR: /zig/local-cache
      ZIG_GLOBAL_CACHE_DIR: /zig/global-cache
    steps:
      - name: Checkout code
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
      - name: Setup Cache
        uses: namespacelabs/nscloud-cache-action@15799a6b54e5765f85b2aac25b3f0df43ed571c0 # v1.4.3
        with:
          path: |
            /nix
            /zig
      - name: Setup Nix
        uses: cachix/install-nix-action@8aa03977d8d733052d78f4e008a241fd1dbf36b3 # v31.10.6
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - uses: cachix/cachix-action@5f2d7c5294214f71b873db4b969586b980625e71 # v17
        with:
          name: ghostty
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
          useDaemon: false # sometimes fails on short jobs
      - name: Check Zig cache
        run: nix develop -c ./nix/build-support/check-zig-cache.sh
