What's the recommended way changing build dir in nix-shell?

I know that I can build a derivation in nix-shell by hand for debugging purposes. When I do:

nix-shell '<nixpkgs>' -A hello
> genericBuild

I get back a hello-${version} subfolder in which the binary is built. That makes sense to me.
But in other builds, like for example one using buildGoPackage there’s a command that changes directory to NIX_BUILD_TOP:

Which results in me ending up in /run/user/1000, which is the default value for NIX_BUILD_TOP on my system when I start a nix-shell.

I’m wondering:

  1. Is there some canonical way to tell nix-shell to use a specific build folder?
  2. Should derivations reference NIX_BUILD_TOP in their steps like that?
  3. Why is nix-shell ignoring my set NIX_BUILD_TOP when I use --keep?
 > NIX_BUILD_TOP=/tmp/build nix-shell '<nixpkgs>' --keep NIX_BUILD_TOP -A hello 

[nix-shell:~]$ echo $NIX_BUILD_TOP

Yeah, these are interesting questions. Unless nix-shell could provide a more container-like behavior similar to nix-build. Maybe something like guix environment --container.

IMHO, no. I consider it a bug, but I never got around to submitting an issue about it. (If you have time, please do.)

1 Like