Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active April 28, 2024 17:20
Show Gist options
  • Save n8henrie/85a391f85d9628a09c7f99c3db41a62d to your computer and use it in GitHub Desktop.
Save n8henrie/85a391f85d9628a09c7f99c3db41a62d to your computer and use it in GitHub Desktop.
{
description = "Demo of https://zimbatm.com/notes/1000-instances-of-nixpkgs";
inputs.nixpkgs.url = "github:nixos/nixpkgs";
outputs =
{ self, nixpkgs }:
let
systems = [
"aarch64-darwin"
"x86_64-linux"
"aarch64-linux"
];
eachSystem =
with nixpkgs.lib;
f: foldAttrs mergeAttrs { } (map (s: mapAttrs (_: v: { ${s} = v; }) (f s)) systems);
in
eachSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
packages = {
default = self.packages.${system}.run-both;
run-both = pkgs.writeShellApplication {
name = "run";
text = ''
set -x
${self.outputs.packages.${system}.test-import-script}/bin/test-import
${self.outputs.packages.${system}.test-legacyPackages-script}/bin/test-legacyPackages
'';
};
import-example = pkgs.writeText "flake.nix" ''
{
inputs.nixpkgs.url = "github:nixos/nixpkgs";
outputs = {self, nixpkgs}: {
packages.${system}.default = let
pkgs = import nixpkgs { system = "${system}"; };
in pkgs.writeShellScriptBin "say-hello" '''
echo "hello"
''';
};
}
'';
legacyPackages-example = pkgs.writeText "flake.nix" ''
{
inputs.nixpkgs.url = "github:nixos/nixpkgs";
outputs = {self, nixpkgs}: {
packages.${system}.default = let
pkgs = nixpkgs.legacyPackages.${system};
in pkgs.writeShellScriptBin "say-hello" '''
echo "hello"
''';
};
}
'';
test-import-script = pkgs.writeShellApplication {
name = "test-import";
runtimeInputs = with pkgs; [
nix
nixfmt-rfc-style
];
excludeShellChecks = [
"SC2016"
"SC2154"
];
text = ''
mkdir -p demo-import
cd demo-import
echo '{ inputs.nixpkgs.url = "github:nixos/nixpkgs";' > flake.nix
for num in {1..100}; do
mkdir -p import-"$num"
pushd import-"$num" >/dev/null
cp ${self.outputs.packages.${system}.import-example} flake.nix
chmod 0644 flake.nix
popd >/dev/null
echo "inputs.import-$num.url = \"./import-$num\";" >> flake.nix
echo "inputs.import-$num.inputs.nixpkgs.follows = \"nixpkgs\";" >> flake.nix
done
cat <<'EOF' >> flake.nix
outputs = {self, nixpkgs, ...}: {
packages.${system}.default = (import nixpkgs { system = "${system}"; }).runCommand "fake" {} '''
touch $out
EOF
for num in {1..100}; do
echo '# fake reference to ''${self.inputs.import-'"$num.packages.${system}.default}" >> flake.nix
done
echo "'''; }; }" >> flake.nix
nixfmt .
${pkgs.time}/bin/time -v nix build 2>&1 1>/dev/null |
grep 'Maximum resident set size'
'';
};
test-legacyPackages-script = pkgs.writeShellApplication {
name = "test-legacyPackages";
runtimeInputs = with pkgs; [
nix
nixfmt-rfc-style
];
excludeShellChecks = [
"SC2016"
"SC2154"
];
text = ''
mkdir -p demo-legacyPackages
cd demo-legacyPackages
echo '{ inputs.nixpkgs.url = "github:nixos/nixpkgs";' > flake.nix
for num in {1..100}; do
mkdir -p legacyPackages-"$num"
pushd legacyPackages-"$num" >/dev/null
cp ${self.outputs.packages.${system}.legacyPackages-example} flake.nix
chmod 0644 flake.nix
popd >/dev/null
echo "inputs.legacyPackages-$num.url = \"./legacyPackages-$num\";" >> flake.nix
echo "inputs.legacyPackages-$num.inputs.nixpkgs.follows = \"nixpkgs\";" >> flake.nix
done
cat <<'EOF' >> flake.nix
outputs = {self, nixpkgs, ...}: {
packages.${system}.default = nixpkgs.legacyPackages.${system}.runCommand "fake" {} '''
touch $out
EOF
for num in {1..100}; do
echo '# fake reference to ''${self.inputs.legacyPackages-'"$num.packages.${system}.default}" >> flake.nix
done
echo "'''; }; }" >> flake.nix
nixfmt .
${pkgs.time}/bin/time -v nix build 2>&1 1>/dev/null |
grep 'Maximum resident set size'
'';
};
apps.default = {
type = "app";
program = "${self.packages.${system}.run-both}/bin/run";
};
devShells.default = pkgs.mkShell { };
};
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment