name: Build NixOS (on demand)

on:
  workflow_dispatch:
    inputs:
      hosts:
        description: 'list of hosts to build'
        required: true
        default: ',othalan,algiz'
        type: string
      lockFile:
        description: 'flake.lock file'
        type: string

  workflow_call:
    inputs:
      hosts:
        description: 'list of hosts to build'
        default: ',othalan,algiz'
        type: string

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - uses: ./.github/actions/clean-disk
        with:
          purge-packages: 'true'

      - uses: DeterminateSystems/nix-installer-action@v14
        with:
          source-url: https://install.lix.systems/lix/lix-installer-x86_64-linux
          extra-conf: |
            experimental-features = pipe-operator
            accept-flake-config = true

      - uses: DeterminateSystems/magic-nix-cache-action@v8

      # - uses: cachix/cachix-action@v15
      #   with:
      #     name: daylin
      #     authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
      
      - name: write lock file
        if: "${{ inputs.lockFile != '' }}"
        run: |
          echo '${{ inputs.lockFile }}' > flake.lock
          git diff

      - name: Pre-build Oizys
        run: nix build . --print-build-logs

      - name: Build
        run: |
          nix run . \
            -- \
            build --minimal \
              --host,="${{ inputs.hosts }}" \
              --flake . \
              --debug \
              -- \
              --keep-going \
              --print-build-logs