name: CI

on:
  push:
    branches:
      - main
    paths-ignore:
      - "hosts/machine/kanade/**"
  pull_request:
    paths-ignore:
      - "hosts/machine/kanade/**"

jobs:
  check:
    runs-on: x86_64
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Check flake
        run: |
          source /etc/bashrc
          nix flake check
  ida:
    runs-on: x86_64
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Prefetch IDA
        run: |
          source /etc/bashrc
          nix-prefetch-url --type sha256 https://file.mizuki.guru/.ida/ida-free-pc_93_x64linux.run
  cpt:
    runs-on: x86_64
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Prefetch Cisco Packet Tracer
        run: |
          source /etc/bashrc
          nix-prefetch-url --type sha256 https://file.mizuki.guru/.cpt/CiscoPacketTracer_900_Ubuntu_64bit.deb
          nix-prefetch-url --type sha256 https://file.mizuki.guru/.cpt/CiscoPacketTracer822_amd64_signed.deb

  build-nixos:
    needs: check
    runs-on: x86_64
    strategy:
      fail-fast: false
      matrix:
        host: [mizuki, ena, hako, kazusa]

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

      - name: Build NixOS configuration with nom
        run: |
          source /etc/bashrc
          nix build .#nixosConfigurations.${{ matrix.host }}.config.system.build.toplevel

      - 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 imnyang \"$ATTIC_SERVER\" \"$ATTIC_TOKEN\"
            attic push imnyang ./result
          "
      - name: Clean Builder Environment
        if: success()
        run: |
          nix-collect-garbage -d
