Those 5 different commands apparently wanting to achieve the same goals are a bit confusing
Some seems to be wrapper on each other, but at this point I don’t know which
I am wondering, is it planned to deprecate / merge some of them together?
(particularly nix-collect-garbage with nix-store --gc / nix store gc as the wrapper seems so light it seems counter-productive to be separated)
Also, which of them have a different behaviour when being used with sudo?
Garbage Collection (GC) roots are all the valid symlinks in /nix/var/nix/gcroots (including subdirectories), and running processes.
GC means deleting paths that are not referred to by any GC roots.
Deleting generations is basically invalidating some of those symlinks, allowing those generations and paths to get picked up by the next round of GC.
nix store gc is the nix 3 CLI version of nix-store --gc.
nix-collect-garbage is a wrapper around nix-store --gc and the profile management options in nix-env. It combines the GC root invalidation and the actual GC into one step, for convenience.
nix-heuristic-gc is not part of nix, they’re a third party project and you can read their README to figure out if you want to use them or not.
The difference with nix-heuristic-gc comes when you don’t want to collect all garbage possible (because you foresee some of it being reused in the near future).
nix-collect-garbage --max-freed ... will do something like this - it will delete unreferenced packages until it has reached the size limit, but it does so at random. So you could have lots of small packages you re-use frequently removed, and a huge obscure package you’ve never touched since building left behind.
nix-heuristic-gc lets you adjust how you favour the packages that will be most readily deleted based on several heuristics.
This was initially a consideration, but I think it would be nice to come up with something that had fewer “knobs” if it were going to go anywhere near core. nix is also not the most fun or appropriate place to add new, complex code. It’s a tangle of c++ that’s gradually being refactored into something nicer. Perhaps if nix grew some sort of plugin mechanism that were powerful enough to allow this kind of thing and/or I were able to implement it in rust I might have a stab.
(FWIW if I started it again today I would have done it in rust)