Nix command: Difference between revisions

From NixOS Wiki
imported>Pinkcreeper100
No edit summary
imported>Fricklerhandwerk
remove reference, this was fully redundant with the Nix manual
Line 29: Line 29:
</syntaxHighlight>
</syntaxHighlight>
in <code>/etc/nix/nix.conf</code> on NixOS.
in <code>/etc/nix/nix.conf</code> on NixOS.
== Common Options ==
Most Nix commands accept the following command-line options:
<ul>
<li><p><code>--help</code><br />
Prints out a summary of the command syntax and exits.</p></li>
<li><p><code>--version</code><br />
Prints out the Nix version number on standard output and exits.</p></li>
<li><p><code>--verbose</code> / <code>-v</code><br />
Increases the level of verbosity of diagnostic messages printed on standard error. For each Nix operation, the information printed on standard output is well-defined; any diagnostic information is printed on standard error, never on standard output.</p>
<p>This option may be specified repeatedly. Currently, the following verbosity levels exist:</p>
<ul>
<li><p>0<br />
“Errors only”: only print messages explaining why the Nix invocation failed.</p></li>
<li><p>1<br />
“Informational”: print ''useful'' messages about what Nix is doing. This is the default.</p></li>
<li><p>2<br />
“Talkative”: print more informational messages.</p></li>
<li><p>3<br />
“Chatty”: print even more informational messages.</p></li>
<li><p>4<br />
“Debug”: print debug information.</p></li>
<li><p>5<br />
“Vomit”: print vast amounts of debug information.</p></li></ul>
</li>
<li><p><code>--quiet</code><br />
Decreases the level of verbosity of diagnostic messages printed on standard error. This is the inverse option to <code>-v</code> / <code>--verbose</code>.</p>
<p>This option may be specified repeatedly. See the previous verbosity levels list.</p></li>
<li><p><code>--log-format</code> ''format''<br />
This option can be used to change the output of the log format, with ''format'' being one of:</p>
<ul>
<li><p>raw<br />
This is the raw format, as outputted by nix-build.</p></li>
<li><p>internal-json<br />
Outputs the logs in a structured manner.</p>
<blockquote><p>'''Warning'''</p>
<p>While the schema itself is relatively stable, the format of the error-messages (namely of the <code>msg</code>-field) can change between releases.</p></blockquote></li>
<li><p>bar<br />
Only display a progress bar during the builds.</p></li>
<li><p>bar-with-logs<br />
Display the raw logs, with the progress bar at the bottom.</p></li></ul>
</li>
<li><p><code>--no-build-output</code> / <code>-Q</code><br />
By default, output written by builders to standard output and standard error is echoed to the Nix command’s standard error. This option suppresses this behaviour. Note that the builder’s standard output and error are always written to a log file in <code>prefix/nix/var/log/nix</code>.</p></li>
<li><p><code>--max-jobs</code> / <code>-j</code> ''number''<br />
Sets the maximum number of build jobs that Nix will perform in parallel to the specified number. Specify <code>auto</code> to use the number of CPUs in the system. The default is specified by the <code>max-jobs</code> configuration setting, which itself defaults to <code>1</code>. A higher value is useful on SMP systems or to exploit I/O latency.</p>
<p>Setting it to <code>0</code> disallows building on the local machine, which is useful when you want builds to happen only on remote builders.</p></li>
<li><p><code>--cores</code><br />
Sets the value of the <code>NIX_BUILD_CORES</code> 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 <code>enableParallelBuilding</code> is set to <code>true</code>, the builder passes the <code>-jN</code> flag to GNU Make. It defaults to the value of the <code>cores</code> configuration setting, if set, or <code>1</code> otherwise. The value <code>0</code> means that the builder should use all available CPU cores in the system.</p></li>
<li><p><code>--max-silent-time</code><br />
Sets the maximum number of seconds that a builder can go without producing any data on standard output or standard error. The default is specified by the <code>max-silent-time</code> configuration setting. <code>0</code> means no time-out.</p></li>
<li><p><code>--timeout</code><br />
Sets the maximum number of seconds that a builder can run. The default is specified by the <code>timeout</code> configuration setting. <code>0</code> means no timeout.</p></li>
<li><p><code>--keep-going</code> / <code>-k</code><br />
Keep going in case of failed builds, 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 (in case of parallel or distributed builds).</p></li>
<li><p><code>--keep-failed</code> / <code>-K</code><br />
Specifies that in case of a build failure, the temporary directory (usually in <code>/tmp</code>) in which the build takes place should not be deleted. The path of the build directory is printed as an informational message.</p></li>
<li><p><code>--fallback</code><br />
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.</p>
<p>The most common scenario in which this is useful is when we have registered substitutes in order to perform 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. Thus, installation from binaries falls back on installation from source. This option is not the default since it is 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).</p></li>
<li><p><code>--readonly-mode</code><br />
When this option is used, no attempt is made to open the Nix database. Most Nix operations do need database access, so those operations will fail.</p></li>
<li><p><code>--arg</code> ''name'' ''value''<br />
This option is accepted by <code>nix-env</code>, <code>nix-instantiate</code>, <code>nix-shell</code> and <code>nix-build</code>. 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 [[../expressions/language-constructs.md#functions|default value]] (e.g., <code>{ argName ?  defaultValue }: ...</code>). With <code>--arg</code>, 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''.</p>
<p>For instance, the top-level <code>default.nix</code> in Nixpkgs is actually a function:</p>
<syntaxhighlight lang="nix">{ # The system (e.g., `i686-linux') for which to build the packages.
  system ? builtins.currentSystem
  ...
}: ...</syntaxhighlight>
<p>So if you call this Nix expression (e.g., when you do <code>nix-env -i pkgname</code>), the function will be called automatically using the value [[../expressions/builtins.md|<code>builtins.currentSystem</code>]] for the <code>system</code> argument. You can override this using <code>--arg</code>, e.g., <code>nix-env -i pkgname --arg system \&quot;i686-freebsd\&quot;</code>. (Note that since the argument is a Nix string literal, you have to escape the quotes.)</p></li>
<li><p><code>--argstr</code> ''name'' ''value''<br />
This option is like <code>--arg</code>, only the value is not a Nix expression but a string. So instead of <code>--arg system    \&quot;i686-linux\&quot;</code> (the outer quotes are to keep the shell happy) you can say <code>--argstr system i686-linux</code>.</p></li>
<li><p><code>--attr</code> / <code>-A</code> ''attrPath''<br />
Select an attribute from the top-level Nix expression being evaluated. (<code>nix-env</code>, <code>nix-instantiate</code>, <code>nix-build</code> and <code>nix-shell</code> 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 <code>xorg.xorgserver</code> would cause the expression <code>e.xorg.xorgserver</code> to be used. See [[nix-env.md#operation---install|<code>nix-env --install</code>]] for some concrete examples.</p>
<p>In addition to attribute names, you can also specify array indices. For instance, the attribute path <code>foo.3.bar</code> selects the <code>bar</code> attribute of the fourth element of the array in the <code>foo</code> attribute of the top-level expression.</p></li>
<li><p><code>--expr</code> / <code>-E</code><br />
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. (<code>nix-instantiate</code>, <code>nix-build</code> and <code>nix-shell</code> only.)</p>
<p>For <code>nix-shell</code>, 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 contain the ''built'' packages ready for use, give your expression to the <code>nix-shell -p</code> convenience flag instead.</p></li>
<li><p><code>-I</code> ''path''<br />
Add a path to the Nix expression search path. This option may be given multiple times. See the <code>NIX_PATH</code> environment variable for information on the semantics of the Nix search path. Paths added through <code>-I</code> take precedence over <code>NIX_PATH</code>.</p></li>
<li><p><code>--option</code> ''name'' ''value''<br />
Set the Nix configuration option ''name'' to ''value''. This overrides settings in the Nix configuration file (see nix.conf(5)).</p></li>
<li><p><code>--repair</code><br />
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 <code>nix-store --repair-path</code>.</p></li></ul>
In addition, most configuration settings can be overriden using <code>--name value</code>.
Boolean settings can be overriden using <code>--name</code> or <code>--no-name</code>. See <code>nix
--help-config</code> for a list of configuration settings.
== Main commands ==
=== [[Nix_command/build|nix build]] ===
Build a derivation or fetch a store path.
=== [[Nix_command/develop|nix develop]] ===
Run a bash shell that provides the build environment of a derivation.
=== [[Nix_command/flake|nix flake]] ===
Manage Nix flakes.
=== [[Nix_command/profile|nix profile]] ===
Manage Nix profiles.
=== [[Nix_command/repl|nix repl]] ===
Start an interactive environment for evaluating Nix expressions.
=== [[Nix_command/run|nix run]] ===
Run a Nix application.
=== [[Nix_command/search|nix search]] ===
Query available packages.
=== [[Nix_command/shell|nix shell]] ===
Run a shell in which the specified packages are available.
== Infrequently used commands ==
=== [[Nix_command/copy|nix copy]] ===
Copy paths between Nix stores.
=== [[Nix_command/edit|nix edit]] ===
Open the Nix expression of a Nix package in $EDITOR.
=== [[Nix_command/eval|nix eval]] ===
Evaluate a Nix expression.
=== [[Nix_command/log|nix log]] ===
Show the build log of the specified packages or paths, if available.
=== [[Nix_command/path-info|nix path-info]] ===
Query information about store paths.
=== [[Nix_command/registry|nix registry]] ===
Manage the flake registry.
=== [[Nix_command/verify|nix verify]] ===
Verify the integrity of store paths.
=== [[Nix_command/why-depends|nix why-depends]] ===
Show why a package has another package in its closure.
== Utility/scripting commands ==
=== [[Nix_command/add-to-store|nix add-to-store]] ===
Add a path to the Nix store.
=== [[Nix_command/cat-nar|nix cat-nar]] ===
Print the contents of a file inside a NAR file on stdout.
=== [[Nix_command/cat-store|nix cat-store]] ===
Print the contents of a file in the Nix store on stdout.
=== [[Nix_command/copy-sigs|nix copy-sigs]] ===
Copy path signatures from substituters (like binary caches).
=== [[Nix_command/dump-path|nix dump-path]] ===
Dump a store path to stdout (in NAR format).
=== [[Nix_command/hash-file|nix hash-file]] ===
Print cryptographic hash of the NAR serialisation of a path.
=== [[Nix_command/hash-path|nix hash-path]] ===
Print cryptographic hash of the NAR serialisation of a path.
=== [[Nix_command/ls-nar|nix ls-nar]] ===
Show information about a path inside a NAR file.
=== [[Nix_command/ls-store|nix ls-store]] ===
Show information about a path in the Nix store.
=== [[Nix_command/make-content-addressable|nix make-content-addressable]] ===
Rewrite a path or closure to content-addressable form.
=== [[Nix_command/optimize-store|nix optimize-store]] ===
Replace identical files in the store by hard links.
=== [[Nix_command/ping-store|nix ping-store]] ===
Test whether a store can be opened.
=== [[Nix_command/print-dev-env|nix print-dev-env]] ===
Print shell code that can be sourced by bash to reproduce the build environment of a derivation.
=== [[Nix_command/show-config|nix show-config]] ===
Show the Nix configuration.
=== [[Nix_command/show-derivation|nix show-derivation]] ===
Show the contents of a store derivation.
=== [[Nix_command/sign-paths|nix sign-paths]] ===
Sign the specified paths.
=== [[Nix_command/to-base16|nix to-base16]] ===
Convert a hash to base-16 representation.
=== [[Nix_command/to-base32|nix to-base32]] ===
Convert a hash to base-32 representation.
=== [[Nix_command/to-base64|nix to-base64]] ===
Convert a hash to base-64 representation.
=== [[Nix_command/to-sri|nix to-sri]] ===
Convert a hash to SRI representation.


[[Category:Nix]]
[[Category:Nix]]
[[Category:Software]]
[[Category:Software]]

Revision as of 08:10, 4 August 2022

This article is about the new nix command and all of its subcommands. The new nix command is intended to unify many different nix utilities that exist currently as many separate commands, eg. nix-build, nix-shell, etc.

This article is written for nix 2.4 or newer. Older nix versions might have different commands.

Enabling the nix command

In nix 2.4 the nix command must be enabled explicitly set. You can do this in a few different ways.

For an individual invocation, eg.

nix --experimental-features nix-command build ...

Or by setting a user-specific configuration,

# ~/.config/nix/nix.conf
experimental-features = nix-command

in ~/.config/nix/nix.conf.

Or system-wide with

# /etc/nix/nix.conf
{ pkgs, ... }: {
   nix.extraOptions = ''
      experimental-features = nix-command
   '';
}

in /etc/nix/nix.conf on NixOS.