Derivation Specific Cleanup

My flake derivation performs the following steps. The code is huge. Patches are small.

git clone
git patch
make build

Patches change frequently. So, every derivation duplicates the code. I gotta garbage collect after every 6 builds otherwise system runs out of memory. However, this deletes all unused paths. Which means my custom compilers and whatnot which are not in my persistent system config are also deleted.

I need to only delete paths generated by nix build .#mytarget for this specific flake.nix derivation. Or a way to delete previous derivation before running another.

You want to create garbage collector roots for the dependencies that don’t rebuild as frequently.

1 Like

I wonder if I can automate it with smth like:

postInstall = ''
    ln -s current.derivation.hash  /nix/var/nix/gcroots/bar

However I assume it is impossible to get the hash since correct hash requires a successful build with the correct hash.
Nope: Heresy here.

An easier alternative is direnv. Store paths are symlinked to .direnv. Which prevents flakes from gc.

1 Like

I’d personally not do that with postInstall, but in a flake app and a wrapper script or something. That postInstall would be run by any process building your derivation, which would then make it create roots for itself and cause it to not be cleaned up. Problematic if it gets used by anyone who didn’t read your code.

direnv is pretty cool, indeed, but only solves your problem if you use this derivation as part of a shell. It could be a nice place to call another script that creates garbage collector roots though.

1 Like