From e9ee1495544ea4df23465d322e520faabd16bb38 Mon Sep 17 00:00:00 2001 From: illustris Date: Wed, 4 Feb 2026 10:06:57 +0530 Subject: [PATCH] nix flake check: free nixosConfigurations values after checking Save each nixosConfiguration's thunk state before checking, then restore it immediately after. This makes the evaluated configuration tree unreachable, allowing GC_gcollect() to reclaim memory before processing the next config. This keeps only one configuration's evaluation tree in memory at a time, rather than holding all evaluated configurations simultaneously. --- src/nix/flake.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 07868c37933..d6beb93cf04 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1,6 +1,7 @@ #include "nix/main/common-args.hh" #include "nix/main/shared.hh" #include "nix/expr/eval.hh" +#include "nix/expr/eval-gc.hh" #include "nix/expr/eval-inline.hh" #include "nix/expr/eval-settings.hh" #include "nix/expr/get-drvs.hh" @@ -723,9 +724,22 @@ struct CmdFlakeCheck : FlakeCommand else if (name == "nixosConfigurations") { state->forceAttrs(vOutput, pos, ""); - for (auto & attr : *vOutput.attrs()) + + for (auto & attr : *vOutput.attrs()) { + // Save thunk state before forcing + Value savedValue = *attr.value; + checkNixOSConfiguration( fmt("%s.%s", name, state->symbols[attr.name]), *attr.value, attr.pos); + + // Restore thunk so evaluation tree becomes unreachable + *attr.value = savedValue; + + // Trigger GC to free the evaluation tree before next config +#if NIX_USE_BOEHMGC + GC_gcollect(); +#endif + } } else if (name == "hydraJobs")