56 lines
1.7 KiB
Nix
Raw Permalink Normal View History

{
# Makes outputs available on multiple platforms using a hand-rolled
# `forAllSystems` helper (no extra dependencies).
# Compare with the `flake-utils` approach described in notes/005-multi-system.md.
description = "Multi-system outputs with forAllSystems";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
2026-04-20 11:12:42 +02:00
outputs =
{ self, nixpkgs, ... }:
let
# List every system you want to support.
supportedSystems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
];
# Helper: apply a function to each system and collect results into an attrset.
# `nixpkgs.lib.genAttrs` turns a list of keys + a function into { key = f key; … }.
2026-04-20 11:12:42 +02:00
forAllSystems =
f: nixpkgs.lib.genAttrs supportedSystems (system: f (import nixpkgs { inherit system; }));
in
{
# Every output that varies per system goes through `forAllSystems`.
packages = forAllSystems (pkgs: {
default = pkgs.writeShellScriptBin "hello-multi" ''
echo "hello from $(uname -s)/$(uname -m)"
'';
});
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
2026-04-20 11:12:42 +02:00
packages = with pkgs; [
jq
ripgrep
];
shellHook = ''
echo "dev shell on $(uname -s)/$(uname -m)"
'';
};
});
# `nix flake check` evaluates everything under `checks`.
# Wrapping a simple test here shows the pattern.
checks = forAllSystems (pkgs: {
2026-04-20 11:12:42 +02:00
runs = pkgs.runCommand "hello-multi-runs" { } ''
${self.packages.${pkgs.stdenv.hostPlatform.system}.default}/bin/hello-multi > $out
'';
});
};
}