Memoize undeclared systems

The memoize function is not a pretty thing in terms of implementation,
but it's exactly what we need to solve this UX problem and performance
problem.

Without this, every distinct `withSystem` call will cause a re-evaluation
of the `perSystem` module, which is inefficient. Now, it's a one-time
13KB and length(system) attribute lookups: negligible compared to any
instantiations and such.

Nix doesn't offer memoization for functions yet, so this is the best we
can do.
This commit is contained in:
Robert Hensing 2025-07-21 20:01:38 +02:00
parent 7782624440
commit 577772566b
7 changed files with 120 additions and 2 deletions

View file

@ -169,6 +169,28 @@ rec {
partitionedAttrs.devShells = "dev";
});
/**
This one is for manual testing. Should look like:
```
nix-repl> checks.x86_64-linux.eval-tests.internals.printSystem.withSystem "foo" ({ config, ... }: null)
trace: Evaluating perSystem for foo
null
nix-repl> checks.x86_64-linux.eval-tests.internals.printSystem.withSystem "foo" ({ config, ... }: null)
null
```
*/
printSystem = mkFlake
{ inputs.self = { }; }
({ withSystem, ... }: {
systems = [ ];
perSystem = { config, system, ... }:
builtins.trace "Evaluating perSystem for ${system}" { };
flake.withSystem = withSystem;
});
dogfoodProvider = mkFlake
{ inputs.self = { }; }
({ flake-parts-lib, ... }: {