.\" -*- mode: troff; coding: utf-8 -*- .TH "nix3-shell" "1" "" .RS .PP \fBWarning\fR .br This program is \fB\fBexperimental\fR\fR and its interface is subject to change. .RE .SH Name .LP \fCnix shell\fR - run a shell in which the specified packages are available .SH Synopsis .LP \fCnix shell\fR [\fIoption\fR\[u2026]] \fIinstallables\fR\[u2026] .SH Examples .IP "\(bu" 3 Start a shell providing \fCyoutube-dl\fR from the \fCnixpkgs\fR flake: .LP .EX # nix shell nixpkgs#youtube-dl # youtube-dl --version 2020.11.01.1 .EE .IP "\(bu" 3 Start a shell providing GNU Hello from NixOS 20.03: .LP .EX # nix shell nixpkgs/nixos-20.03#hello .EE .IP "\(bu" 3 Run GNU Hello: .LP .EX # nix shell nixpkgs#hello --command hello --greeting 'Hi everybody!' Hi everybody! .EE .IP "\(bu" 3 Run multiple commands in a shell environment: .LP .EX # nix shell nixpkgs#gnumake --command sh -c \(dqcd src && make\(dq .EE .IP "\(bu" 3 Run GNU Hello in a chroot store: .LP .EX # nix shell --store \(ti/my-nix nixpkgs#hello --command hello .EE .IP "\(bu" 3 Start a shell providing GNU Hello in a chroot store: .LP .EX # nix shell --store \(ti/my-nix nixpkgs#hello nixpkgs#bashInteractive --command bash .EE .IP Note that it\(cqs necessary to specify \fCbash\fR explicitly because your default shell (e.g. \fC/bin/bash\fR) generally will not exist in the chroot. .SH Description .LP \fCnix shell\fR runs a command in an environment in which the \fC$PATH\fR variable provides the specified \fB\fIinstallables\fR\fR. If no command is specified, it starts the default shell of your user account specified by \fC$SHELL\fR. .SH Use as a \fC#!\fR-interpreter .LP You can use \fCnix\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 #! nix shell installables --command real-interpreter .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 \fIinstallables\fR are the attribute names of the dependencies in Nixpkgs. .PP The lines starting with \fC#! nix\fR specify 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 #! nix shell github:tomberek/-#python3With.prettytable --command python 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 #! nix shell github:tomberek/-#perlWith.HTMLTokeParserSimple.LWP --command perl -x 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 #! nix shell --impure --expr \(ga\(ga #! nix with (import (builtins.getFlake ''nixpkgs'') {}); #! nix terraform.withPlugins (plugins: [ plugins.openstack ]) #! nix \(ga\(ga #! nix --command bash terraform \(dq$@\(dq .EE .RS .PP \fBNote\fR .PP You must use double backticks (\fC\(ga\(ga\fR) when passing a simple Nix expression in a nix shell shebang. .RE .PP Finally, using the merging of multiple nix shell shebangs the following Haskell script uses a specific branch of Nixpkgs/NixOS (the 21.11 stable branch): .LP .EX #!/usr/bin/env nix #!nix shell --override-input nixpkgs github:NixOS/nixpkgs/nixos-21.11 #!nix github:tomberek/-#haskellWith.download-curl.tagsoup --command runghc 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 --override-input nixpkgs github:NixOS/nixpkgs/eabc38219184cc3e04a974fe31857d8e0eac098d .EE .PP 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 #! nix shell --impure --file 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 {}; python3.withPackages (ps: with ps; [ prettytable ]) .EE .SH Options .IP "\(bu" 3 \fB\fC--command\fR\fR / \fC-c\fR \fIcommand\fR \fIargs\fR .IP Command and arguments to be executed, defaulting to \fC$SHELL\fR .IP "\(bu" 3 \fB\fC--ignore-environment\fR\fR / \fC-i\fR .IP Clear the entire environment (except those specified with \fC--keep\fR). .IP "\(bu" 3 \fB\fC--keep\fR\fR / \fC-k\fR \fIname\fR .IP Keep the environment variable \fIname\fR. .IP "\(bu" 3 \fB\fC--stdin\fR\fR .IP Read installables from the standard input. No default installable applied. .IP "\(bu" 3 \fB\fC--unset\fR\fR / \fC-u\fR \fIname\fR .IP Unset the environment variable \fIname\fR. .SS Common evaluation options .IP "\(bu" 3 \fB\fC--arg\fR\fR \fIname\fR \fIexpr\fR .IP Pass the value \fIexpr\fR as the argument \fIname\fR to Nix functions. .IP "\(bu" 3 \fB\fC--arg-from-file\fR\fR \fIname\fR \fIpath\fR .IP Pass the contents of file \fIpath\fR as the argument \fIname\fR to Nix functions. .IP "\(bu" 3 \fB\fC--arg-from-stdin\fR\fR \fIname\fR .IP Pass the contents of stdin as the argument \fIname\fR to Nix functions. .IP "\(bu" 3 \fB\fC--argstr\fR\fR \fIname\fR \fIstring\fR .IP Pass the string \fIstring\fR as the argument \fIname\fR to Nix functions. .IP "\(bu" 3 \fB\fC--debugger\fR\fR .IP Start an interactive environment if evaluation fails. .IP "\(bu" 3 \fB\fC--eval-store\fR\fR \fIstore-url\fR .IP The \fBURL of the Nix store\fR to use for evaluation, i.e. to store derivations (\fC.drv\fR files) and inputs referenced by them. .IP "\(bu" 3 \fB\fC--impure\fR\fR .IP Allow access to mutable paths and repositories. .IP "\(bu" 3 \fB\fC--include\fR\fR / \fC-I\fR \fIpath\fR .IP Add \fIpath\fR to the Nix search path. The Nix search path is initialized from the colon-separated \fB\fCNIX_PATH\fR\fR environment variable, and is used to look up the location of Nix expressions using \fBpaths\fR enclosed in angle brackets (i.e., \fC\fR). .IP For instance, passing .LP .EX -I /home/eelco/Dev -I /etc/nixos .EE .IP will cause Nix to look for paths relative to \fC/home/eelco/Dev\fR and \fC/etc/nixos\fR, in that order. This is equivalent to setting the \fCNIX_PATH\fR environment variable to .LP .EX /home/eelco/Dev:/etc/nixos .EE .IP It is also possible to match paths against a prefix. For example, passing .LP .EX -I nixpkgs=/home/eelco/Dev/nixpkgs-branch -I /etc/nixos .EE .IP will cause Nix to search for \fC\fR in \fC/home/eelco/Dev/nixpkgs-branch/path\fR and \fC/etc/nixos/nixpkgs/path\fR. .IP If a path in the Nix search path 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 consist of a single top-level directory. For example, passing .LP .EX -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz .EE .IP tells Nix to download and use the current contents of the \fCmaster\fR branch in the \fCnixpkgs\fR repository. .IP The URLs of the tarballs from the official \fCnixos.org\fR channels (see \fBthe manual page for \fCnix-channel\fR\fR) can be abbreviated as \fCchannel:\fR. For instance, the following two flags are equivalent: .LP .EX -I nixpkgs=channel:nixos-21.05 -I nixpkgs=https://nixos.org/channels/nixos-21.05/nixexprs.tar.xz .EE .IP You can also fetch source trees using \fBflake URLs\fR and add them to the search path. For instance, .LP .EX -I nixpkgs=flake:nixpkgs .EE .IP specifies that the prefix \fCnixpkgs\fR shall refer to the source tree downloaded from the \fCnixpkgs\fR entry in the flake registry. Similarly, .LP .EX -I nixpkgs=flake:github:NixOS/nixpkgs/nixos-22.05 .EE .IP makes \fC\fR refer to a particular branch of the \fCNixOS/nixpkgs\fR repository on GitHub. .IP "\(bu" 3 \fB\fC--override-flake\fR\fR \fIoriginal-ref\fR \fIresolved-ref\fR .IP Override the flake registries, redirecting \fIoriginal-ref\fR to \fIresolved-ref\fR. .SS Common flake-related options .IP "\(bu" 3 \fB\fC--commit-lock-file\fR\fR .IP Commit changes to the flake\(cqs lock file. .IP "\(bu" 3 \fB\fC--inputs-from\fR\fR \fIflake-url\fR .IP Use the inputs of the specified flake as registry entries. .IP "\(bu" 3 \fB\fC--no-registries\fR\fR .IP Don\(cqt allow lookups in the flake registries. .RS .IP \fBDEPRECATED\fR .IP Use \fB\fC--no-use-registries\fR\fR instead. .RE .IP "\(bu" 3 \fB\fC--no-update-lock-file\fR\fR .IP Do not allow any updates to the flake\(cqs lock file. .IP "\(bu" 3 \fB\fC--no-write-lock-file\fR\fR .IP Do not write the flake\(cqs newly generated lock file. .IP "\(bu" 3 \fB\fC--output-lock-file\fR\fR \fIflake-lock-path\fR .IP Write the given lock file instead of \fCflake.lock\fR within the top-level flake. .IP "\(bu" 3 \fB\fC--override-input\fR\fR \fIinput-path\fR \fIflake-url\fR .IP Override a specific flake input (e.g. \fCdwarffs/nixpkgs\fR). This implies \fC--no-write-lock-file\fR. .IP "\(bu" 3 \fB\fC--recreate-lock-file\fR\fR .IP Recreate the flake\(cqs lock file from scratch. .RS .IP \fBDEPRECATED\fR .IP Use \fB\fCnix flake update\fR\fR instead. .RE .IP "\(bu" 3 \fB\fC--reference-lock-file\fR\fR \fIflake-lock-path\fR .IP Read the given lock file instead of \fCflake.lock\fR within the top-level flake. .IP "\(bu" 3 \fB\fC--update-input\fR\fR \fIinput-path\fR .IP Update a specific flake input (ignoring its previous entry in the lock file). .RS .IP \fBDEPRECATED\fR .IP Use \fB\fCnix flake update\fR\fR instead. .RE .SS Logging-related options .IP "\(bu" 3 \fB\fC--debug\fR\fR .IP Set the logging verbosity level to \(oqdebug\(cq. .IP "\(bu" 3 \fB\fC--log-format\fR\fR \fIformat\fR .IP Set the format of log output; one of \fCraw\fR, \fCinternal-json\fR, \fCbar\fR or \fCbar-with-logs\fR. .IP "\(bu" 3 \fB\fC--print-build-logs\fR\fR / \fC-L\fR .IP Print full build logs on standard error. .IP "\(bu" 3 \fB\fC--quiet\fR\fR .IP Decrease the logging verbosity level. .IP "\(bu" 3 \fB\fC--verbose\fR\fR / \fC-v\fR .IP Increase the logging verbosity level. .SS Miscellaneous global options .IP "\(bu" 3 \fB\fC--help\fR\fR .IP Show usage information. .IP "\(bu" 3 \fB\fC--offline\fR\fR .IP Disable substituters and consider all previously downloaded files up-to-date. .IP "\(bu" 3 \fB\fC--option\fR\fR \fIname\fR \fIvalue\fR .IP Set the Nix configuration setting \fIname\fR to \fIvalue\fR (overriding \fCnix.conf\fR). .IP "\(bu" 3 \fB\fC--refresh\fR\fR .IP Consider all previously downloaded files out-of-date. .IP "\(bu" 3 \fB\fC--repair\fR\fR .IP During evaluation, rewrite missing or corrupted files in the Nix store. During building, rebuild missing or corrupted store paths. .IP "\(bu" 3 \fB\fC--version\fR\fR .IP Show version information. .SS Options that change the interpretation of \fBinstallables\fR .IP "\(bu" 3 \fB\fC--expr\fR\fR \fIexpr\fR .IP Interpret \fB\fIinstallables\fR\fR as attribute paths relative to the Nix expression \fIexpr\fR. .IP "\(bu" 3 \fB\fC--file\fR\fR / \fC-f\fR \fIfile\fR .IP Interpret \fB\fIinstallables\fR\fR as attribute paths relative to the Nix expression stored in \fIfile\fR. If \fIfile\fR is the character -, then a Nix expression will be read from standard input. Implies \fC--impure\fR. .RS .LP \fBNote\fR .PP See \fB\fCman nix.conf\fR\fR for overriding configuration settings with command line flags. .RE