Ad hoc shell environments#

In a Nix shell environment, you can immediately use any program packaged with Nix, without installing it permanently.

You can also share the command invoking such a shell with others, and it will work on all Linux distributions, WSL, and macOS[1].

Create a shell environment#

Once you install Nix, you can use it to create new shell environments with programs that you want to use.

In this section you will run two exotic programs called cowsay and lolcat that you will probably not have installed on your machine:

$ cowsay no can do
The program ‘cowsay’ is currently not installed.

$ echo no chance | lolcat
The program ‘lolcat’ is currently not installed.

Use nix-shell with the -p (--packages) option to specify that we need the cowsay and lolcat packages. The first invocation of nix-shell may take a while to download all dependencies.

$ nix-shell -p cowsay lolcat
these 3 derivations will be built:


Within the Nix shell, you can use the programs provided by these packages:

[nix-shell:~]$ cowsay Hello, Nix! | lolcat

Type exit or press CTRL-D to exit the shell, and the programs won’t be available anymore.

[nix-shell:~]$ exit

$ cowsay no more
The program ‘cowsay’ is currently not installed.

$ echo all gone | lolcat
The program ‘lolcat’ is currently not installed.

Running programs once#

You can go even faster, by running any program directly:

$ nix-shell -p cowsay --run "cowsay Nix"

If the command consists only of the program name, no quotes are needed:

$ nix-shell -p hello --run hello

Search for packages#

What can you put in a shell environment? If you can think of it, there’s probably a Nix package of it.


Enter the program name you want to run in to find packages that provide it.

For the following example, find the package names for these programs:

  • git

  • nvim

  • npm

In the search results, each item shows the package name, and the details list the available programs.[2]

Run any combination of programs#

Once you have the package name, you can start a shell with that package. The -p (--packages) argument can take multiple package names.

Start a Nix shell with the packages providing git, nvim, and npm. Again, the first invocation may take a while to download all dependencies.

$ nix-shell -p git neovim nodejs
these 9 derivations will be built:
these 151 paths will be fetched (186.43 MiB download, 1018.20 MiB unpacked):


Check package versions#

Check that you have indeed the specific version of these programs provided by Nix, even if you had any of them already installed on your machine.

[nix-shell:~]$ which git

[nix-shell:~]$ git --version
git version 2.38.3

[nix-shell:~]$ which nvim

[nix-shell:~]$ nvim --version | head -1
NVIM v0.8.1

[nix-shell:~]$ which npm

[nix-shell:~]$ npm --version

Nested shell sessions#

If you need an additional program temporarily, you can run a nested Nix shell. The programs provided by the specified packages will be added to the current environment.

[nix-shell:~]$ nix-shell -p python3
this path will be fetched (11.42 MiB download, 62.64 MiB unpacked):
copying path '/nix/store/pwy30a7siqrkki9r7xd1lksyv9fg4l1r-python3-3.10.11' from ''...

[nix-shell:~]$ python --version
Python 3.10.11

Exit the shell as usual to return to the previous environment.

Towards reproducibility#

These shell environments are very convenient, but the examples so far are not reproducible yet. Running these commands on another machine may fetch different versions of packages, depending on when Nix was installed there.

What do we mean by reproducible? A fully reproducible example would give exactly the same results no matter when or where you run the command. The environment provided would be identical each time.

The following example creates a fully reproducible environment. You can run it anywhere, anytime to obtain the exact same version of the git.

$ nix-shell -p git --run "git --version" --pure -I nixpkgs=
git version 2.33.1

There are three things going on here:

  1. --run executes the given Bash command within the Nix shell and exits when it’s done.

    You can use this with nix-shell whenever you want to quickly run a program you don’t have installed on your machine.

  2. --pure discards most environment variables set on your system when running the shell.

    It means that only the git provided by Nix is available inside that shell. This is useful for simple one-liners such as in the example. While developing, however, you will usually want to have your editor and other tools around. Therefore we recommend to omit --pure for development environments, and to add it only when the extra isolation is needed.

  3. -I determines what to use as a source of package declarations.

    Here we provided a specific Git revision of nixpkgs, leaving no doubt about which version of the packages in that collection will be used.


Next steps#

If you’re done trying out Nix for now, you may want to free up some disk space occupied by the different versions of programs you downloaded by running the examples:

$ nix-collect-garbage