I created a derivation and added it to the nix store, now how do I remove it?

Viewed 736

I followed the 7th Nix pill tutorial, and created a derivation that placed an executable in the nix store, i.e. /nix/store/gh66mkic4c1dys8ag8yqnv10x59b7vmh-simple/simple.

I can run that executable, either directly or via symlinks to it. However, how do I remove it? I tried deleting old generations with $ nix-env --delete-generations old, and also garbage collecting with nix-store --gc, but my derivation's output still appears at that path and can be run there.

Now that I've completed the tutorial, how do I get rid of what I've created in the nix store? Does nixos ever clean up such old derivations? Does it need to be somehow marked as irrelevant before running the delete-old-generations or garbage-collect commands?

1 Answers

Garbage collection deletes everything that isn't reachable from any GC root. This means that if something sticks around, you there's a GC root somewhere that you're not thinking of. You can find these with the nix-store -q --roots command:

For example, here's why my emacs is "alive":

$ nix-store -q --roots /nix/store/hwial1dr7sd6ydf81d465jrllxn4gpdm-emacs-with-packages-27.2/bin/emacs
/nix/var/nix/gcroots/per-user/user/current-home -> /nix/store/kl2l02697jxy9mzf5yz72ph18hh0vgsd-home-manager-generation
/nix/var/nix/profiles/per-user/user/home-manager-3-link -> /nix/store/kl2l02697jxy9mzf5yz72ph18hh0vgsd-home-manager-generation
/nix/var/nix/profiles/per-user/user/profile-25-link -> /nix/store/fds0f002lq8sxj0kqj9zq6d7vdakbakf-user-environment
/proc/6695/maps -> /nix/store/z7hr6k4apccj824pvymlyma8dppz7f16-home-manager-path
/proc/3589/maps -> /nix/store/z7hr6k4apccj824pvymlyma8dppz7f16-home-manager-path

The first two roots have been created by home-manager, the third by the nix-env -i command that home-manager uses. The ones from /proc are memory-mapped files in active processes.

Related