Jump to content

Nix (command): Difference between revisions

From NixOS Wiki
imported>Gdamjan
No edit summary
Malix (talk | contribs)
No edit summary
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Navbox Nix}}
{{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]].}}
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.  
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.  


Line 5: Line 8:
== Enabling the nix command ==
== 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.
In nix 2.4, the nix command must be explicitly enabled. You can do this in a few different ways.


For an individual invocation, eg.
=== As an individual invocation ===
<syntaxHighlight>
<syntaxhighlight lang="console">
nix --experimental-features nix-command build ...
nix --experimental-features nix-command build ...
</syntaxhighlight>
=== By setting it in the nix configuration ===
{{File|3=experimental-features = nix-command|name=~/.config/nix/nix.conf|lang=toml}}
=== 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}}
== 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>
$ 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>


Or by setting a user-specific configuration,
To migrate from <code>nix profile</code> to <code>nix-env</code>, you need to delete your current profile:
<syntaxHighlight lang=toml>
 
# ~/.config/nix/nix.conf
{{warning|This will delete packages that have been installed before, so you may want to back this information before running the command.}}
experimental-features = nix-command
 
<syntaxHighlight lang=console>
$ rm -rf /nix/var/nix/profiles/per-user/$USER/profile
</syntaxHighlight>
</syntaxHighlight>
in <code>~/.config/nix/nix.conf</code>.


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>:
== New equivalents to old commands ==
<syntaxHighlight lang=nix>
<syntaxhighlight lang="shell">
#  
# create a store derivation for a package defined in the current directory's default.nix
{ pkgs, ... }: {
old$ nix-instantiate -A somepackage
  …
# assumes you are now using flakes
  nix.settings.experimental-features = [ "nix-command" ];
new$ nix eval .#somepackage.drvPath
  …
# alternative option
}
new$ nix derivation show .#somepackage | jq '.[keys[0]]' | nix derivation add
</syntaxHighlight>
</syntaxhighlight>
and then run <code>sudo nixos-rebuild switch</code> as always.


[[Category:Nix]]
[[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