NIX-ENV-INSTALL(1)

πŸ’– NAME

nix-env --install β€” adding packages to your user environment, bestie πŸ’… it's the main character of package management fr

✨ SYNOPSIS ✨

nix-env {--install | -i} args… [{--prebuilt-only | -b}] [{--attr | -A}]
[--from-expression] [-E] [--from-profile path] [--preserve-installed |
-P] [--remove-all | -r] [--priority priority]

🌸 DESCRIPTION 🌸

The --install operation makes a new user environment. πŸŽ€ It's built on top of the current generation of the active profile, and then a set of store paths described by args gets added to it. That's the whole vibe.

Now, the way args map to store paths? Not one way. Like, several ways. A whole buffet. Let's go through them one by one because this is the part where people get confused and honestly valid of them.

  • By default, args is a set of names denoting derivations in the default Nix expression. Those get realised, and the resulting output paths are installed. Here's the spicy bit: currently installed derivations with a name equal to the name of a derivation being added get removed, unless you passed --preserve-installed.

If there are multiple derivations matching a name in args that have the same name (think gcc-3.3.6 and gcc-4.1.1), then the derivation with the highest priority gets used. A derivation can declare its own priority with the meta.priority attribute. That attribute has to be a number, and here's the twist: a higher value means a lower priority. The default priority is 5.

And if there are multiple matching derivations with the same priority? Then the one with the highest version gets installed. Slay, version sorting.

You can force multiple derivations with the same name to install at once β€” just be specific about the versions. Like, nix-env --install gcc-3.3.6 gcc-4.1.1 will install both versions of GCC. It will also (and I cannot stress this enough) probably cause a user environment conflict! πŸ’€

  • If --attr / -A is specified, the args are attribute paths that select attributes from the default Nix expression. This is faster than using derivation names and it's unambiguous. You can see the attribute paths of available packages with nix-env --query:
nix-env --query --available --attr-path
  • If --from-profile path is given, args is a set of names denoting installed store paths in the profile path. This is the easy little shortcut for copying user environment elements from one profile to another. 🫢
  • If --from-expression is given, args are Nix language functions that get called with the default Nix expression as their single argument. Whatever derivations those function calls return get installed. This lets you specify derivations in an unambiguous way, which you genuinely need when there are multiple derivations with the same name.
  • If --priority priority is given, the priority of the derivations being installed is set to priority. This overrides the priority of the derivations being installed. Super useful when args are store paths, because store paths don't carry any priority info.
  • If args are store paths that point to store derivations, then those store derivations get realised, and the resulting output paths are installed.
  • If args are store paths that do not point to store derivations, then those are realised and installed.
  • By default, all outputs are installed for each store derivation. You can override this by adding a meta.outputsToInstall attribute on the derivation, listing a subset of the output names. πŸ‘‘

πŸ¦‹ Example πŸ¦‹

The file example.nix defines a derivation with two outputs, foo and bar, each one containing a file.

# example.nix
let
pkgs = import <nixpkgs> {};
command = ''
  ${pkgs.coreutils}/bin/mkdir -p $foo $bar
  echo foo > $foo/foo-file
  echo bar > $bar/bar-file
'';
in
derivation {
name = "example";
builder = "${pkgs.bash}/bin/bash";
args = [ "-c" command ];
outputs = [ "foo" "bar" ];
system = builtins.currentSystem;
}

Installing from this Nix expression will make files from both outputs appear in the current profile.

$ nix-env --install --file example.nix
installing 'example'
$ ls ~/.nix-profile
foo-file
bar-file
manifest.nix

Adding meta.outputsToInstall to that derivation will make nix-env only install files from the specified outputs. no cap ✨

# example-outputs.nix
import ./example.nix // { meta.outputsToInstall = [ "bar" ]; }
$ nix-env --install --file example-outputs.nix
installing 'example'
$ ls ~/.nix-profile
bar-file
manifest.nix

