name: Nix Flake Check

on:
  push:
    branches: [main, nix]
    tags:
      - 'v*'
    paths:
      - 'flake.nix'
      - 'flake.lock'
      - 'bun.nix'
      - 'bun.lock'
      - 'package.json'
      - '.github/workflows/nix-build.yml'
  pull_request:
    branches: [main]
    paths:
      - 'flake.nix'
      - 'flake.lock'
      - 'bun.nix'
      - 'bun.lock'
      - 'package.json'
      - '.github/workflows/nix-build.yml'

permissions:
  contents: read

jobs:
  check:
    runs-on: ubuntu-latest
    timeout-minutes: 45
    env:
      NIX_CONFIG: |
        accept-flake-config = true
        access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}

    steps:
      - uses: actions/checkout@v4

      - name: Install Nix
        uses: DeterminateSystems/nix-installer-action@main

      - name: Setup Nix Cache
        uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Regenerate bun.nix from bun.lock
        run: nix run --accept-flake-config github:nix-community/bun2nix -- -o bun.nix

      - name: Check flake
        run: nix flake check --accept-flake-config

      - name: Show flake info
        run: nix flake show --accept-flake-config

      - name: Build package
        if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
        run: nix build --print-build-logs --accept-flake-config
