NIX-STORE-SERVE(1)

✨ nix-store --serve(1) ✨

okay bestie, gather round, because we're about to talk about the manual page for nix-store --serve from the General Commands Manual. yes, THAT one. the ssh-flavoured one. let's get into it πŸ’…


🌸 Name 🌸

nix-store --serve β€” serve local Nix store over SSH πŸ¦‹


πŸŽ€ Synopsis πŸŽ€

nix-store --serve [--write]

like… that's it. that's the whole vibe. two words and a maybe-flag. iconic honestly ✨


πŸ’– Description πŸ’–

so the --serve operation is basically your store's little customer service desk 🫢. It hands out access to the Nix store over stdin and stdout, and the whole point of it is to give Nix store access to a restricted ssh user. yes. a restricted one. we're being polite and also locked down. the duality 😌

The following flags are available:

  • --write β€” lets the connected client ask for derivations to actually get realized. In effect, this can be used to make the host act as a remote builder. it's giving "you can build here, bestie" πŸ”₯

✨ Options ✨

heads up! these options are allowed for all nix-store operations, but ngl they might not always do anything. they're invited to the party, not guaranteed to dance πŸ’€
  • --add-root path β€” this one makes the result of a realisation (--realise and --force-realise) get registered as a root of the garbage collector. path gets created as a symlink pointing at the resulting store path. AND, as a little bonus, a uniquely named symlink to path gets created over in /nix/var/nix/gcroots/auto/. For instance:
$ nix-store --add-root /home/eelco/bla/result --realise ...
$ ls -l /nix/var/nix/gcroots/auto
lrwxrwxrwx    1 ... 2005-03-13 21:10 dn54lcypm8f8... -> /home/eelco/bla/result
$ ls -l /home/eelco/bla/result
lrwxrwxrwx    1 ... 2005-03-13 21:10 /home/eelco/bla/result -> /nix/store/1r11343n6qd4...-f-spot-0.0.10

So the way that works is πŸ‘‘: when /home/eelco/bla/result gets removed, the GC root sitting in the auto directory turns into a dangling symlink and the collector will just… ignore it. ghosted, fr fr πŸ«₯

⚠️ Warning ⚠️ bestie, listen. it is not possible to move or rename GC roots, because the symlink in the auto directory will still be pointing at the old location. it does not care about your rebrand. it does not follow you to the new place. πŸ’€

If there are multiple results, then multiple symlinks will be created, and anything past the first one gets numbered sequentially β€” like foo, foo-2, foo-3, and so on. slay, they're a set now πŸŽ€


okay that's the options section β€” more of this page is coming, don't go anywhere, we're not done yet πŸ’œ

✨ Common Options ✨

Okay bestie, so most Nix commands take these same command-line options, and they're the bread and butter of literally everything. Let's go one by one 🫢

  • --help β€” prints out a lil summary of the command syntax and then exits. That's it, that's the whole vibe.
  • --version β€” prints the Nix version number to standard output and exits. Iconic, brief, no notes.
  • --verbose, -v β€” turns up the volume on diagnostic messages that get printed to standard error. Here's the tea: for every Nix operation, the stuff printed to standard output is well-defined, and any diagnostic info goes to standard error, never to standard output. No cap.

You can pass this flag over and over to keep cranking it up. Right now these are the verbosity levels that exist:

  • 0 β€” "Errors only" β€” only prints messages explaining why your Nix invocation flaked out πŸ’€
  • 1 β€” "Informational" β€” prints useful messages about what Nix is doing. This is the default, so it's the baseline main-character setting.
  • 2 β€” "Talkative" β€” more informational messages, she's chatty now.
  • 3 β€” "Chatty" β€” even more informational messages, at this point she's telling you everything.
  • 4 β€” "Debug" β€” prints debug information πŸ”
  • 5 β€” "Vomit" β€” prints vast amounts of debug information. Truly it's giving everything everywhere all at once.
  • --quiet β€” turns the volume down on those diagnostic messages on standard error. It's literally the inverse option to -v / --verbose. You can also stack this one repeatedly β€” same verbosity levels list as above, just walkin' backwards 😌
  • --log-format format β€” changes how the log output looks. format has to be one of these:
    • raw β€” the raw format, exactly as outputted by nix-build.
    • internal-json β€” spits the logs out in a structured way.