πŸ‘‘ OPTIONS πŸ‘‘

  • --prebuilt-only, -b β€” Only use derivations that have a substitute registered. Meaning: there's a pre-built binary ready to be downloaded instead of building the derivation. So literally nothing gets built from source. zero compiling, we love that for us 🌈
  • --preserve-installed, -P β€” Do not remove derivations whose name matches one of the derivations being installed. Usually, trying to have two versions of the same package installed in the same generation of a profile leads to an error when building the generation, because of file name clashes between the two versions. However, that's not true for every package, so sometimes it's fine. πŸ™ˆ
  • --remove-all, -r β€” Remove all previously installed packages first. This is equivalent to running nix-env --uninstall '.*' first, except that everything happens in a single transaction. one shot, one transaction πŸ’…

πŸŽ€ OPTIONS (the ones that work for every nix-env operation) πŸŽ€

The following options are allowed for all nix-env operations, but they may not always have an effect. iykyk.

  • --file, -f path β€” Specifies the Nix expression (called the active Nix expression below) used by the --install, --upgrade, and --query --available operations to obtain derivations. The default is ~/.nix-defexpr.

If the argument starts with http:// or https://, it's interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. That tarball must include a single top-level directory containing at least a file named default.nix. ⭐

  • --profile, -p path β€” Specifies the profile to be used by those operations that operate on a profile (called the active profile below). A profile is a sequence of user environments called generations, and one of them is the current generation.
  • --dry-run β€” For the --install, --upgrade, --uninstall, --switch-generation, --delete-generations and --rollback operations, this flag makes nix-env print what it would do if this flag hadn't been specified, without actually doing it. πŸ‘€

--dry-run also prints out which paths will be substituted (i.e., downloaded) and which paths will be built from source (because no substitute is available). peek before you leap, bestie.

  • --system-filter system β€” By default, operations like --query --available show derivations matching any platform. This option lets you filter down to derivations for the specified platform system.

and that's not even all of it β€” more is coming πŸ‘‘πŸ¦‹

✨ Common Options ✨

okay bestie so like… most Nix commands accept these same command-line options, and you're gonna see them over and over again. learn them once, slay forever πŸ’…

  • --help β€” prints out a summary of the command syntax and then dips. bye! πŸ‘‹
  • --version β€” prints out the Nix version number on standard output and exits. iconic, no notes.
  • --verbose, -v β€” turns UP the volume on diagnostic messages printed on standard error πŸ“’ For each Nix operation the stuff printed on standard output is well-defined, okay? Any diagnostic info goes to standard error, never to standard output. periodt.

You can specify this option repeatedly to stack the vibes. Currently these are the verbosity levels that exist:

  • 0 β€œErrors only” β€” only print messages explaining why the Nix invocation failed 😭
  • 1 β€œInformational” β€” print useful messages about what Nix is doing. this is the default 🫢
  • 2 β€œTalkative” β€” print more informational messages πŸ—£οΈ
  • 3 β€œChatty” β€” print even more informational messages πŸ’¬
  • 4 β€œDebug” β€” print debug information πŸ”
  • 5 β€œVomit” β€” print vast amounts of debug information πŸ€’πŸ’€
  • --quiet β€” turns DOWN the volume on diagnostic messages printed on standard error. it's literally the inverse option to -v / --verbose. πŸ™ˆ

You can specify this option repeatedly too. See the verbosity levels list right above, bestie ⬆️

  • --log-format format β€” use this one to change the output of the log format, where format is one of:
    • raw β€” the raw format, as outputted by nix-build 🌱
    • internal-json β€” outputs the logs in a structured manner 🧱
