.\" -*- mode: troff; coding: utf-8 -*- .TH "nix-shell" "1" "" .SH Name \fCnix-shell\fR - start an interactive shell based on a Nix expression .SH Synopsis \fCnix-shell\fR [\fC--arg\fR \fIname\fR \fIvalue\fR] [\fC--argstr\fR \fIname\fR \fIvalue\fR] [{\fC--attr\fR | \fC-A\fR} \fIattrPath\fR] [\fC--command\fR \fIcmd\fR] [\fC--run\fR \fIcmd\fR] [\fC--exclude\fR \fIregexp\fR] [\fC--pure\fR] [\fC--keep\fR \fIname\fR] {{\fC--packages\fR | \fC-p\fR} {\fIpackages\fR | \fIexpressions\fR} … | [\fIpath\fR]} .SH Description The command \fCnix-shell\fR will build the dependencies of the specified derivation, but not the derivation itself. It will then start an interactive shell in which all environment variables defined by the derivation \fIpath\fR have been set to their corresponding values, and the script \fC$stdenv/setup\fR has been sourced. This is useful for reproducing the environment of a derivation for development. .PP If \fIpath\fR is not given, \fCnix-shell\fR defaults to \fCshell.nix\fR if it exists, and \fCdefault.nix\fR otherwise. .PP If \fIpath\fR starts with \fChttp://\fR or \fChttps://\fR, it is interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. The tarball must include a single top-level directory containing at least a file named \fCdefault.nix\fR. .PP If the derivation defines the variable \fCshellHook\fR, it will be run after \fC$stdenv/setup\fR has been sourced. Since this hook is not executed by regular Nix builds, it allows you to perform initialisation specific to \fCnix-shell\fR. For example, the derivation attribute .LP .EX shellHook = '' echo \(dqHello shell\(dq export SOME_API_TOKEN=\(dq$(cat \(ti/.config/some-app/api-token)\(dq ''; .EE .PP will cause \fCnix-shell\fR to print \fCHello shell\fR and set the \fCSOME_API_TOKEN\fR environment variable to a user-configured value. .SH Options All options not listed here are passed to \fCnix-store --realise\fR, except for \fC--arg\fR and \fC--attr\fR / \fC-A\fR which are passed to \fCnix-instantiate\fR. .IP "\(bu" 2 \fC--command\fR \fIcmd\fR .br In the environment of the derivation, run the shell command \fIcmd\fR. This command is executed in an interactive shell. (Use \fC--run\fR to use a non-interactive shell instead.) However, a call to \fCexit\fR is implicitly added to the command, so the shell will exit after running the command. To prevent this, add \fCreturn\fR at the end; e.g. \fC--command \(dqecho Hello; return\(dq\fR will print \fCHello\fR and then drop you into the interactive shell. This can be useful for doing any additional initialisation. .IP "\(bu" 2 \fC--run\fR \fIcmd\fR .br Like \fC--command\fR, but executes the command in a non-interactive shell. This means (among other things) that if you hit Ctrl-C while the command is running, the shell exits. .IP "\(bu" 2 \fC--exclude\fR \fIregexp\fR .br Do not build any dependencies whose store path matches the regular expression \fIregexp\fR. This option may be specified multiple times. .IP "\(bu" 2 \fC--pure\fR .br If this flag is specified, the environment is almost entirely cleared before the interactive shell is started, so you get an environment that more closely corresponds to the “real” Nix build. A few variables, in particular \fCHOME\fR, \fCUSER\fR and \fCDISPLAY\fR, are retained. .IP "\(bu" 2 \fC--packages\fR / \fC-p\fR \fIpackages\fR… .br Set up an environment in which the specified packages are present. The command line arguments are interpreted as attribute names inside the Nix Packages collection. Thus, \fCnix-shell -p libjpeg openjdk\fR will start a shell in which the packages denoted by the attribute names \fClibjpeg\fR and \fCopenjdk\fR are present. .IP "\(bu" 2 \fC-i\fR \fIinterpreter\fR .br The chained script interpreter to be invoked by \fCnix-shell\fR. Only applicable in \fC#!\fR-scripts (described below). .IP "\(bu" 2 \fC--keep\fR \fIname\fR .br When a \fC--pure\fR shell is started, keep the listed environment variables. .LP The following common options are supported: .SH Environment variables .IP "\(bu" 2 \fCNIX_BUILD_SHELL\fR .br Shell used to start the interactive environment. Defaults to the \fCbash\fR found in \fC\fR, falling back to the \fCbash\fR found in \fCPATH\fR if not found. .SH Examples To build the dependencies of the package Pan, and start an interactive shell in which to build it: .LP .EX $ nix-shell '' -A pan [nix-shell]$ eval ${unpackPhase:-unpackPhase} [nix-shell]$ cd pan-* [nix-shell]$ eval ${configurePhase:-configurePhase} [nix-shell]$ eval ${buildPhase:-buildPhase} [nix-shell]$ ./pan/gui/pan .EE .PP The reason we use form \fCeval ${configurePhase:-configurePhase}\fR here is because those packages that override these phases do so by exporting the overridden values in the environment variable of the same name. Here bash is being told to either evaluate the contents of \[u2018]configurePhase\[u2019], if it exists as a variable, otherwise evaluate the configurePhase function. .PP To clear the environment first, and do some additional automatic initialisation of the interactive shell: .LP .EX $ nix-shell '' -A pan --pure \e --command 'export NIX_DEBUG=1; export NIX_CORES=8; return' .EE .PP Nix expressions can also be given on the command line using the \fC-E\fR and \fC-p\fR flags. For instance, the following starts a shell containing the packages \fCsqlite\fR and \fClibX11\fR: .LP .EX $ nix-shell -E 'with import { }; runCommand \(dqdummy\(dq { buildInputs = [ sqlite xorg.libX11 ]; } \(dq\(dq' .EE .PP A shorter way to do the same is: .LP .EX $ nix-shell -p sqlite xorg.libX11 [nix-shell]$ echo $NIX_LDFLAGS … -L/nix/store/j1zg5v…-sqlite-3.8.0.2/lib -L/nix/store/0gmcz9…-libX11-1.6.1/lib … .EE .PP Note that \fC-p\fR accepts multiple full nix expressions that are valid in the \fCbuildInputs = [ ... ]\fR shown above, not only package names. So the following is also legal: .LP .EX $ nix-shell -p sqlite 'git.override { withManual = false; }' .EE .PP The \fC-p\fR flag looks up Nixpkgs in the Nix search path. You can override it by passing \fC-I\fR or setting \fCNIX_PATH\fR. For example, the following gives you a shell containing the Pan package from a specific revision of Nixpkgs: .LP .EX $ nix-shell -p pan -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/8a3eea054838b55aca962c3fbde9c83c102b8bf2.tar.gz [nix-shell:\(ti]$ pan --version Pan 0.139 .EE .SH Use as a \fC#!\fR-interpreter You can use \fCnix-shell\fR as a script interpreter to allow scripts written in arbitrary languages to obtain their own dependencies via Nix. This is done by starting the script with the following lines: .LP .EX #! /usr/bin/env nix-shell #! nix-shell -i real-interpreter -p packages .EE .PP where \fIreal-interpreter\fR is the “real” script interpreter that will be invoked by \fCnix-shell\fR after it has obtained the dependencies and initialised the environment, and \fIpackages\fR are the attribute names of the dependencies in Nixpkgs. .PP The lines starting with \fC#! nix-shell\fR specify \fCnix-shell\fR options (see above). Note that you cannot write \fC#! /usr/bin/env nix-shell -i ...\fR because many operating systems only allow one argument in \fC#!\fR lines. .PP For example, here is a Python script that depends on Python and the \fCprettytable\fR package: .LP .EX #! /usr/bin/env nix-shell #! nix-shell -i python -p python pythonPackages.prettytable import prettytable # Print a simple table. t = prettytable.PrettyTable([\(dqN\(dq, \(dqN\(ha2\(dq]) for n in range(1, 10): t.add_row([n, n * n]) print t .EE .PP Similarly, the following is a Perl script that specifies that it requires Perl and the \fCHTML::TokeParser::Simple\fR and \fCLWP\fR packages: .LP .EX #! /usr/bin/env nix-shell #! nix-shell -i perl -p perl perlPackages.HTMLTokeParserSimple perlPackages.LWP use HTML::TokeParser::Simple; # Fetch nixos.org and print all hrefs. my $p = HTML::TokeParser::Simple->new(url => 'http://nixos.org/'); while (my $token = $p->get_tag(\(dqa\(dq)) { my $href = $token->get_attr(\(dqhref\(dq); print \(dq$href\en\(dq if $href; } .EE .PP Sometimes you need to pass a simple Nix expression to customize a package like Terraform: .LP .EX #! /usr/bin/env nix-shell #! nix-shell -i bash -p \(dqterraform.withPlugins (plugins: [ plugins.openstack ])\(dq terraform apply .EE .RS .PP \fBNote\fR .PP You must use double quotes (\fC\(dq\fR) when passing a simple Nix expression in a nix-shell shebang. .RE .LP Finally, using the merging of multiple nix-shell shebangs the following Haskell script uses a specific branch of Nixpkgs/NixOS (the 20.03 stable branch): .LP .EX #! /usr/bin/env nix-shell #! nix-shell -i runghc -p \(dqhaskellPackages.ghcWithPackages (ps: [ps.download-curl ps.tagsoup])\(dq #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-20.03.tar.gz import Network.Curl.Download import Text.HTML.TagSoup import Data.Either import Data.ByteString.Char8 (unpack) -- Fetch nixos.org and print all hrefs. main = do resp <- openURI \(dqhttps://nixos.org/\(dq let tags = filter (isTagOpenName \(dqa\(dq) $ parseTags $ unpack $ fromRight undefined resp let tags' = map (fromAttrib \(dqhref\(dq) tags mapM_ putStrLn $ filter (/= \(dq\(dq) tags' .EE .PP If you want to be even more precise, you can specify a specific revision of Nixpkgs: .LP .EX #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/0672315759b3e15e2121365f067c1c8c56bb4722.tar.gz .EE .PP The examples above all used \fC-p\fR to get dependencies from Nixpkgs. You can also use a Nix expression to build your own dependencies. For example, the Python example could have been written as: .LP .EX #! /usr/bin/env nix-shell #! nix-shell deps.nix -i python .EE .PP where the file \fCdeps.nix\fR in the same directory as the \fC#!\fR-script contains: .LP .EX with import {}; runCommand \(dqdummy\(dq { buildInputs = [ python pythonPackages.prettytable ]; } \(dq\(dq .EE