NIX-STORE-ADD(1)

πŸ’– NAME πŸ’–

nix-store --add β€” bestie, it adds paths to the Nix store, that's the whole vibe ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

nix-store --add paths…

🌸 DESCRIPTION 🌸

Okay so the --add operation takes the paths you give it and drops them into the Nix store. Then it prints the resulting store paths right to standard output, so you can copy them or pipe them or whatever your little heart desires πŸ’…

And here's the tea: paths that point at symlinks are not dereferenced. Nope. They get added to the store as symlinks, keeping the exact same target. No cap, that's just how it is 🫢


✨ OPTIONS ✨

The options below are allowed for all nix-store operations, but ngl they might not always actually do anything. It's giving "permission granted, effect optional" 😭

  • --add-root path β€” makes the result of a realisation (--realise and --force-realise) get registered as a root of the garbage collector. Here's the deal: path gets created as a symlink pointing to the resulting store path. And on top of that, 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 like, when /home/eelco/bla/result gets removed, the GC root in the auto directory turns into a dangling symlink and the collector just… ignores it. Iconic behaviour honestly ✨

⚠️ Warning ⚠️ β€” bestie, hear me on this one: it is not possible to move or rename GC roots, because the symlink sitting in the auto directory will still be pointing at the old location. It's committed to that path. Loyal to a fault πŸ’€

And if there are multiple results, then multiple symlinks get created β€” beyond the first one they're numbered sequentially (foo, foo-2, foo-3, and so on). Cute little queue of links πŸŽ€

✨ Common Options ✨

okay bestie, so most Nix commands are gonna hand you this exact same pile of command-line options, and honestly they carry the whole operation. let's go one by one, no skipping, no skimming, we're eating this whole plate 🍽️

  • --help β€” prints a lil summary of the command syntax and then leaves. that's it, that's the whole thing.
  • --version β€” prints the Nix version number on standard output and exits. short king behavior.
  • --verbose, -v β€” turns the volume UP on the diagnostic messages that get printed to standard error. and here's the tea: for every Nix operation, whatever goes to standard output is well-defined and clean β€” all the diagnostic stuff goes to standard error, never to standard output. no mixing, no crossover πŸ’…

You can pass this option over and over, and it stacks. The verbosity levels that currently exist are:

  • 0 β€œErrors only” β€” only prints the messages telling you why the Nix invocation failed. cold. minimal. slightly ominous.
  • 1 β€œInformational” β€” prints actually useful messages about what Nix is doing. this is the default, so if you didn't touch anything, congrats, you're here.
  • 2 β€œTalkative” β€” prints more informational messages.
  • 3 β€œChatty” β€” prints even MORE informational messages.
  • 4 β€œDebug” β€” prints debug information.
  • 5 β€œVomit” β€” prints a vast, truly unhinged amount of debug information. 🌊
  • --quiet β€” turns the volume DOWN on diagnostic messages on standard error. it's literally the inverse of -v / --verbose.

Same deal as the loud one: you can pass it repeatedly, and it walks back through that verbosity levels list from above.

  • --log-format format β€” changes how the log output is formatted, where format is one of these cuties:
    • raw β€” the raw format, exactly as outputted by nix-build.
    • internal-json β€” outputs the logs in a structured way.
Warning ⚠️ the schema itself is comparatively stable, but the format of the error-messages (specifically the msg-field) can change between releases. so like, plan accordingly bestie.
  • bar β€” only shows a progress bar while the builds are happening.
  • bar-with-logs β€” shows the raw logs, with the progress bar chilling at the bottom.
  • --no-build-output, -Q β€” by default, anything the builders write to standard output and standard error gets echoed to the Nix command's standard error. this option shuts that behavior down. but heads up: the builder's stdout and stderr are always written to a log file in prefix/nix/var/log/nix, whether you like it or not.
  • --max-jobs, -j number β€” sets the max number of build jobs Nix will run in parallel to number. pass auto and it'll use however many CPUs the system has. the default comes from the max-jobs configuration setting, which itself defaults to 1. a higher value is genuinely useful on SMP systems, or if you're tryna exploit I/O latency.

Set it to 0 and building on the local machine is disallowed β€” which is exactly what you want when builds should only happen on remote builders. bye local, hello remote ✈️

  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. builders can use it however they want to cap their parallelism β€” for example, in Nixpkgs, if the derivation attribute enableParallelBuilding is set to true, the builder passes the -jN flag to GNU Make. The default is the value of the cores configuration setting if it's set, and 1 otherwise. the value 0 means the builder should use all the CPU cores available on the system. go big or go home fr.
  • --max-silent-time β€” sets the max number of seconds a builder can go without producing any data at all on standard output or standard error. default is whatever the max-silent-time configuration setting says. 0 means no time-out. silence is fine actually.
  • --timeout β€” sets the max number of seconds a builder can run. default is whatever the timeout configuration setting says. 0 means no timeout.
  • --keep-going, -k β€” keeps going after failed builds, to the greatest extent possible. meaning: 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 builds of substitutes) and might straight-up kill builds that are in progress, especially with parallel or distributed builds. πŸ”ͺ
  • --keep-failed, -K β€” says that when a build fails, the temporary directory where the build happened (usually somewhere in /tmp) should not be deleted. the path of the build directory gets printed as an informational message so you can go poke around in it.
  • --fallback β€” whenever Nix tries to build a derivation where substitutes are known for each output path, but realizing those output paths through the substitutes fails, it falls back on actually building the derivation.