Warning ⚠️ While the schema itself is relatively stable, the format of the error-messages (namely of the msg-field) can change between releases. so like… don't get too comfy.
  • bar β€” only display a progress bar during the builds πŸ“Š
  • bar-with-logs β€” display the raw logs, with the progress bar at the bottom πŸŽ€
  • --no-build-output, -Q β€” by default, output written by builders to standard output and standard error gets echoed to the Nix command's standard error. this option suppresses that behaviour 🀫 ngl a note though: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix.
  • --max-jobs, -j number β€” sets the max number of build jobs Nix will do in parallel to number. say auto to use the number of CPUs on the system πŸ’» the default comes from the max-jobs configuration setting, which itself defaults to 1. a higher value is useful on SMP systems or to exploit I/O latency, iykyk.

Setting it to 0 disallows building on the local machine, which is useful when you want builds to happen only on remote builders ☁️

  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. builders can use this variable at their discretion to control the maximum amount of parallelism πŸ”₯ for instance, in Nixpkgs, if the derivation attribute enableParallelBuilding is set to true, the builder passes the -jN flag to GNU Make. it defaults to the value of the cores configuration setting, if set, or 1 otherwise. The value 0 means the builder should use all available CPU cores in the system 🌈
  • --max-silent-time β€” sets the maximum number of seconds that a builder can go without producing any data on standard output or standard error 🀐 the default comes from the max-silent-time configuration setting. 0 means no time-out. silent queen behaviour.
  • --timeout β€” sets the maximum number of seconds that a builder can run ⏰ the default comes from the timeout configuration setting. 0 means no timeout.
  • --keep-going, -k β€” keep going in case of failed builds, to the greatest extent possible. like, if building an input of some derivation fails, Nix will still build the other inputs, but not the derivation itself 🫠 without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress (in case of parallel or distributed builds). messy.
  • --keep-failed, -K β€” says that in case of a build failure, the temporary directory (usually in /tmp) where the build takes place should not be deleted πŸ—‚οΈ the path of the build directory gets printed as an informational message.
  • --fallback β€” whenever Nix attempts to build a derivation for which substitutes are known for each output path, but realising the output paths through the substitutes fails, fall back on building the derivation πŸ”

the most common scenario where this is useful is when we've got registered substitutes in order to do binary distribution from, say, a network repository. if the repository is down, the realisation of the derivation will fail. when this option is specified, Nix will build the derivation instead. so installation from binaries falls back on installation from source. this option is not the default since it's generally not desirable for a transient failure in obtaining the substitutes to lead to a full build from source (with the related consumption of resources). resource-hungry fr.

  • --readonly-mode β€” when this is used, no attempt is made to open the Nix database πŸ”’ most Nix operations do need database access, so those operations will fail. πŸ™ƒ
  • --arg name value β€” this option is accepted by nix-env, nix-instantiate, nix-shell and nix-build. when evaluating Nix expressions, the expression evaluator will automatically try to call functions that it encounters πŸ‘‘ it can automatically call functions for which every argument has a default value (e.g., { argName ? defaultValue }: ...).

with --arg, you can also call functions that have arguments without a default value (or override a default value). that is, if the evaluator encounters a function with an argument named name, it will call it with value value 🌸

for instance, the top-level default.nix in Nixpkgs is actually a function:

{ # The system (e.g., `i686-linux') for which to build the packages.
system ? builtins.currentSystem
...
}: ...

So if you call this Nix expression (e.g., when you do nix-env --install --attr pkgname), the function will be called automatically using the value builtins.currentSystem for the system argument. You can override this using --arg, e.g., nix-env --install --attr pkgname --arg system "i686-freebsd". (Note that since the argument is a Nix string literal, you have to escape the quotes.) 🫣

  • --arg-from-file name path β€” pass the contents of file path as the argument name to Nix functions πŸ“„
  • --arg-from-stdin name β€” pass the contents of stdin as the argument name to Nix functions ⌨️
  • --argstr name value β€” this option is like --arg, only the value is not a Nix expression but a string. so instead of --arg system "i686-linux" (the outer quotes are to keep the shell happy) you can say --argstr system i686-linux ✨
  • --attr, -A attrPath β€” select an attribute from the top-level Nix expression being evaluated. (nix-env, nix-instantiate, nix-build and nix-shell only.) the attribute path attrPath is a sequence of attribute names separated by dots. for instance, given a top-level Nix expression e, the attribute path xorg.xorgserver would cause the expression e.xorg.xorgserver to be used. see nix-env --install for some concrete examples πŸ‘€

