FAQ/zh: Difference between revisions
No edit summary |
Updating to match new version of source page |
||
| Line 412: | Line 412: | ||
<syntaxhighlight lang="command"> | <syntaxhighlight lang="command"> | ||
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> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=== There's an updated version for $software on the unstable branch, but I use stable, how can I use it? === | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
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. | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==== Using channels ==== | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
First we need to add the unstable channel to our system channels: | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
{{Warning|`nixos-rebuild --upgrade` will by default only update the channel named `nixos`, which this new channel is not. Use `nixos-rebuild --upgrade-all` instead.}} | |||
</div> | |||
<syntaxhighlight lang="console"> | |||
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable | |||
$ sudo nix-channel --update | |||
</syntaxhighlight> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Then we can import this channel using the angle-bracket notation to refer to it: | |||
</div> | |||
<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> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==== Using flakes ==== | |||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
We simply add the unstable branch to our flake inputs, and pass them into the NixOS module system using <code>specialArgs</code>: | |||
</div> | |||
<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> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Using this in <code>configuration.nix</code> then looks as follows: | |||
</div> | |||
<syntaxhighlight lang="nixos"> | |||
# configuration.nix | |||
{ | |||
pkgs, | |||
flake-inputs, | |||
... | |||
}: { | |||
environment.systemPackages = [ | |||
flake-inputs.nixpkgs-unstable.legacyPackages.${pkgs.system}.hello | |||
]; | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||