Stop flake from being garbage collected?

Hi,

I’ve created a flake that builds a jupyter notebook kernel with several machine learning libraries: ml_env/flake.nix at a4ee8a7cce0f6eb3c6f27c1078874d34aa47fcf2 · AlexChalk/ml_env · GitHub .

After garbage collection, when I run nix run, the flake rebuilds. (This isn’t due to updating flake.lock or poetry.lock).

How can I stop this? I would expect the symlink pointing to the flake output in result/bin to stop garbage collection from running.

Thanks!

1 Like

Garbage collection is probably deleting build dependencies. I guess that’s why your flake gets rebuilt after garbage collection. It detected that it’s missing its build dependencies and is trying to fetch them.

ln -s result /nix/var/nix/gcroots/

Aren’t results a gc root already ?

1 Like

I don’t think a link is created by nix run. To create one, instead of nix run <installable>, use nix build -o <out link path> <installable>. Example:

$ nix run nixpkgs#hello
... will download and run hello...
$ nix-collect-garbage
... garbage collects hello...
$ nix run nixpkgs#hello
... will re-download and run hello...
$ nix build -o pleasedonotdeleteme nixpkgs#hello
.... will create a link called pleasedonotdeleteme, add it to the garbage collection roots, ...
$ nix-collect-garbage
... now will NOT collect hello ...
$ nix run nixpkgs#hello
... will use the cached hello

In your case, you would have to create a link for .#jupyter-wrapper.

Note that moving the out link (pleasedonotdeleteme) will break the garbage collection root, so the package will be garbage collected again.

For more information, see the manuals for nix run and nix build.

4 Likes

Take a look at direnv and nix-direnv, it takes care of this automagically and brings along many useful goodies.

Hi, does anybody know how this could be done when doing the build for a remote host (nixos-rebuild switch --flake .#host --target-host xxx) ?

I don’t find which directory could be symlinked into /nix/var/nix/gcroots/

I found a way to preserve remote hosts builds in the nix store, I first do a nixos-rebuld build-vm --fast --flake .#hostname, which symlinks the result in /nix/var/nix/gcroots/