name: test
on: [push, pull_request]
jobs:

  nix-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v31
      - id: set-matrix
        name: Generate Nix Matrix
        run: |
          set -Eeu
          matrix="$(nix eval --json '.#githubActions.matrix')"
          echo "matrix=$matrix" >> "$GITHUB_OUTPUT"

  build-nix:
    name: "nix: ${{ matrix.name }} (${{ matrix.system }})"
    needs: nix-matrix
    runs-on: ${{ matrix.os }}
    strategy:
      matrix: ${{fromJSON(needs.nix-matrix.outputs.matrix)}}
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v31
      - uses: cachix/cachix-action@v16
        with:
          name: arximboldi
          signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
      - run: nix build -L '.#${{ matrix.attr }}'

  docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - uses: cachix/install-nix-action@v31
      - uses: cachix/cachix-action@v16
        with:
          name: arximboldi
          signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
      - run: nix develop --command cmake -B build
      - run: nix develop --command make -C build docs
      - uses: shimataro/ssh-key-action@v2
        if: github.repository_owner == 'arximboldi' && github.ref == 'refs/heads/master'
        with:
          key: ${{ secrets.SINUSOIDES_SSH_KEY }}
          known_hosts: ${{ secrets.SINUSOIDES_KNOWN_HOSTS }}
      - run: nix develop --command make -C build upload-docs
        if: github.repository_owner == 'arximboldi' && github.ref == 'refs/heads/master'

  check:
    strategy:
      matrix:
        type: [Debug, Release]
        toolchain: [gnu, llvm]
        opts: [[]]
        include:
          - type: Debug
            toolchain: gnu
            opts: ['coverage']
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v31
      - uses: cachix/cachix-action@v16
        with:
          name: arximboldi
          signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
      - name: configure cmake
        run: |
          nix develop .#${{ matrix.toolchain }} --command \
            cmake -B build -G Ninja \
              -DCMAKE_BUILD_TYPE=${{ matrix.type }} \
              -DENABLE_COVERAGE=${{ contains(matrix.opts, 'coverage') }}
      - run: nix develop .#${{ matrix.toolchain }} --command ninja -C build check
      - run: nix develop .#${{ matrix.toolchain }} --command bash -c "bash <(curl -s https://codecov.io/bash)"
        if: ${{ contains(matrix.opts, 'coverage') }}
