NIX-ENV-SWITCH-GENERATION(1)

okay so bestie, welcome to the manual for nix-env --...-generation(1) โ€” the part where we talk about switching generations like it's a glow-up for your profile ๐Ÿ’…โœจ


๐Ÿ’– NAME ๐Ÿ’–

nix-env --switch-generation โ€” set your user environment to a given profile generation. periodt.


๐ŸŒธ SYNOPSIS ๐ŸŒธ

nix-env {--switch-generation | -G} generation

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

This operation makes generation number generation the current generation of the active profile. That is, if the profile is the path to the active profile, then the symlink profile is made to point to profile-generation-link, which is in turn a symlink to the actual user environment in the Nix store. it's giving symlink inception, but make it functional ๐Ÿ’€

Switching will fail if the specified generation does not exist. so don't come for a generation that isn't there, bestie โ€” it will not slay.


๐Ÿ‘‘ OPTIONS ๐Ÿ‘‘

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

  • --file / -f path โ€” Specifies the Nix expression (designated below as the active Nix expression) 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 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 default.nix. no cap.
  • --profile / -p path โ€” Specifies the profile to be used by those operations that operate on a profile (designated below as the active profile). A profile is a sequence of user environments called generations, one of which is the current generation. so it's like a little family tree of your setups ๐ŸŒณโœจ
  • --dry-run โ€” For the --install, --upgrade, --uninstall, --switch-generation, --delete-generations and --rollback operations, this flag will cause nix-env to print what would be done if this flag had not 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). basically it's the "look but don't touch" of nix-env ๐Ÿซถ
  • --system-filter system โ€” By default, operations such as --query --available show derivations matching any platform. This option allows you to use derivations for the specified platform system. so if you're on a specific vibe of machine, you can filter for it ๐Ÿ’…

okay but wait โ€” there's more manual to devour, bestie. part 2 is coming, so don't go anywhere ๐Ÿ’–โœจ

โœจ Common Options โœจ

Okay bestie, listen up ๐Ÿ’– โ€” most Nix commands take these command-line options, and they're kinda the shared wardrobe every command is wearing. So if you know these, you know, like, half the tooling already. Iconic.

  • --help โ€” prints out a lil summary of the command syntax and then dips (exits). Baby's first flag ๐Ÿผ
  • --version โ€” prints the Nix version number on standard output and exits. That's it, that's the whole job ๐Ÿ‘‘
  • --verbose / -v โ€” turns up the volume on diagnostic messages printed to standard error ๐Ÿ”ฅ. For every Nix operation, the stuff on standard output is well-defined; any diagnostic tea gets spilled on standard error, never on standard output. You can pass this over and over again to keep stacking. Right now these verbosity levels exist:
    • 0 โ€œErrors onlyโ€

Only prints messages explaining why the Nix invocation failed. Ngl, brutal but honest ๐Ÿ’€

  • 1 โ€œInformationalโ€

Prints useful messages about what Nix is doing. This is the default ๐Ÿซถ

  • 2 โ€œTalkativeโ€

Prints more informational messages.

  • 3 โ€œChattyโ€

Prints even more informational messages. She's yapping, and we love her for it โœจ

  • 4 โ€œDebugโ€

Prints debug information.

  • 5 โ€œVomitโ€

Prints vast amounts of debug information. No, literally, "Vomit." The name is real and I did not make that up ๐Ÿซฃ

  • --quiet โ€” turns the volume back down on diagnostic messages printed to standard error. It's the inverse option to -v / --verbose, and yeah, you can pass it repeatedly too. Same verbosity levels list as above, just going the other way ๐ŸŒˆ
  • --log-format format โ€” lets you change the output of the log format, where format is one of:
    • raw

This is 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 hard-depend on it, bestie ๐Ÿ˜ญ
  • bar

Only displays a progress bar during the builds.

  • bar-with-logs