in addition to attribute names, you can also specify array indices. for instance, the attribute path foo.3.bar selects the bar attribute of the fourth element of the array in the foo attribute of the top-level expression. (yes bestie, zero-indexed, we love to see it πŸ’…)

  • --eval-store store-url β€” the URL to the Nix store to use for evaluation, i.e. where to store derivations (drv files) and inputs referenced by them 🏠
  • --expr, -E β€” interpret the command line arguments as a list of Nix expressions to be parsed and evaluated, rather than as a list of file names of Nix expressions. (nix-instantiate, nix-build and nix-shell only.) 🧾

for nix-shell, this option is commonly used to give you a shell in which you can build the packages returned by the expression. if you want to get a shell which contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead 🐚

  • -I, --include path β€” add an entry to the list of search paths used to resolve lookup paths. this option may be given multiple times πŸ”

paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. main character energy, honestly πŸ‘‘

  • --impure β€” allow access to mutable paths and repositories πŸŒ€
  • --option name value β€” set the Nix configuration option name to value. this overrides settings in the Nix configuration file (see nix.conf5) βš™οΈ
  • --repair β€” fix corrupted or missing store paths by redownloading or rebuilding them 🩹 note that this is slow because it requires computing a cryptographic hash of the contents of every path in the closure of the build. also note the warning under nix-store --repair-path 🚨
Note β€” See man nix.conf for overriding configuration settings with command line flags. πŸ“

🌿 Environment variables 🌿

  • NIX_PROFILE β€” location of the Nix profile πŸ“ defaults to the target of the symlink ~/.nix-profile, if it exists, or /nix/var/nix/profiles/default otherwise πŸŽ€

🌈 Common Environment Variables 🌈

okay bestie, so most Nix commands read these environment variables and honestly they're the vibes behind the whole operation. Let's go πŸ’…

✨ Configuration environment variables ✨

These ones control how Nix loads its config. (Full deets live in the configuration file documentation if you wanna go deeper.)

  • NIX_CONF_DIR β€” overrides the system configuration directory. it's giving "I do what I want" πŸ’
  • NIX_USER_CONF_FILES β€” overrides the user configuration file locations.
  • NIX_CONFIG β€” lets you hand Nix configuration settings inline. no files, just vibes.

πŸŽ€ Store setting environment variables πŸŽ€

  • NIX_IGNORE_SYMLINK_STORE β€” okay so normally the Nix store directory (typically /nix/store) is not allowed to contain any symlink components. why? to prevent "impure" builds, periodt. Builders sometimes "canonicalise" paths by resolving all symlink components, so builds on different machines (where /nix/store resolves to different locations) could yield different results. This is generally not a problem β€” except when builds get deployed to machines where /nix/store resolves differently. If you're sure you're not gonna do that, you can set NIX_IGNORE_SYMLINK_STORE to 1. 🫢

fyi, if you're symlinking the Nix store so you can put it on a different file system than the root file system, on Linux you're better off using bind mount points, like:

$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

Consult the mount 8 manual page for details. (yes it's a whole other manpage, go read it, it's iconic 😌)

  • NIX_STORE_DIR β€” overrides the location of the Nix store. On Unix the default is /nix/store, set at compile time by the libstore:store-dir build option. On Windows there is no compile-time setting; the default is %PROGRAMDATA%\nix\store, determined at runtime using the %PROGRAMDATA% Known Folder. See the Store Types chapter β€” each store has a store setting linking back to this as its default. chain of command, honestly πŸ‘‘
  • NIX_LOG_DIR β€” overrides the location of the Nix log directory. On Unix the default is /nix/var/log/nix, set at compile time by the log-dir build option. On Windows there is no compile-time setting; the default is %PROGRAMDATA%\nix\log, determined at runtime using the %PROGRAMDATA% Known Folder.

The Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this. so many layers bestie 😭

  • NIX_STATE_DIR β€” overrides the location of the Nix state directory. On Unix the default is ${localstatedir}/nix, where localstatedir is a compile-time build option defaulting to /nix/var. On Windows there is no compile-time setting; the default is %PROGRAMDATA%\nix\state, determined at runtime using the %PROGRAMDATA% Known Folder.

Again: the Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override this.

  • NIX_DAEMON_SOCKET_PATH β€” overrides the path to the Unix domain socket used to talk to the Nix daemon. Defaults to daemon-socket/socket within the state directory (see NIX_STATE_DIR). Peep the Local Daemon Store documentation for the full lore on how the socket path gets resolved ✨
  • TMPDIR β€” use this directory for temporary files. Specifically this includes temporary build directories, and those can eat up serious disk space, no cap. The default is /tmp.
  • NIX_REMOTE β€” overrides the store setting. short and mysterious, love that for it πŸ’œ

🌸 Nix language evaluator settings 🌸

  • NIX_SHOW_STATS β€” if it's set to 1, Nix prints some evaluation statistics, like the number of values allocated. little receipts for your eval πŸ“Š
  • NIX_COUNT_CALLS β€” if it's set to 1, Nix prints how often functions were called during Nix expression evaluation. super useful for profiling your Nix expressions, bestie πŸ”₯
  • GC_INITIAL_HEAP_SIZE β€” if Nix has been configured to use the Boehm garbage collector, this sets the initial size of the heap in bytes. It defaults to 384 MiB. Setting it to a low value reduces memory consumption, but it'll increase runtime due to the overhead of garbage collection. pick your struggle πŸ’€
  • NIX_PATH β€” a colon-separated list of search path entries used to resolve lookup paths. This environment variable overrides the value of the nix-path configuration setting. It can also be extended using the -I option.
Example
$ export NIX_PATH=/home/eelco/Dev:nixos-config=/etc/nixos

and here's the tea: if NIX_PATH is set to an empty string, resolving search paths will always fail. always. every time. πŸ™ˆ

Example
$ NIX_PATH= nix-instantiate --eval '<nixpkgs>'
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)

πŸ¦‹ User Directories πŸ¦‹