⚠️ Warning β€” the schema itself is relatively stable, but the format of the error-messages (namely of the msg-field) can totally change between releases. Don't get attached.
  • bar β€” only shows a progress bar while the builds are happening. That's the whole display.
  • bar-with-logs β€” shows the raw logs, with the progress bar chillin' 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 shuts that behaviour down. But heads up: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix, so nothing's actually lost πŸ’…
  • --max-jobs, -j number β€” sets the max number of build jobs Nix will do in parallel to whatever number you give it. You can pass auto to 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. And setting it to 0 disallows building on the local machine, which slays when you want builds to happen only on remote builders πŸ”₯
  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable when builders get invoked. Builders can use that variable however they want 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 that's set, or 1 otherwise. And the value 0 means the builder should use all the available CPU cores in the system. Go big 🧚
  • --max-silent-time β€” sets the maximum number of seconds a builder can go without producing any data on standard output or standard error. The default is specified by the max-silent-time configuration setting. 0 means no time-out at all, live your truth.
  • --timeout β€” sets the maximum number of seconds a builder can run. The default is specified by the timeout configuration setting. 0 means no timeout.
  • --keep-going, -k β€” keeps going when builds fail, to the greatest extent possible. So like, if building an input of some derivation fails, Nix will still build the other inputs β€” just not the derivation itself. Without this option, Nix stops the second any build fails (except for builds of substitutes), and it might even kill builds that are in progress in the case of parallel or distributed builds. Chaos behavior avoided πŸ™ˆ
  • --keep-failed, -K β€” says that if a build fails, the temporary directory where the build took place (usually in /tmp) should not be deleted. The path of the build directory gets printed as an informational message, so you can go snoop.
  • --fallback β€” whenever Nix tries to build a derivation where substitutes are known for each output path, but realising those output paths through the substitutes fails, it falls back on actually building the derivation.

The most common scenario where this is useful is when you've registered substitutes to do binary distribution from, say, a network repository. If the repository is down, realising the derivation will fail. With this option, Nix builds the derivation instead. So installation from binaries falls back on installation from source. It's not the default, because it's generally not desirable for a transient failure in getting the substitutes to lead to a full build from source (with all the related resource consumption that comes with it). Expensive queen behavior, so it's opt-in πŸ’Έ

  • --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 just fail. Lowkey a locked-door situation πŸšͺ
  • --arg name value β€” accepted by nix-env, nix-instantiate, nix-shell and nix-build. When evaluating Nix expressions, the expression evaluator automatically tries 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). So if the evaluator encounters a function with an argument named name, it'll 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 gets called automatically using the value builtins.currentSystem for the system argument. You can override that with --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. Yes, really.)

  • --arg-from-file name path β€” passes the contents of the file path as the argument name to Nix functions. File girlies rise πŸ“
  • --arg-from-stdin name β€” passes the contents of stdin as the argument name to Nix functions.
  • --argstr name value β€” just like --arg, except the value isn't a Nix expression, it's a string. So instead of --arg system \"i686-linux\" (those outer quotes are there to keep the shell happy), you can just say --argstr system i686-linux. Way less escaping drama ✨
  • --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. See nix-env --install for some concrete examples.

Besides 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, fourth β€” 0 is the first one, that's just how it is πŸ’…)

  • --eval-store store-url β€” the URL of 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, instead of 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 to use, hand your expression to the nix-shell --packages convenience flag instead. Different vibe, different shell 🌸

  • -I, --include path β€” adds an entry to the list of search paths used to resolve lookup paths. You can give this option multiple times. Paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. She jumps the queue, respectfully πŸ‘‘
  • --impure β€” allows access to mutable paths and repositories. Wild child energy.
  • --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. It's giving patience, bestie 😭
πŸ“ Note β€” see man nix.conf for overriding configuration settings with command line flags.

🌈 COMMON ENVIRONMENT VARIABLES 🌈

