Nix command: Difference between revisions

From NixOS Wiki
imported>Fricklerhandwerk
add link to manual
import from old wiki
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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 utilities that exist currently as many separate commands, eg. <code>nix-build</code>, <code>nix-shell</code>, etc.  
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.  


See the [https://nixos.org/manual/nix/stable/command-ref/experimental-commands.html Nix manual] for a complete reference.
See the [https://nixos.org/manual/nix/stable/command-ref/experimental-commands.html Nix manual] for a complete reference.
Line 8: Line 8:


For an individual invocation, eg.
For an individual invocation, eg.
<syntaxHighlight>
<syntaxHighlight lang=text>
nix --experimental-features nix-command build ...
nix --experimental-features nix-command build ...
</syntaxHighlight>
</syntaxHighlight>
Line 19: Line 19:
in <code>~/.config/nix/nix.conf</code>.
in <code>~/.config/nix/nix.conf</code>.


Or system-wide with
On NixOS you can't edit <code>/etc/nix/nix.conf</code> directly, but you can enable this feature by editing <code>/etc/nixos/configuration.nix</code>:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
# /etc/nix/nix.conf
#  
{ pkgs, ... }: {
{ pkgs, ... }: {
  nix.extraOptions = ''
  …
      experimental-features = nix-command
  nix.settings.experimental-features = [ "nix-command" ];
  '';
  …
}
}
</syntaxHighlight>
</syntaxHighlight>
in <code>/etc/nix/nix.conf</code> on NixOS.
and then run <code>sudo nixos-rebuild switch</code> as always.
 
== Switching between <code>nix-env</code> and <code>nix profile</code> ==
 
{{warning|Be careful when testing.
Once you have used <code>nix profile</code> you can no longer use <code>nix-env</code> without first deleting <code>/nix/var/nix/profiles/per-user/$USER/profile</code>}}
 
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>
$ 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>
 
To migrate back to <code>nix-env</code> you can 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.}}
 
<syntaxHighlight lang=console>
$ rm -rf /nix/var/nix/profiles/per-user/$USER/profile
</syntaxHighlight>
 
== New equivalents to old commands ==
<syntaxHighlight lang=console>
# 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>


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

Latest revision as of 18:06, 1 April 2024

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

On NixOS you can't edit /etc/nix/nix.conf directly, but you can enable this feature by editing /etc/nixos/configuration.nix:

# 
{ pkgs, ... }: {
  
  nix.settings.experimental-features = [ "nix-command" ];
  
}

and then run sudo nixos-rebuild switch as always.

Switching between nix-env and nix profile

Warning: Be careful when testing. Once you have used nix profile you can no longer use nix-env without first deleting /nix/var/nix/profiles/per-user/$USER/profile

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 back to nix-env you can 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