nix-shell -A go-dev-shell '<nixpkgs>' works fine, but nix run -f '<nixpkgs>' go-dev-shell always complains about This derivation is not meant to be built, aborting.
From what I’ve learned, mkShell is a simple wrapper around stdenv.mkDerivation that disables all build phases (see pkgs/build-support/mkshell/default.nix) - there shouldn’t be any special handling for mkShell in nix-shell, right?
Therefore I cannot understand what is the difference between nix-shell and nix run .
nix-shell gives you an environment where the dependencies of the supplied expression are available, not the package itself.
That’s why derivations created with mkShell cannot be built.
nix-shell has the -p flag that behaves as you expected (i.e. provides a shell with the given package). e.g.:
nix-shell doesn’t build the package itself. I haven’t looked into the details but AIUI it doesn’t run the phases for the derivation, though I think it makes them available as shell functions. mkShell disables building by defining a single phase that always aborts. nix run does build the derivation and makes it available to the shell. It’s like nix-shell -p, which I would assume would abort on a mkShell the same way nix run does.
tl;drnix run is a “new interface”, part of nix 2.0. This new interface has no implementation for the feature from nix-shell that you are looking for.
nix run pkg1 pkg2 supersedes nix-shell -p pkg1 pkg2, but nix-shell -A pkg has no equivalent. The magic of nix-shell -A pkg is that it goes trough all the dependencies of pkg and starts a shell with these packages in the environment.
To reproduce that, you need to be able to extract all the dependencies of a package, which is done in nix-shell (around here and here) but has no equivalent in nix run (here) because it has no extraction logic.
If you want to emulate nix-shell with nix run, you may consider using nix run $(nix-build $(nix-store -q --references $(nix-instantiate) | grep ".drv$" | grep -v bash)) which still uses the old interface to obtain the .drv (nix-instantiate), to extract the dependencies (nix-store -q --references) and to build these .drvs (nix-build). The new nix build interface does not print the outputs, and therefore cannot be used here. the others have no equivalent.
nix 2.0 interface is still a work in progress, you should stick with nix-shell to get a shell from a derivation seen as an “environment” for that shell.
That would be a question for @edolstra, but here is what seems likely to happen from my point of view, which may be very wrong.
nix-shell contains a lot of ad-hoc hacks to setup the environment, and this stuff does not really pertain to nix per se. So I guess there is little chance to see nix-shell ported as-is. More probably, the functionnality will see a large reactoring to move most of the custom hacks into nixpkgs and cleaner, well-defined interfaces.
This is a lot of work, and no-one seems too eager to start working on it as nix-shell is still fully supported, maintained, and working as intended.
More than 10 months passed since the last reply here - are there any news regarding nix-shell in the new interface? Maybe there is some ticket on the topic?
BTW the “better option” here is nix run . nix-shell -p foo sets up the environment as it would for building foo , so all of foo 's dependencies are in your PATH and all of the other environment vars are set too (I count 23 env vars containing NIX ), but nix run produces an environment much more similar to what you’d get if you simply installed the package (e.g. it basically just prefixes your PATH with the appropriate directories from the specified package).
That said, nix-shell replaces your normal shell initialization files and nix run doesn’t, so depending on how your .bashrc is set up you might override the PATH . For example with my setup, echo $PATH in a nix run shell starts the path with /usr/local/bin before the nix-provided path value, because my current .bashrc unconditionally prefixes my PATH (which I really should fix).
❯ nix-shell
these paths will be fetched (0.05 MiB download, 0.28 MiB unpacked):
/nix/store/qgbwdnk91rk26b5bkd6qv5r6c2v733kb-bash-interactive-4.4-p23-dev
copying path '/nix/store/qgbwdnk91rk26b5bkd6qv5r6c2v733kb-bash-interactive-4.4-p23-dev' from 'https://cache.nixos.org'...
[nix-shell:~/dev/research/julia/odecontrol]$
Can I do something similar with nix run? I haven’t been able to make it work:
❯ nix run
error: opening file '/home/skainswo/dev/research/julia/odecontrol/default.nix': No such file or directory
For 2.3, no, run does not read from a shell.nix, as the content of a shell.nix is not meant to be built. run though builds and then gives the build result in the environment. nix-shell and nix shell (2.3) though do not build the mentioned derivations, but instead their buildinputs and give those into the environment.
2.4s run works completely different from both, and require a flake with applications.
What is the difference between nix shell and nix develop? From nix --help for 2.4pre:
nix develop - run a bash shell that provides the build environment of a derivation
nix shell - run a shell in which the specified packages are available
but I still can’t mentally differentiate between the two, unlike nix run, which clearly has a different purpose now - to run nix apps.
While the wiki does have a lot of good info, I’d recommend you make sure to read through all the man pages for nix. By now I think these pages have a lot more info on the new-style Nix commands: