Jump to content

Nix (command): Difference between revisions

From NixOS Wiki
imported>Mic92
Malix (talk | contribs)
No edit summary
 
(36 intermediate revisions by 12 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 <code>experimental-features = nix-command</code>
See the [https://nixos.org/manual/nix/stable/command-ref/experimental-commands.html Nix manual] for a complete reference.
in <code>~/.config/nix/nix.conf</code> or system-wide in <code>/etc/nix/nix.conf</code>.
In NixOS the latter one can be also achieved by using:


<syntaxHighlight lang=nix>
== Enabling the nix command ==
{ pkgs, ... }: {
  nix.extraOptions = ''
      experimental-features = nix-command
  '';
}
</syntaxHighlight>
 
In the following we describe all sub commands of nix:
 
== Main commands ==
 
=== <code>nix build</code>  ===
 
==== Usage ====
 
<syntaxHighlight lang=console>
$ nix build FLAGS... INSTALLABLES...
</syntaxHighlight>
 
==== Description ====
Build a derivation or fetch a store path
 
==== Examples ====
 
To build and run GNU Hello from NixOS 20.03:
 
<syntaxHighlight lang=console>
$ nix build -f channel:nixos-20.03 hello; ./result/bin/hello
</syntaxHighlight>
 
To build the build.x86_64-linux attribute from release.nix:
 
<syntaxHighlight lang=console>
$ nix build -f release.nix build.x86_64-linux
</syntaxHighlight>
 
To make a profile point at GNU Hello (what is a profile?)
 
<syntaxHighlight lang=console>
$ nix build --profile /tmp/profile 'nixpkgs#hello'
</syntaxHighlight>
 
===  <code>nix develop</code> ===
 
==== Usage ====


<syntaxHighlight lang=console>
In nix 2.4, the nix command must be explicitly enabled. You can do this in a few different ways.
$ nix develop FLAGS... INSTALLABLE?
</syntaxHighlight>


==== Description ====
=== As an individual invocation ===
<syntaxhighlight lang="console">
nix --experimental-features nix-command build ...
</syntaxhighlight>


run a bash shell that provides the build environment of a derivation.
=== By setting it in the nix configuration ===
{{File|3=experimental-features = nix-command|name=~/.config/nix/nix.conf|lang=toml}}


==== Examples ====
=== On NixOS, by setting it in the NixOS configuration ===
''On NixOS you can't edit <code>/etc/nix/nix.conf</code> directly, so you have to set it through the NixOS configuration instead''
{{File|3={ pkgs, ... }: {
  nix.settings.experimental-features = [ "nix-command" ];
}|name=/etc/nixos/configuration.nix|lang=nix}}{{Evaluate}}


To get the build environment of GNU hello:
== Switching from <code>nix profile</code> to <code>nix-env</code> ==
{{Warning|Using <code>nix-env</code> is not recommended.}}
Once you installed a package with <code>nix profile</code>, you get the following error message when using <code>nix-env</code>:


<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix develop nixpkgs#hello
$ 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>
</syntaxHighlight>


To get the build environment of the default package of flake in the current directory:
To migrate from <code>nix profile</code> to <code>nix-env</code>, you need to delete your current profile:
<syntaxHighlight lang=console>
$ nix develop
</syntaxHighlight>


To store the build environment in a profile:
{{warning|This will delete packages that have been installed before, so you may want to back this information before running the command.}}
<syntaxHighlight lang=console>
$ nix develop --profile /tmp/my-shell nixpkgs#hello
</syntaxHighlight>


To use a build environment previously recorded in a profile:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
$ nix develop /tmp/my-shell
$ rm -rf /nix/var/nix/profiles/per-user/$USER/profile
</syntaxHighlight>
</syntaxHighlight>


=== <code>nix flake</code> ===
== New equivalents to old commands ==
 
<syntaxhighlight lang="shell">
manage Nix flakes
# 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
=== <code>nix profile</code> ===
new$ nix eval .#somepackage.drvPath
 
# alternative option
manage Nix profiles
new$ nix derivation show .#somepackage | jq '.[keys[0]]' | nix derivation add
 
</syntaxhighlight>
=== <code>nix repl</code> ===
 
start an interactive environment for evaluating Nix expressions
 
=== <code>nix run</code> ===
 
run a Nix application
 
=== <code>nix search</code> ===
 
query available packages
 
=== <code>nix shell</code> ===
 
run a shell in which the specified packages are available
 
== Infrequently used commands ==
 
=== <code>nix copy</code> ===
copy paths between Nix stores
 
=== <code>nix edit</code> ===
 
open the Nix expression of a Nix package in $EDITOR
 
=== <code>nix eval</code> ===
 
evaluate a Nix expression
 
=== <code>nix log</code> ===
 
show the build log of the specified packages or paths, if available
 
=== <code>nix path-info</code> ===
 
query information about store paths
 
=== <code>nix registry</code> ===
 
manage the flake registry
 
=== <code>nix verify</code> ===
 
verify the integrity of store paths
 
=== <code>nix why-depends</code> ===
 
show why a package has another package in its closure
 
== Utility/scripting commands ==
 
=== <code>nix add-to-store</code> ===
 
add a path to the Nix store
 
=== <code>nix cat-nar</code> ===
 
print the contents of a file inside a NAR file on stdout
 
=== <code>nix cat-store</code> ===
 
print the contents of a file in the Nix store on stdout
 
=== <code>nix copy-sigs</code> ===
 
copy path signatures from substituters (like binary caches)
 
=== <code>nix dump-path</code> ===
 
dump a store path to stdout (in NAR format)
 
=== <code>nix hash-file</code> ===
 
print cryptographic hash of the NAR serialisation of a path
 
=== <code>nix hash-path</code> ===
 
print cryptographic hash of the NAR serialisation of a path
 
=== <code>ls-nar</code> ===
 
show information about a path inside a NAR file
 
=== <code>nix ls-store</code> ===
 
show information about a path in the Nix store
 
=== <code>nix make-content-addressable</code> ===
 
rewrite a path or closure to content-addressable form
 
=== <code>nix optimise-store</code> ===
 
replace identical files in the store by hard links
 
=== <code>nix ping-store</code> ===
 
test whether a store can be opened
 
=== <code>nix print-dev-env</code> ===
 
print shell code that can be sourced by bash to reproduce the build environment of a derivation
 
=== <code>nix show-config</code> ===
 
show the Nix configuration
 
=== <code>nix show-derivation</code> ===
 
show the contents of a store derivation
 
=== <code>nix sign-paths</code> ===
 
sign the specified paths
 
=== <code>nix to-base16</code> ===
 
convert a hash to base-16 representation
 
=== <code>nix to-base32</code> ===
 
convert a hash to base-32 representation
 
=== <code>nix to-base64</code> ===
 
convert a hash to base-64 representation
 
=== <code>nix to-sri</code> ===


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