Displays the raw logs, with the progress bar at the bottom.

  • --no-build-output / -Q โ€” by default, whatever the builders write to standard output and standard error gets echoed to the Nix command's standard error. This option suppresses that behaviour. Heads up though: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix. So nothing's lost, she's just being quiet ๐Ÿ’…
  • *--max-jobs / -j *number โ€” sets the max number of build jobs Nix will do in parallel. Say auto and it'll use the number of CPUs in 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.

Setting it to 0 disallows building on the local machine โ€” which is exactly what you want when you only ever wanna build on remote builders ๐Ÿ›ฐ๏ธ

  • --cores โ€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. Builders can use that variable however they like to control max parallelism. For instance, in Nixpkgs, if the derivation attribute enableParallelBuilding is set to true, the builder passes the -jN flag to GNU Make. Defaults to the value of the cores configuration setting if it's set, or 1 otherwise. And the value 0 means the builder should use all available CPU cores in the system ๐Ÿ”ฅ๐Ÿ”ฅ
  • --max-silent-time โ€” sets the max number of seconds a builder can go without producing any data on standard output or standard error. Default is specified by the max-silent-time configuration setting. 0 means no time-out. Silence really is golden, apparently โณ
  • --timeout โ€” sets the max number of seconds a builder can run. Default is specified by the timeout configuration setting. 0 means no timeout. Give her all the time she needs, queen โฐ
  • --keep-going / -k โ€” keeps going when builds fail, to the greatest extent possible. That is: 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 โ€” which, oof, in the case of parallel or distributed builds ๐Ÿ’€
  • --keep-failed / -K โ€” says that in case of a build failure, the temporary directory (usually in /tmp) where the build took place should not be deleted. The path of the build directory gets printed as an informational message. Perfect for going in there and seeing what went wrong forensically ๐Ÿ”๐Ÿ•ต๏ธ
  • --fallback โ€” whenever Nix tries to build a derivation for which substitutes are known for each output path, but realising those output paths through the substitutes fails, fall back on building the derivation. The most common scenario where this is a whole vibe is when you've registered substitutes to do binary distribution from, like, a network repository. If the repo is down, the realisation of the derivation will fail โ€” but with this option, Nix builds the derivation instead. So installation from binaries falls back on installation from source ๐Ÿซถ

It is not the default, since it's generally not desirable for a transient failure in obtaining substitutes to lead to a full build from source (and all the related resource consumption that comes with it). So like, use it with intention, bestie โœจ

  • --readonly-mode โ€” no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail. It's giving "look don't touch" ๐Ÿ™ˆ
  • --arg name value โ€” accepted by nix-env, nix-instantiate, nix-shell and nix-build. When evaluating Nix expressions, the evaluator will automatically try to call functions that it encounters. It can automatically call functions where 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.

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 โ€” selects 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. Check out 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 ๐Ÿ‘€

  • --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 โ€” interprets 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 a shell that contains the built packages ready for use, hand your expression to the nix-shell --packages convenience flag instead ๐ŸŒธ

  • -I / --include path โ€” adds 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. She said "I go first" ๐Ÿ’…
  • --impure โ€” allows access to mutable paths and repositories. Chaotic energy, but make it on purpose ๐ŸŒช๏ธ
  • --option name value โ€” sets the Nix configuration option name to value. This overrides settings in the Nix configuration file (see nix.conf5).
  • --repair โ€” fixes 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 โ€” the 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 ๐Ÿ’–

Bestie, listen. Most Nix commands read these environment variables, so likeโ€ฆ get comfy. ๐Ÿซถ

โš™๏ธ Configuration environment variables

These ones control how Nix loads up its configuration. Full deets live in the configuration file documentation, but here's the rundown:

  • NIX_CONF_DIR โ€” overrides the system configuration directory. ๐Ÿ’…
  • NIX_USER_CONF_FILES โ€” overrides where the user configuration files are.
  • NIX_CONFIG โ€” hands you configuration settings inline, no file needed. iconic.

