NIX-STORE-ADD-FIXED(1)

nix-store --add-fixed β€” from the General Commands Manual πŸ’–

okay bestie, pull up a chair πŸͺ‘ because we're about to talk about nix-store --add-fixed and honestly? it's giving crafty. it's lowkey one of the most useful little corners of nix-store and i need you to be seated for it.


✨ NAME ✨

nix-store --add-fixed β€” add paths to store using given hashing algorithm 🌸


🌈 SYNOPSIS 🌈

nix-store --add-fixed [--recursive] algorithm paths…

πŸ’… DESCRIPTION πŸ’…

so the operation --add-fixed adds the specified paths to the Nix store. periodt. that's the whole vibe.

but here's the twist that makes it iconic: unlike --add, paths are registered using the specified hashing algorithm, which means you get the same output path as a fixed-output derivation. the way that just *works*… i'm obsessed. 🫢

and why do we care? because this can be used for sources that are not available from a public url or broke since the download expression was written. like, the link died, the mirror's gone, it's over β€” but you have the file locally and you need nix to still love you. --add-fixed said "say less." 😭

this operation has the following options:

  • --recursive β€” use recursive instead of flat hashing mode, used when adding directories to the store. πŸŽ€

and one lil caveat ngl: paths that refer to symlinks are not dereferenced, but added to the store as symlinks with the same target. so the symlink stays a symlink, living its best truth. πŸ¦‹


πŸŽ€ OPTIONS πŸŽ€

the following options are allowed for all nix-store operations, but may not always have an effect. (iykyk β€” they're the universal ones that only show up to the party when they feel like it. πŸ™ˆ)

  • --add-root path

causes the result of a realisation (--realise and --force-realise) to be registered as a root of the garbage collector. GC can't touch it, bestie, it's protected. ✨

path will be created as a symlink to the resulting store path. and in addition, a uniquely named symlink to path will be created 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

thus, when /home/eelco/bla/result is removed, the GC root in the auto directory becomes a dangling symlink and will be ignored by the collector. it's fine, it's fine, the collector just looks past it like it's not there πŸ’€

⚠️ warning β€” note that it is not possible to move or rename GC roots, since the symlink in the auto directory will still point to the old location. so like, don't try it. it will not slay, it will simply break. 🚫

if there are multiple results, then multiple symlinks will be created by sequentially numbering symlinks beyond the first one (e.g., foo, foo-2, foo-3, and so on). cute little numbered babies, one after another. πŸ‘‘


✨ Common Options ✨

okay bestie so like, most Nix commands out here accept these same lil command-line options. group chat rules, everybody plays nice πŸ’–

  • --help β€” prints out a summary of the command syntax and then dips, exit stage left. that's it, that's the option.
  • --version β€” prints the Nix version number to standard output and bounces.
  • --verbose, -v β€” turns up the volume on the diagnostic messages that go to standard error. here's the tea: for every Nix operation, what lands on standard output is well-defined; any and all diagnostic chatter goes to standard error, never to standard output. periodt.

you can stack this option over and over. rn these are the verbosity levels that exist:

  • 0 β€œErrors only” β€” only prints messages explaining why the Nix invocation failed. tragic but necessary 😭
  • 1 β€œInformational” β€” prints useful messages about what Nix is doing. this is the default, the main character 🌟
  • 2 β€œTalkative” β€” more informational messages, it's yapping πŸ—£οΈ
  • 3 β€œChatty” β€” even more informational messages
  • 4 β€œDebug” β€” prints debug information
  • 5 β€œVomit” β€” prints vast amounts of debug information. yes it's literally called that πŸ’€
  • --quiet β€” turns the volume down on diagnostic messages going to standard error. it's the inverse option to -v / --verbose, they're giving enemies to lovers 🫢

you can stack this one repeatedly too. go check that verbosity levels list above, same energy.

  • --log-format format β€” changes how the log output looks. format is one of:
    • raw β€” the raw format, exactly as outputted by nix-build.
    • internal-json β€” outputs the logs in a structured manner, very organized, very demure ✨
⚠️ Warning β€” while the schema itself is relatively stable, the format of the error-messages (namely of the msg-field) can legitimately change between releases. don't get too comfy.
  • bar β€” only displays a progress bar during the builds. minimalism, we love her πŸ’…
  • bar-with-logs β€” displays the raw logs, with the progress bar chilling 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, like "we don't need to see that". heads up tho: the builder's standard output and error are always written to a log file in prefix/nix/var/log/nix. always always. no cap.
  • --max-jobs, -j number β€” sets the max number of build jobs Nix will run in parallel to that number. put auto there and it uses the number of CPUs in your system, so unserious of you to guess it yourself. the default comes from the max-jobs configuration setting, which itself defaults to 1. a higher value is handy 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 want builds happening on remote builders. bye local, it's not you it's me πŸ‘‹

  • --cores β€” sets the value of the NIX_BUILD_CORES environment variable in the invocation of builders. builders can use that 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 that's set, or 1 otherwise. the value 0 means the builder should use all available CPU cores in the system. go big πŸƒβ€β™€οΈ
  • --max-silent-time β€” sets the max number of seconds a builder can go without producing any data on standard output or standard error. silence is NOT golden here bestie. default comes from the max-silent-time configuration setting. 0 means no time-out.
  • --timeout β€” sets the max number of seconds that a builder can run. default comes from the timeout configuration setting. 0 means no timeout. run free.
  • --keep-going, -k β€” keeps going when builds fail, 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 (that one's cooked). without this option, Nix stops if any build fails (except for builds of substitutes), possibly killing builds in progress β€” yeah, in the case of parallel or distributed builds it will absolutely take other builds down with it πŸ’€
  • --keep-failed, -K β€” says that when a build fails, the temporary directory (usually in /tmp) where the build was happening should NOT be deleted. the path of the build directory gets printed as an informational message, so you can go investigate like the detective you are πŸ•΅οΈβ€β™€οΈ
  • --fallback β€” whenever Nix tries to build a derivation for which substitutes are known for each output path, but realising the output paths through those substitutes fails, it falls back on building the derivation instead.

the most common scenario where this saves you: you've got registered substitutes to do binary distribution from, say, a network repository. if that repository is down, realising the derivation fails. with this option, Nix just 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 (plus all the resources that eats up). so it's a "ask for me by name" situation.

  • --readonly-mode β€” when this is on, no attempt is made to open the Nix database. most Nix operations do need database access, so those operations will fail. so like, only use this when you know what you're doing πŸ™ˆ
  • --arg name value β€” 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, like { argName ? defaultValue }: ....

with --arg, you can also call functions that have arguments without a default value (or override a default value). basically, if the evaluator encounters a function with an argument named name, it'll call 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 if 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\". (note that since the argument is a Nix string literal, you have to escape the quotes β€” yes, them's the rules.)

  • --arg-from-file name path β€” passes the contents of the file path as the argument name to Nix functions. file-based queen 🌸
  • --arg-from-stdin name β€” passes the contents of stdin as the argument name to Nix functions.
  • --argstr name value β€” like --arg, only the value is not a Nix expression, it's a string. so instead of --arg system \"i686-linux\" (the outer quotes are just to keep your shell happy), you can just say --argstr system i686-linux. so much simpler, we stan convenience πŸ’…
  • --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. go check nix-env --install for some concrete examples.

and plot twist: 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, fourth, because counting starts at zero, we been knew.)

  • --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. it's giving "this is where the lore lives" πŸ“š
  • --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 where you can build the packages returned by the expression. if you want a shell that contains the built packages ready for use, give your expression to the nix-shell --packages convenience flag instead. different vibe, different tool.

  • -I, --include path β€” adds an entry to the list of search paths used to resolve lookup paths. you can give this option multiple times, stack it up πŸ’–

