Specifying Package Version in `shell.nix`?

nix-env without -A (--attr) searches all packages for one with matching name attribute. It is better to use attribute paths (i.e. attribute name in all-packages.nix and so on) to refer to package since it avoids the need to evaluate all the packages and possible ambiguities. You can print the attribute paths by adding -P (--atrr-path) to nix-env.

$ nix-env -qP --available nodejs
nixos.nodejs       nodejs-10.18.1
nixos.nodejs-10_x  nodejs-10.18.1
nixos.nodejs-12_x  nodejs-12.14.1
nixos.nodejs-13_x  nodejs-13.6.0
$ nix-env -iA nixos.nodejs-13_x

The attribute paths are what is used in Nix expression so you would use normalPackages.nodejs-14_x.

If you really wanted to use the derivation names, you would do something like lib.filterAttrs (a: p: lib.isDerivation p && p.name == "nodejs-12.14.1") pkgs but you would need to handle recursion, packages that fail to evaluate and other issues. Using attribute is much better choice as described above.

3 Likes