FAQ: Difference between revisions

Ardenet (talk | contribs)
Marked this version for translation
TLATER (talk | contribs)
Add FAQ entry on mixing stable/unstable
 
Line 413: Line 413:
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>
</syntaxhighlight>
<translate>
=== There's an updated version for $software on the unstable branch, but I use stable, how can I use it? ===
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.
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.
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.
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.
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 ====
First we need to add the unstable channel to our system channels:
{{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.}}
</translate>
<syntaxhighlight lang="console">
$ sudo nix-channel --add https://nixos.org/channels/nixos-unstable nixos-unstable
$ sudo nix-channel --update
</syntaxhighlight>
<translate>
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>
<translate>
==== Using flakes ====
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>
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>
<translate>
=== How do I install a specific version of a package for build reproducibility etc.? === <!--T:14-->
=== How do I install a specific version of a package for build reproducibility etc.? === <!--T:14-->