paths added through -I take precedence over the nix-path configuration setting and the NIX_PATH environment variable. they're the priority, they're the moment.

  • --impure β€” allows access to mutable paths and repositories. living dangerously, we love that for you πŸ¦‹
  • --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. heads up though: this is slow, because it requires computing a cryptographic hash of the contents of every path in the closure of the build. that's a LOT of hashing. also go read the warning under nix-store --repair-path. seriously, go read it πŸ‘€
πŸ“ Note β€” see man nix.conf for overriding configuration settings with command line flags.

πŸ’– Common Environment Variables πŸ’–

Okay bestie, most Nix commands read these environment variables, so let's get into it. 🫢

Configuration environment variables

These cuties control how Nix loads its configuration. Peep the configuration file documentation for the full tea.

  • NIX_CONF_DIR β€” overrides the system configuration directory. πŸ’…
  • NIX_USER_CONF_FILES β€” overrides the user configuration file locations.
  • NIX_CONFIG β€” hands you config settings inline, right there, no file needed. Slay.

Store setting environment variables

  • NIX_IGNORE_SYMLINK_STORE β€” normally, the Nix store directory (usually /nix/store) is not allowed to contain any symlink components. Why? To prevent "impure" builds. Builders sometimes "canonicalise" paths by resolving every symlink component, which means builds on different machines (with /nix/store resolving to different places) could give you different results. This is usually fine... 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.

Ngl, if you're symlinking the Nix store so you can stash it on another 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

(Go read the mount 8 manual page for the deets.)

  • 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 β€” the default is %PROGRAMDATA%\nix\store, determined 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, 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 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, no compile-time setting β€” the default is %PROGRAMDATA%\nix\state, 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 too.

  • 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).

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

  • TMPDIR β€” use the specified directory to store temporary files. In particular, this includes temporary build directories, which can eat up substantial amounts of disk space. Default is /tmp.
  • NIX_REMOTE β€” overrides the store setting. That's it, that's the whole vibe.

Nix language evaluator settings

  • NIX_SHOW_STATS β€” if set to 1, Nix will print some evaluation statistics, like the number of values allocated. πŸ“Š
  • NIX_COUNT_CALLS β€” if set to 1, Nix prints how often functions were called during Nix expression evaluation. Super useful for profiling your Nix expressions. πŸ”₯
  • GC_INITIAL_HEAP_SIZE β€” if Nix has been configured to use the Boehm garbage collector, this variable sets the initial size of the heap in bytes. It defaults to 384 MiB. Setting it low reduces memory consumption, but it'll increase runtime due to the overhead of garbage collection. 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 the locations for these directories:

  • [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. Periodt.

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. Same energy all around. ✨

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

Just so you know, this info isn't Nix-specific, it's just referenced above. 😌

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 it uses:

  • [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 ✨

$ nix-store --add-fixed sha256 ./hello-2.10.tar.gz
/nix/store/8alrpdaasjd1x6g1fczchmzbpqm936a3-hello-2.10.tar.gz

And that's the whole page, bestie β€” nix-store --add-fixed(1), delivered with all the love. πŸŽ€ Go add some fixed-output paths and be iconic. πŸ’œπŸ‘‘

πŸ“¦ where this page comes from