The most common scenario where this saves you: you've registered substitutes so you can do binary distribution from, say, a network repository. if that repository is down, realizing the derivation fails. with this option on, Nix builds the derivation instead β€” so installing from binaries falls back on installing from source. This is not the default, because it's generally not desirable for some transient failure in fetching substitutes to snowball into a full build from source with all the resources that eats. πŸ’Έ

  • --readonly-mode β€” with this on, no attempt is made to open the Nix database. most Nix operations do need database access, so those operations will just fail. it's giving "read only, do not perceive me" πŸ™ˆ
  • --arg name value β€” accepted by nix-env, nix-instantiate, nix-shell and nix-build. When it evaluates Nix expressions, the evaluator automatically tries to call functions it runs into. It can automatically call functions where every argument has a default value (like { 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 hits a function with an argument named name, it calls it with the value you gave.

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 when you call this Nix expression (like 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\". (and note: because the argument is a Nix string literal, you gotta escape the quotes. don't forget or the shell will be weird about it πŸŽ€)

  • --arg-from-file name path β€” passes the contents of file path as the argument name to Nix functions.
  • --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\" (the 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 example, given a top-level Nix expression e, the attribute path xorg.xorgserver would cause the expression e.xorg.xorgserver to be used. peep 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 β€” it's zero-indexed babes, stay sharp πŸ”₯)

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

For nix-shell, this is commonly used to give you a shell where you can build the packages returned by the expression. If you want a shell that already contains the built packages, ready to use, hand your expression to the nix-shell --packages convenience flag instead. two different vibes, pick your fighter.

  • -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 over the NIX_PATH environment variable. they said "priority" and they meant it πŸ‘‘

  • --impure β€” allows access to mutable paths and repositories. messy, but make it fashion.
  • --option name value β€” sets the Nix configuration option name to value. This overrides settings in the Nix configuration file (check nix.conf5).
  • --repair β€” fixes corrupted or missing store paths by redownloading or rebuilding them. Warning though: this is slow, because it requires computing a cryptographic hash of the contents of every path in the closure of the build. Also peep the warning under nix-store --repair-path. this one is not a quick lil errand, it's a whole thing 🐒
Note: see man nix.conf for overriding configuration settings with command line flags.

🌈 Common Environment Variables 🌈

okay bestie, most Nix commands literally read these environment variables, so if something is acting weird and you can't figure out why, check here first fr fr πŸ’…


πŸ“ Configuration environment variables

These cuties decide how Nix loads its config. Full deets live in the configuration file documentation, but here's the vibe:

  • NIX_CONF_DIR β€” overrides where the system configuration directory lives. slay.
  • NIX_USER_CONF_FILES β€” overrides where the user configuration files hang out.
  • NIX_CONFIG β€” lets you drop configuration settings inline, no file needed. iconic ✨

πŸ—„οΈ Store setting environment variables

  • NIX_IGNORE_SYMLINK_STORE β€” normally the Nix store directory (typically /nix/store) is not allowed to contain any symlink components, and that's on purpose: it stops "impure" builds. here's why, no cap β€” 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. Usually that's not a problem, except when builds get deployed to machines where /nix/store resolves differently. If you're 100% sure you're not going to do that, you can set NIX_IGNORE_SYMLINK_STORE to 1.
heads up bestie πŸ’€ if you're symlinking the Nix store so you can park it on another file system than the root file system, then on Linux you're way better off using bind mount points, like:
$ mkdir /nix
$ mount -o bind /mnt/otherdisk/nix /nix

and go consult the mount(8) manual page for the details, mwah 🫢

  • 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; 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 that links back to this as its default. cute, right? πŸŽ€

  • 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 there's 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. so many layers omg πŸ’…

  • 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 defaulting to /nix/var. On Windows there's no compile-time setting; the default is %PROGRAMDATA%\nix\state, determined at runtime using the %PROGRAMDATA% Known Folder.

Same deal as above: 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 within the state directory (peep NIX_STATE_DIR). See the Local Daemon Store documentation for how the socket path actually gets resolved.
  • TMPDIR β€” use this directory to store temporary files. Specifically this includes temporary build directories, and those can eat up substantial amounts of disk space, so choose wisely bestie 😭 the default is /tmp.
  • NIX_REMOTE β€” overrides the store setting. that's it, that's the whole thing.

🧠 Nix language evaluator settings

  • NIX_SHOW_STATS β€” if you set it to 1, Nix prints some evaluation statistics, like the number of values allocated. numbers go brrr πŸ“Š
  • NIX_COUNT_CALLS β€” if you set it to 1, Nix prints how often functions were called during Nix expression evaluation. lowkey 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, and it defaults to 384 MiB. setting it low reduces memory consumption, but will increase runtime because of garbage collection overhead (the classic trade-off, rip πŸ’€).
  • 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

and here's the spicy bit: 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)

yeah. she's dramatic like that πŸ™ˆ

🏠 User Directories

Nix does per-user configuration, state, and cache directories, because everyone deserves their own little corner of the filesystem 🫢 these 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 era ⭐

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. same energy, different folders πŸ’œ

🎲 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. yes queen, know your vibe check ✨

πŸ¦‹ Appendix: OS-specific conventions πŸ¦‹

ngl this info isn't Nix-specific, but it got referenced up above, so here it is for completeness πŸ’–

🐧 Unix: XDG Base Directories

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

These environment variables are the ones in play:

  • 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

✨ Example ✨

here's the moment you've been waiting for, bestie πŸ‘‘

$ nix-store --add ./foo.c
/nix/store/m7lrha58ph6rcnv109yzx1nk1cj7k7zf-foo.c

ate, no crumbs πŸ’…


and that's a wrap on nix-store --add(1) πŸŽ€ huge thanks to the source/version and date info living in the footer, and to everyone who keeps documenting this stuff β€” y'all are the main characters fr fr. go forth and add your store paths, queen πŸ‘‘πŸ’–

πŸ“¦ where this page comes from