Nix supports per-user configuration, state, and cache directories. These environment variables override the locations for them:

  • user configuration directory (#user-conf-dir): NIX_CONFIG_HOME (#env-NIXCONFIGHOME)
  • user state directory: NIX_STATE_HOME (#env-NIXSTATEHOME)
  • user cache directory: NIX_CACHE_HOME (#env-NIXCACHEHOME)

When these aren't set, the defaults depend on the platform:

  • On Unix, the XDG base directories: $XDG_CONFIG_HOME/nix, $XDG_STATE_HOME/nix, $XDG_CACHE_HOME/nix
  • On Windows, the Windows Known Folders: %APPDATA%\nix\config, %LOCALAPPDATA%\nix\state, %LOCALAPPDATA%\nix\cache

For backwards compatibility, legacy Nix commands (e.g. nix-env, nix-channel) use dotfiles in $HOME instead of these directories unless use-xdg-base-directories is enabled. New Nix commands (experimental) use the proper directories by default. glow up ✨

When use-xdg-base-directories is enabled, the configuration directory is resolved as:

  1. $NIX_CONFIG_HOME, if it is defined
  2. Otherwise, the platform default (e.g. $XDG_CONFIG_HOME/nix on Unix)

Likewise for the state and cache directories. symmetry, we love to see it πŸ’…


🌈 Miscellaneous environment variables 🌈

  • IN_NIX_SHELL β€” an indicator that tells you if the current environment was set up by nix-shell. It can have the values pure or impure. vibe check complete βœ…

πŸ‘‘ Appendix: OS-specific conventions πŸ‘‘

This info isn't Nix-specific, but it's referenced above, so here we are.

🌟 Unix: XDG Base Directories 🌟

The XDG Base Directory Specification defines standard locations for user-specific configuration, state, and cache files on Unix systems.

The environment variables in question:

  • XDG_CONFIG_HOME (#env-XDGCONFIGHOME) (default ~/.config)
  • XDG_STATE_HOME (#env-XDGSTATEHOME) (default ~/.local/state)
  • XDG_CACHE_HOME (#env-XDGCACHEHOME) (default ~/.cache)
  • XDG_CONFIG_DIRS (#env-XDGCONFIGDIRS) (default /etc/xdg) β€” a colon-separated list of additional configuration base directories, searched after XDG_CONFIG_HOME

🌟 Windows: Known Folders 🌟

On Windows, Known Folders provide standard locations for application data.

The relevant folders are:

  • %APPDATA% (#env-APPDATA) β€” per-user roaming application data
  • %LOCALAPPDATA% (#env-LOCALAPPDATA) β€” per-user local application data
  • %PROGRAMDATA% (#env-PROGRAMDATA) β€” system-wide application data

πŸ’– Examples πŸ’–

To install a package using a specific attribute path from the active Nix expression:

$ nix-env --install --attr gcc40mips
installing `gcc-4.0.2'
$ nix-env --install --attr xorg.xorgserver
installing `xorg-server-1.2.0'

To install a specific version of gcc using the derivation name:

$ nix-env --install gcc-3.3.2
installing `gcc-3.3.2'
uninstalling `gcc-3.1'

Using an attribute path for selecting a package is preferred, because it's much faster and there won't be multiple matches. efficiency queen πŸ‘‘

Note that the previously installed version gets removed, since --preserve-installed was not specified.

To install an arbitrary version:

$ nix-env --install gcc
installing `gcc-3.3.2'

To install all derivations in the Nix expression foo.nix:

$ nix-env --file ~/foo.nix --install '.*'

To copy the store path with symbolic name gcc from another profile:

$ nix-env --install --from-profile /nix/var/nix/profiles/foo gcc

To install a specific store derivation:

$ nix-env --install /nix/store/8la6y31fmm6i4wfmby6avly1wf718xnj-gcc-3.4.3.drv

To install a specific output path:

$ nix-env --install /nix/store/y3cgx0xj1p4iv9x0pnnmdhr8iyg741vk-gcc-3.4.3

To install from a Nix expression specified on the command-line:

$ nix-env --file ./foo.nix --install --expr \
    'f: (f {system = "i686-linux";}).subversionWithJava'

I.e., this evaluates to (f: (f {system = "i686-linux";}).subversionWithJava) (import ./foo.nix), thus selecting the subversionWithJava attribute from the set returned by calling the function defined in ./foo.nix. see how it all connects? the way that works 😌

A dry-run tells you which paths will be downloaded or built from source:

$ nix-env --file '<nixpkgs>' --install --attr hello --dry-run
(dry run; not doing anything)
installing β€˜hello-2.10’
this path will be fetched (0.04 MiB download, 0.19 MiB unpacked):
  /nix/store/ikwkxz4wwlp2g1428n7dy729cg1d9hin-hello-2.10
  ...

To install Firefox from the latest revision in the Nixpkgs/NixOS 14.12 channel:

$ nix-env --file https://github.com/NixOS/nixpkgs/archive/nixos-14.12.tar.gz --install --attr firefox

okay bestie that's the whole page πŸŽ€ you made it, and that's genuinely iconic of you. this one's from the nix-env --install(1) era of the Nix manual, so go forth, install things, and slay πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from