name: Build NixOS ISOs (x86 only)

on:
  push:
    branches: [main]
    paths:
      - "iso/**"
      - "hosts/global/**"
      - "home/global/**"
      - "modules/**"
      - "flake.nix"
      - "flake.lock"
  workflow_dispatch:

jobs:
  build-iso:
    strategy:
      matrix:
        iso-type: [server, desktop]
        arch: [x86]

    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

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

      - name: Build ISO (with retry)
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 45
          max_attempts: 3
          retry_wait_seconds: 30
          command: |
            cd iso
            nix build .#${{ matrix.iso-type }}-iso-x86 \
              --print-build-logs \
              --accept-flake-config

      - name: Get ISO filename
        id: iso-info
        run: |
          cd iso
          ISO_PATH=$(nix build .#${{ matrix.iso-type }}-iso-x86 --print-out-paths --no-link)
          ISO_FILE=$(find $ISO_PATH -name "*.iso" -o -name "*.iso.zst" | head -1)
          ISO_NAME=$(basename "$ISO_FILE")
          echo "iso-path=$ISO_FILE" >> $GITHUB_OUTPUT
          echo "iso-name=$ISO_NAME" >> $GITHUB_OUTPUT

      - name: Upload ISO as artifact
        uses: actions/upload-artifact@v4
        with:
          name: nixos-${{ matrix.iso-type }}-${{ matrix.arch }}-iso
          path: ${{ steps.iso-info.outputs.iso-path }}
          retention-days: 30

  create-release:
    needs: build-iso
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: ./isos

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: ./isos/**/*.iso*
          tag_name: ${{ github.ref_name }}
          name: NixOS ISOs ${{ github.ref_name }}
          body: |
            ## NixOS ISOs for ${{ github.ref_name }}

            This release contains automatically built NixOS ISOs based on my configuration.

            ### Available ISOs:
            - **Server ISO**: Minimal server environment with SSH access
            - **Desktop ISO**: GNOME desktop environment for installation
            - **Architecture**: x86_64 (Intel/AMD systems)

            ### Usage:
            1. Download the appropriate ISO for your system
            2. Flash to USB or boot in VM
            3. Default credentials: user `nixos`, password `nixos`
            4. SSH is enabled for remote installation

            Note: ARM ISOs are not provided in this build. Users requiring ARM versions should build locally.

            Built automatically from commit: ${{ github.sha }}
          draft: false
          prerelease: false