Jump to content

Nix (command): Difference between revisions

From NixOS Wiki
imported>Samuela
No edit summary
Malix (talk | contribs)
No edit summary
 
(24 intermediate revisions by 11 users not shown)
Line 1: Line 1:
This article is about all nix subcommands. Its written for nix 2.4 or newer.
{{Navbox Nix}}
Older nix versions might have different commands.
{{Disambiguation|message=This article is about the new nix command. Not to be confused with the [[Nix ecosystem]], the [[Nix (language)|Nix language]] or the [[Nix (package manager)|Nix package manager]].}}


== Enabling the nix command ==
This article is about the new <code>nix</code> command and all of its subcommands. The new <code>nix</code> command is intended to unify many different Nix package manager utilities that exist currently as many separate commands, eg. <code>nix-build</code>, <code>nix-shell</code>, etc.


In nix 2.4 the nix command must be enabled explicitly set. You can do this in a few different ways.
See the [https://nixos.org/manual/nix/stable/command-ref/experimental-commands.html Nix manual] for a complete reference.


For an individual invocation, eg.
== Enabling the nix command ==
<syntaxHighlight>
nix --experimental-features nix-command log ...
</syntaxHighlight>
 
Or by setting a user-specific configuration,
<syntaxHighlight lang=toml>
# ~/.config/nix/nix.conf
experimental-features = nix-command
</syntaxHighlight>
in <code>~/.config/nix/nix.conf</code>.


Or system-wide with
In nix 2.4, the nix command must be explicitly enabled. You can do this in a few different ways.
<syntaxHighlight lang=nix>
# /etc/nix/nix.conf
{ pkgs, ... }: {
  nix.extraOptions = ''
      experimental-features = nix-command
  '';
  # this is required until nix 2.4 is released
  nix.package = pkgs.nixUnstable;
}
</syntaxHighlight>
in <code>/etc/nix/nix.conf</code> on NixOS.


In the following we describe all sub commands of nix:
=== As an individual invocation ===
<syntaxhighlight lang="console">
nix --experimental-features nix-command build ...
</syntaxhighlight>


== Common flags ==
=== By setting it in the nix configuration ===
{{File|3=experimental-features = nix-command|name=~/.config/nix/nix.conf|lang=toml}}


* <code>--debug</code> enable debug output
=== On NixOS, by setting it in the NixOS configuration ===
* <code>--help</code> show usage information
''On NixOS you can't edit <code>/etc/nix/nix.conf</code> directly, so you have to set it through the NixOS configuration instead''
* <code>--help-config</code> show configuration options
{{File|3={ pkgs, ... }: {
* <code>--log-format FORMAT</code> format of log output; "raw", "internal-json", "bar" or "bar-with-logs"
  nix.settings.experimental-features = [ "nix-command" ];
* <code>--no-net</code> disable substituters and consider all previously downloaded files up-to-date
}|name=/etc/nixos/configuration.nix|lang=nix}}{{Evaluate}}
* <code>--option NAME VALUE</code> set a Nix configuration option (overriding nix.conf)
* <code>-L</code>, <code>--print-build-logs</code> print full build logs on stderr
* <code>--quiet</code> decrease verbosity level
* <code>--refresh</code> consider all previously downloaded files out-of-date
* <code>-v</code>, <code>--verbose</code> increase verbosity level
* <code>--version</code> show version information


In addition, most configuration settings can be overriden using <code>--name value</code>.
== Switching from <code>nix profile</code> to <code>nix-env</code> ==
Boolean settings can be overriden using <code>--name</code> or <code>--no-name</code>. See <code>nix
{{Warning|Using <code>nix-env</code> is not recommended.}}
--help-config</code> for a list of configuration settings.
Once you installed a package with <code>nix profile</code>, you get the following error message when using <code>nix-env</code>:


== Main commands ==
<syntaxHighlight lang=console>
$ nix-env -f '<nixpkgs>' -iA 'hello'
error: --- Error ----------------------------------------------------------------------------------------------------------------- nix-env
profile '/nix/var/nix/profiles/per-user/joerg/profile' is incompatible with 'nix-env'; please use 'nix profile' instead
</syntaxHighlight>


=== [[Nix_command/build|nix build]] ===
To migrate from <code>nix profile</code> to <code>nix-env</code>, you need to delete your current profile:


Build a derivation or fetch a store path.
{{warning|This will delete packages that have been installed before, so you may want to back this information before running the command.}}


=== [[Nix_command/develop|nix develop]] ===
<syntaxHighlight lang=console>
 
$ rm -rf /nix/var/nix/profiles/per-user/$USER/profile
Run a bash shell that provides the build environment of a derivation.
</syntaxHighlight>
 
=== [[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]] ===
== New equivalents to old commands ==
<syntaxhighlight lang="shell">
# create a store derivation for a package defined in the current directory's default.nix
old$ nix-instantiate -A somepackage
# assumes you are now using flakes
new$ nix eval .#somepackage.drvPath
# alternative option
new$ nix derivation show .#somepackage | jq '.[keys[0]]' | nix derivation add
</syntaxhighlight>


Convert a hash to SRI representation.
[[Category:Nix]]

Latest revision as of 16:02, 7 September 2025

Introduction to Nix

Tools and applications

⤧︎
Disambiguation: This article is about the new nix command. Not to be confused with the Nix ecosystem, the Nix language or the Nix package manager.

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

See the Nix manual for a complete reference.

Enabling the nix command

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

As an individual invocation

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

By setting it in the nix configuration

≡︎ ~/.config/nix/nix.conf
experimental-features = nix-command

On NixOS, by setting it in the NixOS configuration

On NixOS you can't edit /etc/nix/nix.conf directly, so you have to set it through the NixOS configuration instead

❄︎ /etc/nixos/configuration.nix
{ pkgs, ... }: {
  nix.settings.experimental-features = [ "nix-command" ];
}
🟆︎
Tip: In order to affect your NixOS system by your nix-language-specific changes you must first evaluate it:
$ nixos-rebuild switch --sudo


Switching from nix profile to nix-env

⚠︎
Warning: Using nix-env is not recommended.

Once you installed a package with nix profile, you get the following error message when using nix-env:

$ nix-env -f '<nixpkgs>' -iA 'hello'
error: --- Error ----------------------------------------------------------------------------------------------------------------- nix-env
profile '/nix/var/nix/profiles/per-user/joerg/profile' is incompatible with 'nix-env'; please use 'nix profile' instead

To migrate from nix profile to nix-env, you need to delete your current profile:

⚠︎
Warning: This will delete packages that have been installed before, so you may want to back this information before running the command.
$ rm -rf /nix/var/nix/profiles/per-user/$USER/profile

New equivalents to old commands

# create a store derivation for a package defined in the current directory's default.nix
old$ nix-instantiate -A somepackage
# assumes you are now using flakes
new$ nix eval .#somepackage.drvPath
# alternative option
new$ nix derivation show .#somepackage | jq '.[keys[0]]' | nix derivation add