LinuxCommandLibrary

nix-shell.3

Enter a development environment with specific dependencies

TLDR

Start an interactive shell with some packages from nixpkgs

$ nix shell [nixpkgs#pkg1 nixpkgs#packageSet.pkg2 ...]
copy

Start a shell providing a package from an older version of nixpkgs (21.05)
$ nix shell [nixpkgs/nixos-21.05#pkg]
copy

Start a shell with the "default package" from a flake in the current directory, printing build logs if any builds happen
$ nix shell -L
copy

Start a shell with a package from a flake on GitHub
$ nix shell [github:owner/repo#pkg]
copy

Run a command in a shell with a package
$ nix shell [nixpkgs#pkg] -c [some-cmd --someflag 'Some other arguments']
copy

SYNOPSIS

nix-shell [option...] [path]
nix-shell [option...] --pure [path]

PARAMETERS

--pure
    Create a pure environment. This means that the environment will only contain the specified dependencies and none of the user's system packages. This gives increased reproducibility, but can also be less convenient as you must explicitly declare all packages the environment needs.

-A
    Select an attribute from the Nix expression to use as the package set.

-p
    Add a package to the environment. Can be used multiple times.

--command
    Execute a command in the environment. If not specified, starts an interactive shell. Note the nix expression provided must evaluate to an environment not a derivation.

--run
    Same as --command but requires the expression to be a derivation and opens shell only when the build is completed.

-I
    Add a path to the Nix search path.

--arg
    Pass an argument to the Nix expression.

--argstr
    Pass an argument string to the Nix expression.

--expr
    Evaluate a Nix expression directly.


    Path to a Nix expression. If omitted, `nix-shell` searches for `shell.nix` or `default.nix`.

DESCRIPTION

The `nix-shell` command allows you to create a development environment based on a Nix expression. This environment typically provides access to specific software packages and tools required for a project, isolating dependencies and ensuring reproducibility. It's particularly useful for projects with complex or conflicting dependencies, allowing developers to work in a clean and consistent environment without affecting the system-wide configuration. Nix-shell uses a nix expression (usually `shell.nix` or `default.nix`) to describe the dependencies for the environment. When you execute `nix-shell`, Nix evaluates the expression and creates a temporary environment with the specified packages available in the `PATH`.
You can either specify the Nix expression directly on the command line or let nix-shell search for a suitable expression in the current directory or its parents. This creates an ephemeral environment. Once you exit the shell, the environment is destroyed.
This command promotes declarative and reproducible development environments.

CAVEATS

The environment created by `nix-shell` is temporary. Changes made within the environment will not persist after exiting the shell. To make changes permanent, you need to modify the Nix expression.

EXAMPLE USAGE

To enter a shell with `ghc` and `cabal-install` available: `nix-shell -p ghc cabal-install`.
To use a specific `shell.nix` file: `nix-shell ./myproject/shell.nix`.
To execute a command within the shell environment, using shell.nix: `nix-shell --command 'echo Hello from Nix shell'`

FINDING SHELL.NIX

If the is not specified, nix-shell will search the directory tree for a file named `shell.nix` or `default.nix`.

HISTORY

The `nix-shell` command is a core part of the Nix package manager, designed to facilitate reproducible development environments. Its development has evolved alongside Nix, emphasizing isolation and declarative configuration. Early versions focused on basic environment creation from Nix expressions, while later versions added features like pure environments and more flexible argument passing. It is widely used by nix users for development, contribution and even package management.

SEE ALSO

nix-build(1), nix(1), nix.conf(5)

Copied to clipboard