LinuxCommandLibrary

nix-shell.2

Enter a reproducible build environment

TLDR

Start with nix expression in shell.nix or default.nix in the current directory

$ nix-shell
copy

Run shell command in non-interactive shell and exit
$ nix-shell --run "[command] [argument1 argument2 ...]"
copy

Start with expression in default.nix in the current directory
$ nix-shell [default.nix]
copy

Start with packages loaded from nixpkgs
$ nix-shell [[-p|--packages]] [package1 package2 ...]
copy

Start with packages loaded from specific nixpkgs revision
$ nix-shell [[-p|--packages]] [package1 package2 ...] [[-I|--include]] nixpkgs=[https://github.com/NixOS/nixpkgs/archive/nixpkgs_revision.tar.gz]
copy

Evaluate rest of file in specific interpreter, for use in #!-scripts (see )
$ nix-shell -i [interpreter] [[-p|--packages]] [package1 package2 ...]
copy

SYNOPSIS

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

PARAMETERS

--pure
    Create an environment without access to system packages.

--packages package...
    Specify packages to make available in the environment.

--attr attribute
    Use a specific attribute from the Nix expression.

--run command
    Execute a command within the environment instead of starting a shell.

--command command
    Same as --run.

-p package...
    Alias for --packages.

--keep keep
    Variables to keep in the shell (e.g., PATH, HOME).

--ignore-environment
    Ignore all environment variables when building. Useful for reproducibility.

--help
    Show help message.

DESCRIPTION

nix-shell builds and enters a development environment based on a Nix expression. This expression, typically found in a default.nix file or specified directly on the command line, describes the dependencies and build instructions for a project. When invoked, nix-shell evaluates the expression, downloads (or builds) the specified dependencies, and sets up an environment (typically a new shell) where these dependencies are available. This allows for reproducible builds and consistent development environments across different systems.

It is particularly useful for managing dependencies that are not available in the system's package manager or for ensuring a specific version of a toolchain. nix-shell is also useful for CI/CD pipelines.

This environment can be customized with shellHook to do configurations such as setting environment variables. It's commonly used for development to avoid installing dependencies system-wide and to maintain project-specific environments.

CAVEATS

If Nix is not properly configured, nix-shell might fail to find or build dependencies. It also depends on the Nix daemon being active.

EXAMPLE

To enter a development shell defined by a 'default.nix' file in the current directory: nix-shell To enter a shell with specific packages: nix-shell -p hello zsh To run a command in the nix-shell environment directly: nix-shell -p hello --run 'hello'

SEE ALSO

nix(1), nix-build(1)

Copied to clipboard