name: KVM Publishable Validation

on:
  push:
  workflow_dispatch:

jobs:
  publishable-kvm-suite:
    runs-on:
      - self-hosted
      - linux
      - x64
    timeout-minutes: 360

    steps:
      - uses: actions/checkout@v4

      - name: Ensure Nix Is Available
        run: |
          set -euo pipefail
          if ! command -v nix >/dev/null 2>&1; then
            curl -L https://nixos.org/nix/install | sh -s -- --no-daemon
          fi
          if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
            . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
          elif [[ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]]; then
            . "$HOME/.nix-profile/etc/profile.d/nix.sh"
          fi
          mkdir -p "$HOME/.config/nix"
          printf '%s\n' 'experimental-features = nix-command flakes' > "$HOME/.config/nix/nix.conf"
          nix --version

      - name: Probe KVM Environment
        run: |
          set -euo pipefail
          if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
            . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
          elif [[ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]]; then
            . "$HOME/.nix-profile/etc/profile.d/nix.sh"
          fi
          echo "hostname=$(hostname)"
          uname -a
          id
          test -e /dev/kvm
          ls -l /dev/kvm
          if [[ -f /sys/module/kvm_intel/parameters/nested ]]; then
            echo "kvm_intel_nested=$(cat /sys/module/kvm_intel/parameters/nested)"
          fi
          if [[ -f /sys/module/kvm_amd/parameters/nested ]]; then
            echo "kvm_amd_nested=$(cat /sys/module/kvm_amd/parameters/nested)"
          fi

      - name: Run Publishable KVM Suite
        run: |
          set -euo pipefail
          if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
            . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
          elif [[ -f "$HOME/.nix-profile/etc/profile.d/nix.sh" ]]; then
            . "$HOME/.nix-profile/etc/profile.d/nix.sh"
          fi
          chmod +x ./nix/test-cluster/run-publishable-kvm-suite.sh
          ./nix/test-cluster/run-publishable-kvm-suite.sh "$RUNNER_TEMP/publishable-kvm-suite"