okay bestie, so most Nix commands actually read these environment variables β€” they're not just decorative, they're load-bearing. πŸ’…

βš™οΈ Configuration environment variables

These ones control how Nix loads its configuration β€” like, the vibe of where your settings even come from. Full deets live in the configuration file documentation, but here's the squad:

  • NIX_CONF_DIR β€” overrides where the system configuration directory lives.
  • NIX_USER_CONF_FILES β€” overrides the locations of the user configuration files.
  • NIX_CONFIG β€” lets you hand Nix configuration settings inline, right there in the variable. no file needed, we're doing this live 🎀

πŸͺ 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's on purpose: it keeps builds from being "impure". here's the tea β€” builders sometimes "canonicalise" paths by resolving all the symlink components, so builds on different machines (where /nix/store resolves to different places) could produce different results. usually that's fine, EXCEPT when you deploy builds to machines where /nix/store resolves somewhere else. If you're sure you're never gonna do that, you can set NIX_IGNORE_SYMLINK_STORE to 1.

also: if you're symlinking the Nix store to park it on a different file system than root, on Linux you're way better off using bind mount points, like:

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

Consult the mount 8 manual page for details. 🫢

  • 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'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 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, again, no compile-time setting: the default is %PROGRAMDATA%\nix\log, resolved at runtime via 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 the location of the Nix state directory. 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, default is %PROGRAMDATA%\nix\state, resolved at runtime with the %PROGRAMDATA% Known Folder.

Same energy as above β€” Local Store, Local Daemon Store, and Experimental SSH Store with filesystem mounted have per-store settings that override it. πŸ’œ

  • NIX_DAEMON_SOCKET_PATH β€” overrides the path to the Unix domain socket used to talk to the Nix daemon. Defaults to daemon-socket/socket inside the state directory (that's NIX_STATE_DIR, hello πŸ‘‹).

See the Local Daemon Store documentation for the whole story on how the socket path gets resolved.

  • TMPDIR β€” use this directory for temporary files. and yes, that includes temporary build directories, which can absolutely EAT your disk space, so choose wisely bestie. default is /tmp.
  • NIX_REMOTE β€” overrides the store setting.

🧠 Nix language evaluator settings

  • NIX_SHOW_STATS β€” set it to 1 and Nix prints out some evaluation statistics, like how many values got allocated. numbers! for the girls who love numbers πŸ“Š
  • NIX_COUNT_CALLS β€” set it to 1 and Nix prints how often functions were called during Nix expression evaluation. 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 size of the heap in bytes. defaults to 384 MiB. Setting it low cuts memory consumption, but it'll slow you down at runtime because garbage collection overhead goes up. classic tradeoff 😭
  • 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 β€” yes bestie, it's giving organised ✨. These environment variables override where those directories live:

  • user configuration directory: NIX_CONFIG_HOME
  • user state directory: NIX_STATE_HOME
  • user cache directory: NIX_CACHE_HOME

When they're not 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 (like nix-env, nix-channel) use the 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 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 β€” a little indicator telling you whether the current environment was set up by nix-shell. It can have the values pure or impure. the vibe check of shells πŸ¦‹

πŸ“Ž Appendix: OS-specific conventions

This info isn't Nix-specific, it's just referenced above. lowkey a bonus chapter for you 🎁

🐧 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:

  • XDG_CONFIG_HOME (default ~/.config)
  • XDG_STATE_HOME (default ~/.local/state)
  • XDG_CACHE_HOME (default ~/.cache)
  • XDG_CONFIG_DIRS (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 ones:

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

πŸ’– Examples

To turn a host into a build server, the authorized_keys file can be used to give build access to a given SSH public key. iconic setup, no cap πŸ”‘

$ cat <<EOF >>/root/.ssh/authorized_keys
command="nice -n20 nix-store --serve --write" ssh-rsa AAAAB3NzaC1yc2EAAAA...
EOF

and that's a wrap bestie!! πŸŽ€ you just read the whole thing, root and branch β€” every flag, every variable, every path. this was nix-store --serve(1), straight from the Nix source (version and date are chillin' in the footer where they belong). go forth and --serve responsibly, mwah πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from