nix-shell.2
Enter a reproducible build environment
TLDR
Start with nix expression in shell.nix or default.nix in the current directory
Run shell command in non-interactive shell and exit
Start with expression in default.nix in the current directory
Start with packages loaded from nixpkgs
Start with packages loaded from specific nixpkgs revision
Evaluate rest of file in specific interpreter, for use in #!-scripts (see
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'