name: 🏗️ Build QOwnNotes for Nix

on:
  push:
    branches-ignore:
      - release
    tags-ignore:
      - "*"
    paths:
      - "src/**"
      - "flake.*"
      - "devenv.*"
      - "shell.nix"
      - "default.nix"
      - "build-systems/nix/**"
      - ".github/workflows/build-nix.yml"
  pull_request:
    paths:
      - "src/**"
      - "flake.*"
      - "devenv.*"
      - "shell.nix"
      - "default.nix"
      - "build-systems/nix/**"
      - ".github/workflows/build-nix.yml"
  workflow_dispatch:

jobs:
  build:
    name: 🏗️ Build ${{ matrix.just-recipe }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        just-recipe: ["nix-build", "nix-build-cmake-qt5", "nix-build-qt5"]
        os: ["ubuntu-latest", "macos-15", "macos-14"]
    steps:
      - name: ⚙️ Install Nix
        uses: cachix/install-nix-action@v31
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - name: 🔒 Cache dependencies
        uses: cachix/cachix-action@v16
        with:
          # https://app.cachix.org/cache/qownnotes-devenv
          name: qownnotes-devenv
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
      - name: 🔧 Install devenv.sh
        run: nix profile add nixpkgs#devenv
      - name: 🧰 Checkout code
        uses: actions/checkout@v6
        with:
          submodules: recursive
      - name: 🚧 Build nix package
        run: devenv shell just ${{ matrix.just-recipe }}

  vm-test:
    name: "🧪 Run VM tests"
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: "📥 Checkout repository"
        uses: actions/checkout@v6

      - name: "📦 Install Nix"
        uses: cachix/install-nix-action@v31
        with:
          nix_path: nixpkgs=channel:nixos-unstable

      - name: 🔒 Cache dependencies
        uses: cachix/cachix-action@v16
        with:
          # https://app.cachix.org/cache/qownnotes-devenv
          name: qownnotes-devenv

      - name: 🧰 Checkout code
        uses: actions/checkout@v6
        with:
          submodules: recursive

      - name: "🧪 Run QOwnNotes integration test"
        run: nix build .?submodules=1#checks.x86_64-linux.qownnotes -L --no-link
