LinuxCommandLibrary

nix-shell.3

Start an interactive shell based on a Nix expression

TLDR

Development shell from shell.nix
$ nix-shell [shell.nix]
copy
Pure shell with no host environment leaking in
$ nix-shell --pure
copy
Ad-hoc shell with specific packages
$ nix-shell -p [python3] [git]
copy
Run a command in the shell environment and exit
$ nix-shell --run "[make check]"
copy
Select attribute from a Nix expression
$ nix-shell -A [dev]
copy
Keep environment variable in a pure shell
$ nix-shell --pure --keep [HOME]
copy

SYNOPSIS

nix-shell [options] [path]

DESCRIPTION

nix-shell starts an interactive shell in which the environment is set up based on a Nix expression. It provides all build dependencies of the specified derivation without actually building it, making it ideal for development environments.
When used with --packages/-p, it creates ad-hoc environments with specific packages available. When used with --pure, the host environment is cleared to closely match a real Nix build environment.
nix-shell can also be used as a script interpreter via shebang lines (`#!/usr/bin/env nix-shell`) to create reproducible scripts with declared dependencies.

PARAMETERS

PATH

Nix expression file (default: shell.nix or default.nix).
-p, --packages packages...
Set up an environment with the specified packages from Nixpkgs.
--pure
Clear the environment before starting the shell, retaining only HOME, USER, and DISPLAY.
--run cmd
Run a command in a non-interactive shell, then exit.
--command cmd
Run a command in an interactive shell, then exit.
-A, --attr attrPath
Select an attribute from the Nix expression.
--keep name
Keep an environment variable when using --pure.
--exclude regexp
Do not build dependencies whose store path matches the regexp.
-i interpreter
Specify the interpreter for nix-shell shebang scripts.
-v
Verbose output.

CAVEATS

Without --pure, the host environment leaks into the shell, which may cause builds to succeed locally but fail in a clean Nix build. The --packages flag uses Nixpkgs attribute names, not package names.

SEE ALSO

nix(1), nix-build(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard