# Copyright 2024 RustFS Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# DISABLED. This workflow is switched off in the repository's Actions settings
# (state: disabled_manually) and does not run on any trigger, including its cron
# and workflow_dispatch. That state lives in GitHub's UI and is invisible when
# reading this file, which has already misled at least one audit — hence this
# banner. Re-enabling is a UI action; anyone doing so should first check that the
# workflow still matches the current CI layout. See rustfs/backlog#1603.
#
name: Nix CI

on:
  push:
    branches: [ "main" ]
    paths:
      - 'flake.nix'
      - 'flake.lock'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - '.github/workflows/nix.yml'
  pull_request:
    types: [ opened, synchronize, reopened, closed ]
    branches: [ "main" ]
    paths:
      - 'flake.nix'
      - 'flake.lock'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - '.github/workflows/nix.yml'
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true
permissions:
  contents: read
  id-token: write

jobs:
  cancel-closed-pr-runs:
    name: Cancel Closed PR Runs
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Explain cancellation run
        run: echo "PR closed; this run only cancels older runs in the same concurrency group."

  nix-validation:
    name: Nix Build & Check
    if: github.event_name != 'pull_request' || github.event.action != 'closed'
    runs-on: ubuntu-latest
    timeout-minutes: 60
    permissions:
      contents: read
      id-token: write
    env:
      NIX_CURL_FLAGS: -A cargo/stable
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          persist-credentials: false

      - name: Install Nix
        uses: DeterminateSystems/determinate-nix-action@4eea0b33e3d1f02ecfe37cf16e7204c424009606 # v3.21.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          extra-conf: |
            experimental-features = nix-command flakes configurable-impure-env
            impure-env = NIX_CURL_FLAGS
            max-jobs = 1

      - name: Cache Nix
        uses: DeterminateSystems/flakehub-cache-action@c01e819d047464c3edf6ba778f075952af5a3aa7 # v3.21.0

      - name: Check Nix Flake Inputs
        uses: DeterminateSystems/flake-checker-action@3164002371bc90729c68af0e24d5aacf20d7c9f6 # v12
        with:
          fail-mode: true
          ignore-missing-flake-lock: false

      - name: Verify Flake
        run: |
          echo "Checking flake structure and evaluation..."
          nix flake show
          for attempt in 1 2 3; do
            if nix flake check --print-build-logs --fallback --option fallback true; then
              break
            fi
            if [ "$attempt" -eq 3 ]; then
              echo "nix flake check failed after 3 attempts."
              exit 1
            fi
            sleep $((attempt * 15))
          done

      - name: Build RustFS
        run: |
          echo "Building the default package..."
          for attempt in 1 2 3; do
            if nix build .#default --print-build-logs --fallback --option fallback true; then
              break
            fi
            if [ "$attempt" -eq 3 ]; then
              echo "nix build failed after 3 attempts."
              exit 1
            fi
            sleep $((attempt * 15))
          done

      - name: Test Binary
        run: |
          echo "Verifying the built binary..."
          if [ -x "./result/bin/rustfs" ]; then
              ./result/bin/rustfs --help
              echo "Binary verification successful."
          else
              echo "Error: Binary not found or not executable at ./result/bin/rustfs"
              exit 1
          fi
