Saving dev shells from GC: `nix develop --profile` VS `nix build .#devShells`

Hello!

I’ve been exploring different methods for preserving Nix development shells from garbage collection.
After reading a bit about it, I came across two suggestions that don’t require external tools:

  • Using nix develop --profile
  • Using nix build .#devShells.${system}.default [1]

If I understand correctly, nix develop --profile seems to be the recommended way to do that with the new CLI, and it does work perfectly for me.

However, I was a bit curious and wanted to see if I could achieve the same result by building the devShells.${system}.default derivation using regular nix build and then manually add a symlink in /nix/var/nix/gcroots.

Here’s the command I ran.

nix build .#devShells.x86_64-linux.default --out-link "devshell"

But when I try running

nix develop ./devshell

I get the following error:

error: argument '/nix/store/hbxw6mlmwbcld9jxamhjcwmigzzbzh5g-nix-shell' did not evaluate to a derivation

When comparing the build outputs of nix develop --profile and nix build .#devShells, I see that:

  • the former points to /nix/store/5v1am6j7rqfd9k3zqdqzd21gg11jz9ha-nix-shell-env which looks like a JSON file that can be activated
  • the latter points to /nix/store/hbxw6mlmwbcld9jxamhjcwmigzzbzh5g-nix-shell which looks like a shell script (which I’m assuming is meant to be internal?)

Here’s an excerpt of ./devshell after running the nix build command.

------------------------------------------------------------
 WARNING: the existence of this path is not guaranteed.
 It is an internal implementation detail for pkgs.mkShell.
------------------------------------------------------------

declare -x AR="ar"
declare -x AS="as"
declare -x CC="gcc"
declare -x CONFIG_SHELL="/nix/store/c9mv5v53f7vy3wd00ah0f77k509mzmn7-bash-5.3p15/bin/bash"
declare -x CXX="g++"
declare -x GZIP_NO_TIMESTAMPS="1"
declare -x HOME="/homeless-shelter"
# ... More `declare` statements ...

So, under the hood, what’s the difference between the two?
Is it possible to make nix build behave so that it outputs something that can be interpreted by nix develop?

Thanks!


  1. From this post, which seems outdated as it suggests trying to build the devShell.x86_64-linux attribute which doesn’t exist in my flake. I’m assuming it used to be exposed by the default flake template at some point but not anymore? ↩︎

1 Like