[Nix-dev] truly persistent nix-shell

Andreas Herrmann andreash87 at gmx.ch
Sun Nov 20 14:41:30 CET 2016


Hi,

I've written a small shell function that creates truly persistent nix-shells (see below). [1]
I thought it might be of use to others. Let me know what you think of it.

What do I mean by "truly persistent"?

The Nix Wiki entry about development environments has a section about "persistent nix-shell environments". [2]
It says:

> nix-shell evaluates an expression and drops you into the environment in one command.
> This is perfect for temporary development environments,
> but you may also want to create a persistent environment
> which you can load again at a later date without re-evaluating the expression.

It then goes on to explain how to achieve that:

>   $ nix-instantiate . --indirect --add-root $PWD/shell.drv
>   $ nix-shell $PWD/shell.drv

However, the above comes short of what I, IMHO, would expect from a _persistent_ nix-shell.
In particular, the above will not protect the dependencies of the nix-shell from garbage collection,
i.e. the packages you want to use within the shell.

Imagine you have configured a nix-shell with a lot of overriden packages that take a long time to build.
If you then create a "persistent" nix-shell according to the above description.
And if you then run the garbage collector.
Then all these packages will be collected (unless another gc-root depends on them)
and you will have to build them once again.

It is possible to avoid this issue by creating a gc-root for every package that the nix-shell pulls in:

    $ nix-store -r $(nix-store --query --references $PWD/shell.drv) \
        --indirect --add-root $PWD/shell.dep

Best, Andreas


[1]: https://gist.github.com/aherrmann/51b56283f9ed5853747908fbab907316
[2]: https://nixos.org/wiki/Development_Environments#Making_a_Persistent_nix-shell_Environment


More information about the nix-dev mailing list