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.
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.