name: CI

on:
  push:
    branches:
      - main
  # pull_request:

jobs:
  list-packages:
    runs-on: x86_64
    outputs:
      packages: ${{ steps.set-matrix.outputs.packages }}
    steps:
      - uses: actions/checkout@v4
      - id: set-matrix
        run: |
          source /etc/bashrc
          PACKAGES=$(nix flake show --json | nix run nixpkgs#jq -- -c '.packages."x86_64-linux" | keys')
          echo "packages=$PACKAGES" >> $GITHUB_OUTPUT

  # check:
  #   runs-on: x86_64
  #   steps:
  #     - name: Checkout code
  #       uses: actions/checkout@v4
  #     - name: Check flake
  #       run: |
  #         source /etc/bashrc
  #         nix flake check
          
  build:
    needs: list-packages
    runs-on: x86_64
    strategy:
      fail-fast: false
      matrix:
        package: ${{ fromJson(needs.list-packages.outputs.packages) }}

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

      - name: Build package
        run: |
          source /etc/bashrc
          nix build .#${{ matrix.package }}

      - name: Cache & Push to Attic
        if: success()
        env:
          ATTIC_SERVER: ${{ secrets.ATTIC_SERVER }}
          ATTIC_TOKEN: ${{ secrets.ATTIC_TOKEN }}
        run: |
          source /etc/bashrc
          nix shell nixpkgs#attic-client -c bash -c "
            attic login mizuki \"$ATTIC_SERVER\" \"$ATTIC_TOKEN\"
            attic cache info public
            attic push public ./result
          "

