18

How do I set the default channel in NixOS's /etc/configuration.nix?

There is a command to set it and rebuild with

sudo nix-channel --add https://nixos.org/channels/nixpkgs-unstable
sudo nixos-rebuild switch -I nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixpkgs

but I'd like to have it setup in configuration.nix so I don't have to remember how to do this everytime. Thanks!

Jacob Wang
  • 4,411
  • 5
  • 29
  • 43

3 Answers3

9

system.autoUpgrade.channel is what you might be looking for

set it to any channel e.g.

system.autoUpgrade.channel = "https://nixos.org/channels/nixos-16.03-small/";

the documentation says:

by default, this is the channel set using nix-channel (run nix-channel --list to see the current value)

an up to date list of channels can be found on https://nixos.org/channels/

ref: https://nixos.org/nixos/manual/options.html#opt-system.autoUpgrade.channel https://nixos.org/nixos/manual/index.html#idm140737317454064

hash
  • 244
  • 2
  • 3
  • Thanks this looks promising. Unfortunately my experimentation with NixOS has ended so I can't verify this. If anyone can verify then I'll select this as the answer. – Jacob Wang May 15 '16 at 12:01
  • Say you had the system on 19.03 initially, and then you set this autoupgrade channel to 19.09. It goes thru with auto upgrade and all is well... But then you run `nixos-rebuild switch --upgrade` and... you rollback to the previous version, right? That doesn't feel good... – hypersw Oct 30 '19 at 00:42
  • Unfortunately the documentation doesn't specify this, but it looks like if you have more than one channel it uses the first one alphabetically as your default. Can someone confirm this? – l0b0 Oct 31 '19 at 06:47
6

Set nixPath = [ "nixpkgs=http://nixos.org/channels/nixos-unstable/nixexprs.tar.xz" ];, see https://github.com/snabblab/snabblab-nixos/blob/d8b9761b107293891b19021f2f0f77a0e3ba3746/modules/common.nix#L39

Kevin Cox
  • 3,080
  • 1
  • 32
  • 32
iElectric
  • 5,633
  • 1
  • 29
  • 31
  • 1
    I tried this but it didn't seem to work similarly as using `nix-channel`, if I understood correctly what was going on. First, when running `nixos-rebuild switch`, it upgraded nixpkgs by downloading the tar.xz file. Second, almost(?) every time nixpkgs was needed, it was downloaded. How can I have functionality as when setting with `nix-channel`? That is, upgrading nixpkgs only when explicitly asked. Still, the channel could be set in configuration.nix. Does this make sense? – Jaakko Luttinen Oct 31 '17 at 05:25
  • @JaakkoLuttinen you could use `nixpkgs="${fetchTarball http://nixos.org/channels/nixos-unstable/nixexprs.tar.xz}"`, this way, the fetching would only happen at nixos-rebuild time, and at runtime the system would see an already spliced-in nix store path – enobayram Aug 27 '18 at 04:58
3

The nix.nixPath (ref) option looks like it will do what you're after.

Also the nixos-unstable channel might be more appropriate for you, rather than nixpkgs-unstable. I believe the pkgs in the nixpkgs channel are tested and built for non-nixOS systems, though I can't remember a reference for that at the moment.

nix-channel --add https://nixos.org/channels/nixos-unstable/ 
nix-channel --update nixos-unstable
# /etc/nixos/configuration.nix
# Put nixos-unstable at the front of nixPath
{ lib, ... }:
{
  nix.nixPath = lib.mkDefault (lib.mkBefore [ "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos-unstable" ]);
}

If you want the imperative nix-channel commands in your configuration.nix as well you could write a small systemd service to do so, as shown here. Edit: To ensure configuration.nix is built from the latest unstable channel just set the value of nixpkgs as in the answer from @iElectric and Nix will use the expressions contained at that URL whenever it evaluates configuration.nix.

PS I realised you could also just point the nixos path to the nixos-unstable channel by doing nix-channel --add https://nixos.org/channels/nixos-unstable/ nixos but I think the first solution is clearer.

Atemu
  • 295
  • 3
  • 12
brocking
  • 821
  • 6
  • 9
  • Thanks for the tips. It looks like that can replace the `nixos-rebuild` command, however how can I add the unstable channel itself in `configuration.nix`? – Jacob Wang Apr 06 '16 at 23:22
  • Warning: this does not work out-of-the-box (anymore at least): the path is wrong, and it removes required entries like nixos-config... – olejorgenb Jun 09 '18 at 09:20
  • 1
    @olejorgenb, the `nixPath` value might need to be wrapped in a `lib.mkDefault` to avoid replacing the `nixos-config` value. I don't have a NixOS setup currently to check that though. But the NixOS manual still has the `nixPath` option so I think it's still the appropriate option to use. I think that path should still work as well, though it seems I accidentally omitted the initial `/`, I will amend that. – brocking Jun 09 '18 at 10:05