nix-shell: add additional packages to the environment

Created on 9 Jun 2014  路  3Comments  路  Source: NixOS/nix

Currently, I can use nix-shell to create an environment with just the particular packages I want:

$ nix-shell -p foo bar

I can also load the dependencies of a package:

$ nix-shell .

However, I want to combine the two, to load the dependencies of a package plus some additional packages:

$ nix-shell . -p foo
error: syntax error, unexpected '.', at (string):1:66

Is this possible?

Most helpful comment

Surely it's possible, it's just not what nix-shell currently does right? I'd also like to be able to load the deps in a shell.nix file and also add in other incidental packages I may need (such as my preferred editor, etc.).

If we shouldn't be using nix-shell this way, what's the recommended way to create a shell environment that reflects a project's shell.nix file, plus a few selected packages?

All 3 comments

No, combining is not possible. That's because -p is implemented by generating a Nix expression on the fly. So here it generates an expression with buildInputs = [ . foo ];.

Surely it's possible, it's just not what nix-shell currently does right? I'd also like to be able to load the deps in a shell.nix file and also add in other incidental packages I may need (such as my preferred editor, etc.).

If we shouldn't be using nix-shell this way, what's the recommended way to create a shell environment that reflects a project's shell.nix file, plus a few selected packages?

I figured out a way to achieve what you're looking for from nix repl .

nix-repl> pkgs = import <nixpkgs> {}
nix-repl> pkg = import ./default.nix {}
nix-repl> pkg-dev = pkgs.stdenv.mkDerivation { name="${pkg.name}-vim"; buildInputs=pkg.buildInputs ++ [pkgs.vim]; }
nix-repl> :s pkg-dev

Using nix-shell -E (which evaluates a nix expression) you can achieve essentially same behaviour:

nix-shell -E 'with import <nixpkgs> {}; runCommand "foo" { buildInputs=(import ./default.nix {}).buildInputs ++ [vim]; } "" '

This is probably too much information, but I plan to create a derivation in ~/.config/nixpkgs/config.nix that encapsulates my vim config requirements. I'll assign this to a bash alias so that I can quickly produce an environment for vim in addition to my package's requirements.

Was this page helpful?
0 / 5 - 0 ratings