From affc1b35d1861461692d6eed75a416d38b4f8e92 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= <tmuehlbacher@posteo.net>
Date: Sat, 22 Mar 2025 20:34:43 +0100
Subject: [PATCH] chore(nix): structure github CI nix flake checks
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Not sure about this but it might make it easier to tell what the issue
is when the nix CI is failing.

Signed-off-by: Thomas Mühlbacher <tmuehlbacher@posteo.net>
---
 .github/workflows/nix-flake.yml | 41 +++++++++------
 flake.lock                      | 21 ++++++++
 flake.nix                       | 89 ++++++++++++++++++++-------------
 3 files changed, 100 insertions(+), 51 deletions(-)

diff --git a/.github/workflows/nix-flake.yml b/.github/workflows/nix-flake.yml
index 4dfb6453..91d9b27e 100644
--- a/.github/workflows/nix-flake.yml
+++ b/.github/workflows/nix-flake.yml
@@ -1,22 +1,31 @@
-name: "Nix-Tests"
+name: Nix Flake actions
+
 on:
   pull_request:
   push:
+
 jobs:
-  nix-flake-check:
+  nix-matrix:
     runs-on: ubuntu-latest
+    outputs:
+      matrix: ${{ steps.set-matrix.outputs.matrix }}
     steps:
-    - uses: actions/checkout@v4
-    - uses: cachix/install-nix-action@v27
-      with:
-        extra_nix_config: |
-          experimental-features = nix-command flakes
-          access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
-    - uses: cachix/cachix-action@v15
-      with:
-        name: bcachefs-tools
-        # If you chose API tokens for write access OR if you have a private cache
-        authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
-    - run: nix flake show
-    - run: nix flake check --print-build-logs
-    - run: nix build --print-build-logs
+      - uses: actions/checkout@v4
+      - uses: cachix/install-nix-action@v30
+      - id: set-matrix
+        name: Generate Nix Matrix
+        run: |
+          set -Eeu
+          matrix="$(nix eval --json '.#githubActions.matrix')"
+          echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
+
+  nix-build:
+    name: ${{ 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@v30
+      - run: nix build -L '.#${{ matrix.attr }}'
diff --git a/flake.lock b/flake.lock
index c8ea8d15..0a9b2b85 100644
--- a/flake.lock
+++ b/flake.lock
@@ -49,6 +49,26 @@
         "type": "github"
       }
     },
+    "nix-github-actions": {
+      "inputs": {
+        "nixpkgs": [
+          "nixpkgs"
+        ]
+      },
+      "locked": {
+        "lastModified": 1737420293,
+        "narHash": "sha256-F1G5ifvqTpJq7fdkT34e/Jy9VCyzd5XfJ9TO8fHhJWE=",
+        "owner": "nix-community",
+        "repo": "nix-github-actions",
+        "rev": "f4158fa080ef4503c8f4c820967d946c2af31ec9",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nix-community",
+        "repo": "nix-github-actions",
+        "type": "github"
+      }
+    },
     "nixpkgs": {
       "locked": {
         "lastModified": 1742422364,
@@ -85,6 +105,7 @@
         "crane": "crane",
         "flake-compat": "flake-compat",
         "flake-parts": "flake-parts",
+        "nix-github-actions": "nix-github-actions",
         "nixpkgs": "nixpkgs",
         "rust-overlay": "rust-overlay",
         "treefmt-nix": "treefmt-nix"
diff --git a/flake.nix b/flake.nix
index 1200a3b9..79ebfb94 100644
--- a/flake.nix
+++ b/flake.nix
@@ -22,6 +22,11 @@
       url = "github:edolstra/flake-compat";
       flake = false;
     };
+
+    nix-github-actions = {
+      url = "github:nix-community/nix-github-actions";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
   };
 
   outputs =
@@ -33,6 +38,7 @@
       crane,
       rust-overlay,
       flake-compat,
+      nix-github-actions,
     }:
     let
       systems = nixpkgs.lib.filter (s: nixpkgs.lib.hasSuffix "-linux" s) nixpkgs.lib.systems.flakeExposed;
@@ -40,6 +46,13 @@
     flake-parts.lib.mkFlake { inherit inputs; } {
       imports = [ inputs.treefmt-nix.flakeModule ];
 
+      flake = {
+        githubActions = nix-github-actions.lib.mkGithubMatrix {
+          # github actions supports fewer architectures
+          checks = nixpkgs.lib.getAttrs [ "aarch64-linux" "x86_64-linux" ] self.checks;
+        };
+      };
+
       inherit systems;
 
       perSystem =
@@ -225,49 +238,55 @@
               default = config.packages.${name};
             };
 
-          checks.cargo-clippy = common.craneLib.cargoClippy (
-            common.args
-            // {
-              inherit (common) cargoArtifacts;
-              cargoClippyExtraArgs = "--all-targets --all-features -- --deny warnings";
-            }
-          );
+          checks = {
+            inherit (config.packages)
+              bcachefs-tools
+              bcachefs-tools-fuse
+              bcachefs-tools-fuse-i686-linux
+              ;
 
-          # we have to build our own `craneLib.cargoTest`
-          checks.cargo-test = common.craneLib.mkCargoDerivation (
-            common.args
-            // {
-              inherit (common) cargoArtifacts;
-              doCheck = true;
-
-              enableParallelChecking = true;
-
-              pnameSuffix = "-test";
-              buildPhaseCargoCommand = "";
-              checkPhaseCargoCommand = ''
-                make ''${enableParallelChecking:+-j''${NIX_BUILD_CORES}} $makeFlags libbcachefs.a
-                cargo test --profile release -- --nocapture
-              '';
-            }
-          );
-
-          # cargo clippy with the current minimum supported rust version
-          # according to Cargo.toml
-          checks.msrv =
-            let
-              rustVersion = cargoToml.package.rust-version;
-              common = pkgs.callPackage mkCommon { inherit crane rustVersion; };
-            in
-            common.craneLib.cargoClippy (
+            cargo-clippy = common.craneLib.cargoClippy (
               common.args
               // {
-                pname = "msrv";
                 inherit (common) cargoArtifacts;
                 cargoClippyExtraArgs = "--all-targets --all-features -- --deny warnings";
               }
             );
 
-          checks."32-bit" = config.packages."${cargoToml.package.name}-i686-linux";
+            # we have to build our own `craneLib.cargoTest`
+            cargo-test = common.craneLib.mkCargoDerivation (
+              common.args
+              // {
+                inherit (common) cargoArtifacts;
+                doCheck = true;
+
+                enableParallelChecking = true;
+
+                pnameSuffix = "-test";
+                buildPhaseCargoCommand = "";
+                checkPhaseCargoCommand = ''
+                  make ''${enableParallelChecking:+-j''${NIX_BUILD_CORES}} $makeFlags libbcachefs.a
+                  cargo test --profile release -- --nocapture
+                '';
+              }
+            );
+
+            # cargo clippy with the current minimum supported rust version
+            # according to Cargo.toml
+            msrv =
+              let
+                rustVersion = cargoToml.package.rust-version;
+                common = pkgs.callPackage mkCommon { inherit crane rustVersion; };
+              in
+              common.craneLib.cargoClippy (
+                common.args
+                // {
+                  pname = "msrv";
+                  inherit (common) cargoArtifacts;
+                  cargoClippyExtraArgs = "--all-targets --all-features -- --deny warnings";
+                }
+              );
+          };
 
           devShells.default = pkgs.mkShell {
             inputsFrom = [