'keep' option to keep the intermediate build folder
Is your feature request related to a problem?
I want to diagnose a reproducible builds problem where the build nondeterministically either reproduces correctly or fails with the same output each time.
Proposed solution
Aside the current --keep-failed, introduce a --keep option that keeps the build directory regardless of whether the (re)build failed.
Alternative solutions
Additional context
Checklist
- [x] checked latest Nix manual (source)
- [x] checked open feature issues and pull requests for possible duplicates
Add :+1: to issues you find important.
Does --rebuild https://nix.dev/manual/nix/2.32/command-ref/new-cli/nix3-build#opt-rebuild help with that?
No, --rebuild will not on its own keep the build dir, it needs --keep-failed for that (but that only keeps it when it fails). Of course you can work around it by adding an 'exit 1' to the installPhase or something like that, but having a flag would be more convenient.
Looks like adding an option means changing the protocol between the client and the daemon, which introduces a bunch of ceremony.
For now I'm just applying this locally
commit 6a3e3982a82fc4c88ce218049702d1d0be032e3d (HEAD -> keep-even-when-not-failed, raboof/keep-even-when-not-failed)
Author: Arnout Engelen <[email protected]>
Date: Wed Nov 26 09:26:01 2025 +0100
Keep build dir even when not failed
diff --git a/src/libstore/unix/build/derivation-builder.cc b/src/libstore/unix/build/derivation-builder.cc
index 333c4dff8..9e12c2a88 100644
--- a/src/libstore/unix/build/derivation-builder.cc
+++ b/src/libstore/unix/build/derivation-builder.cc
@@ -1902,7 +1902,7 @@ void DerivationBuilderImpl::cleanupBuild(bool force)
/* Don't keep temporary directories for builtins because they
might have privileged stuff (like a copy of netrc). */
- if (settings.keepFailed && !force && !drv.isBuiltin()) {
+ if (settings.keepFailed && !drv.isBuiltin()) {
printError("note: keeping build directory '%s'", tmpDir);
chmod(topTmpDir.c_str(), 0755);
chmod(tmpDir.c_str(), 0755);