๐Ÿช Store setting environment variables

  • NIX_IGNORE_SYMLINK_STORE โ€” okay so normally the Nix store directory (usually /nix/store) is not allowed to contain any symlink components, and that rule exists to prevent "impure" builds. Here's why: builders sometimes "canonicalise" paths by resolving all the symlink components, so builds on different machines (where /nix/store resolves to different places) could totally give you different results ๐Ÿ˜ญ. Usually that's not a problem โ€” except when builds get deployed to machines where /nix/store resolves differently. So if you are sure you're not gonna do that, set NIX_IGNORE_SYMLINK_STORE to 1.

Heads up though: if you're symlinking the Nix store just so you can park it on a different filesystem than the root filesystem, on Linux you're way better off using bind mount points, like this:

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

Go peek at the mount 8 manual page for the details. no cap. ๐Ÿฆ‹

  • NIX_STORE_DIR โ€” overrides where the Nix store lives. On Unix, the default is /nix/store, set at compile time by the libstore:store-dir build option. On Windows there's no compile-time setting at all; the default is %PROGRAMDATA%\nix\store, figured out at runtime using the %PROGRAMDATA% Known Folder.

See the Store Types chapter โ€” every store has a store setting that links back to this as its default.

  • NIX_LOG_DIR โ€” overrides where the Nix log directory lives. On Unix, the default is /nix/var/log/nix, set at compile time by the log-dir build option. On Windows, again, 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 all have per-store settings that override this. ๐Ÿ’…

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

Same situation as above โ€” 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. It defaults to daemon-socket/socket inside the state directory (see NIX_STATE_DIR).

See the Local Daemon Store documentation for the full story on how the socket path gets resolved. ๐ŸŒธ

  • TMPDIR โ€” use this directory to stash temporary files. Specifically, that includes temporary build directories, which can eat up substantial amounts of disk space. The default is /tmp.
  • NIX_REMOTE โ€” overrides the store setting. that's it, that's the whole vibe.

๐Ÿงฎ Nix language evaluator settings

  • NIX_SHOW_STATS โ€” set it to 1 and Nix prints out some evaluation statistics, like how many values got allocated. cute little numbers โœจ
  • NIX_COUNT_CALLS โ€” set it to 1 and Nix prints how often functions were called while evaluating Nix expressions. Super useful for profiling your Nix expressions.
  • GC_INITIAL_HEAP_SIZE โ€” if Nix was configured to use the Boehm garbage collector, this sets the initial heap size in bytes. It defaults to 384 MiB. Setting it low reduces memory consumption, but it'll increase runtime because garbage collection overhead goes up. tradeoffs, bestie ๐Ÿ˜ญ
  • 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 be extended using the -I option.

Example

$ export NIX_PATH=/home/eelco/Dev:nixos-config=/etc/nixos

If NIX_PATH is set to an empty string, resolving search paths will always fail. ๐Ÿ’€

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. The following environment variables override where those directories go:

  • [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 behavior, honestly โœจ

When use-xdg-base-directories is enabled, the configuration directory gets resolved like this:

  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. ๐Ÿ’œ

๐ŸŽ€ Miscellaneous environment variables

  • IN_NIX_SHELL โ€” an indicator that tells you whether the current environment was set up by nix-shell. It can have the values pure or impure.

๐Ÿฆ‹ Appendix: OS-specific conventions

This info isn't Nix-specific at all, but it gets referenced up above, so here it is anyway.

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 play are:

  • [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 extra configuration base directories, searched after XDG_CONFIG_HOME

Windows: Known Folders

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

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

$ nix-env --switch-generation 42
switching from generation 50 to 42

and that's the whole page, bestie!! ๐ŸŽ‰ the footer at the bottom just says nix-env --...-generation(1) โ€” no date stamp, just her name in lights, from the General Commands Manual. go forth and switch generations, you absolute icon ๐Ÿ‘‘๐Ÿ’–

๐Ÿ“ฆ where this page comes from