FAQ: Difference between revisions
Marked this version for translation |
m replace <code> with <pre> to unbreak formatting |
||
| (6 intermediate revisions by 5 users not shown) | |||
| Line 28: | Line 28: | ||
Also see: | Also see: | ||
* https://wiki.nixos.org/wiki/User:Winny/WikiRisks | * https://wiki.nixos.org/wiki/User:Winny/WikiRisks | ||
* https://greasyfork.org/en/scripts/ | * https://greasyfork.org/en/scripts/560152-redirect-to-wiki-nixos-org (trivial userscript to redirect nixos.wiki links here) | ||
=== Why is Nix written in C++ rather than a functional language like Haskell? === <!--T:2--> | === Why is Nix written in C++ rather than a functional language like Haskell? === <!--T:2--> | ||
| Line 54: | Line 54: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nixos-rebuild switch | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 61: | Line 63: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r | $ nix-store -qR $(nix-instantiate '<nixpkgs/nixos>' -A system) | xargs nix-store -r | ||
warning: you did not specify `--add-root'; the result might be removed by the garbage collector | warning: you did not specify `--add-root'; the result might be removed by the garbage collector | ||
| Line 73: | Line 75: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-store -qR ~/.nix-profile | xargs nix-store -r | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 99: | Line 103: | ||
</translate> | </translate> | ||
< | <pre> | ||
q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/ | q0yi2nr8i60gm2zap46ryysydd2nhzhp-automake-1.11.1/ | ||
vbi4vwwidvd6kklq2kc0kx3nniwa3acl-automake-1.11.1/ | vbi4vwwidvd6kklq2kc0kx3nniwa3acl-automake-1.11.1/ | ||
| Line 109: | Line 113: | ||
j714mv53xi2j4ab4g2i08knqr137fd6l-nixos-build-vms/ | j714mv53xi2j4ab4g2i08knqr137fd6l-nixos-build-vms/ | ||
xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/ | xvs7y09jf7j48p6l0p87iypgpq470jqw-nixos-build-vms/ | ||
</ | </pre> | ||
<translate> | <translate> | ||
=== I've updated my channel and something is broken, how can I rollback to an earlier channel? === <!--T:5--> | === I've updated my channel and something is broken, how can I rollback to an earlier channel? === <!--T:5--> | ||
| Line 118: | Line 123: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels | $ nix-env --list-generations -p /nix/var/nix/profiles/per-user/root/channels | ||
18 2014-04-17 09:16:28 | 18 2014-04-17 09:16:28 | ||
19 2014-06-13 10:31:24 | 19 2014-06-13 10:31:24 | ||
| Line 130: | Line 135: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels | $ nix-env --rollback -p /nix/var/nix/profiles/per-user/root/channels | ||
switching from generation 20 to 19 | switching from generation 20 to 19 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 140: | Line 145: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels | $ nix-env --switch-generation 18 -p /nix/var/nix/profiles/per-user/root/channels | ||
switching from generation 20 to 18 | switching from generation 20 to 18 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 149: | Line 154: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-build -E 'with import <nixpkgs> { }; callPackage ./mypackage.nix { }' | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 161: | Line 168: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-build -E 'with import <nixpkgs> { }; enableDebugging fooPackage' | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 173: | Line 182: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ sudo nix-build --check -A ncdu | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 199: | Line 210: | ||
</syntaxhighlight></li> | </syntaxhighlight></li> | ||
<li><p><translate><!--T:137--> Install all specified packages using this command:</translate></p> | <li><p><translate><!--T:137--> Install all specified packages using this command:</translate></p> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-env -iA userPackages -f '<nixpkgs>'</syntaxhighlight></li> | |||
</ol> | |||
<translate> | <translate> | ||
| Line 277: | Line 290: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-build mybinaryprogram.nix | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 284: | Line 299: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ ./result/bin/mybinaryprogram | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 318: | Line 335: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-shell fhsUser.nix | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 363: | Line 382: | ||
* For nixpkgs: the trunk [http://hydra.nixos.org/job/nixpkgs/trunk/unstable unstable] job, which contains some critical release packages. | * For nixpkgs: the trunk [http://hydra.nixos.org/job/nixpkgs/trunk/unstable unstable] job, which contains some critical release packages. | ||
</translate> | </translate> | ||
<ol start="2" style="list-style-type: decimal;"> | <ol start="2" style="list-style-type: decimal;"> | ||
| Line 383: | Line 401: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-instantiate --find-file nixpkgs | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 402: | Line 422: | ||
<!--T:83--> | <!--T:83--> | ||
So in short, the <code> | So in short, the <code>release-XX.YY</code> branches have not been run through Hydra yet, whereas the <code>nixos-XX.YY</code> ones have. | ||
=== There's an updated version for $software on nixpkgs but not in channels, how can I use it? === <!--T:13--> | === There's an updated version for $software on nixpkgs but not in channels, how can I use it? === <!--T:13--> | ||
| Line 410: | Line 430: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz nix-shell -p $software | $ NIX_PATH='nixpkgs=https://github.com/NixOS/nixpkgs/archive/release-17.09.tar.gz' nix-shell -p $software | ||
</syntaxhighlight> | |||
<translate> | |||
=== There's an updated version for $software on the unstable branch, but I use stable, how can I use it? === <!--T:143--> | |||
<!--T:144--> | |||
Before going ahead with this, note that firstly, this likely means that the package you intend to update has had a major version change. If you have used it previously, there is a chance that your existing data either will not work with the new version or will need to be migrated; If in doubt, consult the upstream documentation of the package. | |||
<!--T:145--> | |||
Secondly, while you're less likely to run into issues on NixOS than on, for example, Debian when installing packages from different releases, it's not impossible. | |||
<!--T:146--> | |||
Nix ensures that libraries and (usually) runtime dependencies of packages are kept separate, so that you can trivially have many versions of those dependencies installed, without affecting the versions of said dependencies used by important system components. This ensures that you cannot accidentally break your package manager by, say, updating Python, as is quite common on other distros. | |||
<!--T:147--> | |||
Nix cannot however ensure that there will be no incompatibilities with services of which there can inherently be only one running instance. As an example, if you try to use a package from unstable on a stable system that requires a feature in systemd that is not yet present in the systemd version on stable, this package will not work; it's simply not possible to run two different versions of systemd simultaneously. | |||
<!--T:148--> | |||
Nonetheless, it's quite uncommon that end-user facing applications rely on such singleton services, or at the very least they will typically have internal backwards compatibility. As such, mixing channels is usually unproblematic in practice, and even if not, NixOS' rollback features make it trivial to recover from problems should they occur. | |||
==== Using channels ==== <!--T:149--> | |||
<!--T:150--> | |||
First we need to add the unstable channel to our system channels: | |||
<!--T:151--> | |||
{{Warning|<code>nixos-rebuild --upgrade</code> will by default only update the channel named <code>nixos</code>, which this new channel is not. Use <code>nixos-rebuild --upgrade-all</code> instead.}} | |||
</translate> | |||
<syntaxhighlight lang="console"> | |||
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable | |||
$ sudo nix-channel --update | |||
</syntaxhighlight> | |||
<translate> | |||
<!--T:152--> | |||
Then we can import this channel using the angle-bracket notation to refer to it: | |||
</translate> | |||
<syntaxhighlight lang="nixos"># configuration.nix | |||
{ | |||
config, | |||
pkgsUnstable, | |||
... | |||
}: { | |||
# We add a new `pkgsUnstable` to the module arguments; this allows | |||
# us to easily use `pkgsUnstable` in other modules as well, without | |||
# having to evaluate it again. | |||
_module.args.pkgsUnstable = import <nixos-unstable> { inherit (config.nixpkgs) config; }; | |||
environment.systemPackages = [ | |||
# Once we have created our `pkgsUnstable`, we can easily use | |||
# packages from it wherever NixOS modules expect derivations | |||
pkgsUnstable.hello | |||
]; | |||
}</syntaxhighlight> | |||
<translate> | |||
==== Using flakes ==== <!--T:153--> | |||
<!--T:154--> | |||
We simply add the unstable branch to our flake inputs, and pass them into the NixOS module system using <code>specialArgs</code>: | |||
</translate> | |||
<syntaxhighlight lang="nix"> | |||
# flake.nix | |||
{ | |||
inputs = { | |||
nixpkgs.url = "https://channels.nixos.org/nixos-25.05/nixexprs.tar.xz"; | |||
nixpkgs-unstable.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz"; | |||
}; | |||
outputs = { nixpkgs, ... } @ inputs: { | |||
# Note that the hostname "nixos" and the system tuple used here are | |||
# examples. | |||
nixosConfigurations."nixos" = nixpkgs.lib.nixosSystem { | |||
system = "x86_64-linux"; | |||
modules = [ | |||
./configuration.nix | |||
]; | |||
# Any attributes of `specialArgs` will be added to our NixOS module | |||
# arguments. | |||
# | |||
# We've bound `nixpkgs-unstable` to the `inputs` variable using the `@` | |||
# syntax; if we add any other flake inputs in the future those will also | |||
# be added to our module arguments. | |||
specialArgs.flake-inputs = inputs; | |||
}; | |||
}; | |||
} | |||
</syntaxhighlight> | |||
<translate> | |||
<!--T:155--> | |||
Using this in <code>configuration.nix</code> then looks as follows: | |||
</translate> | |||
<syntaxhighlight lang="nixos"> | |||
# configuration.nix | |||
{ | |||
pkgs, | |||
flake-inputs, | |||
... | |||
}: { | |||
environment.systemPackages = [ | |||
flake-inputs.nixpkgs-unstable.legacyPackages.${pkgs.system}.hello | |||
]; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 438: | Line 568: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ sudo unshare -m bash | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 445: | Line 577: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ mount -o remount,rw /nix/store | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 452: | Line 586: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nano <PATH_TO_PACKAGE>/default.nix | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 459: | Line 595: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ exit | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 474: | Line 612: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-prefetch-git https://git.zx2c4.com/password-store | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 505: | Line 645: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nixos-rebuild switch --option binary-caches '' | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 539: | Line 681: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nix-shell -I nixpkgs=channel:nixpkgs-unstable -p somepackage | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 546: | Line 690: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 553: | Line 699: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ sudo nix-channel --update nixos-unstable | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 609: | Line 757: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ su nano /etc/nixos/configuration.nix | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 616: | Line 766: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nix"> | ||
boot.extraModulePackages = [ config.boot.kernelPackages.exfat-nofuse ]; | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 623: | Line 775: | ||
</translate> | </translate> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="console"> | ||
$ nixos-rebuild switch | |||
</syntaxhighlight> | |||
<translate> | <translate> | ||
| Line 661: | Line 815: | ||
<translate> | <translate> | ||
== References == <!--T:134--> | == References == <!--T:134--> | ||