[Nix-dev] truly persistent nix-shell
andreash87 at gmx.ch
Sun Nov 20 14:41:30 CET 2016
I've written a small shell function that creates truly persistent nix-shells (see below). 
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". 
> 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
More information about the nix-dev