NixOS: Setting the default channel in configuration.nix - nix

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!

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

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

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.

Related

Nixos 22.05 system build fails when referencing channel "file 'nixos-2111' was not found in the Nix search path (add it using $NIX_PATH or -I)"

When running sudo nixos-rebuild --upgrade boot I run into an error of:
error: file 'nixos-2111' was not found in the Nix search path (add it using $NIX_PATH or -I)
at /etc/nixos/chris.nix:30:23:
29| };
30| nixos-2111 = import <nixos-2111> {
| ^
31| config = config.nixpkgs.config;
This only occurred after I updated the nixos channel with sudo nix-channel --add https://nixos.org/channels/nixos-22.05 nixos (for the purpose of upgrading my Nixos system). This config built correctly previously (which is on 20.09).
What might be the cause of this issue? How can I diagnose this further? That channel definitely exists according to:
sudo nix-channel --list
nixos https://nixos.org/channels/nixos-22.05
nixos-2003 https://nixos.org/channels/nixos-20.03
nixos-2111 https://nixos.org/channels/nixos-21.11
nixos2003 https://nixos.org/channels/nixos-20.03
nixos2009 https://nixos.org/channels/nixos-20.09
unstable https://nixos.org/channels/nixpkgs-unstable
And:
nix repl '<nixos-2111>'
Welcome to Nix version 2.3.11. Type :? for help.
Loading '<nixos-2111>'...
Added 15491 variables.
My only suspicion is the new nix version of 2.8 from the prior 2.3.11 - perhaps it uses an different set of channels?
Update: Just removed the references to this channel (and the associated packages), and it built successfully. Which is weird as there were other channels referenced in this exact format so maybe something specifically to do with 21.11...

import <channel-name> => not found

I'm following nix pills [1], which doesn't work anymore with the newest channel release [2].
$ echo 'graph test { a -- b }'|result/bin/dot -Tpng -o test.png
Format: "png" not recognized. ...
So I took the advice to use an old channel [3] (and replace binutils with binutils-unwrapped).
nix-channel --add https://nixos.org/channels/nixpkgs-release-14.12
Which works perfectly fine.
nixpkgs https://nixos.org/channels/nixpkgs-unstable
nixpkgs-release-14.12 https://nixos.org/channels/nixpkgs-release-14.12
But when I try to use it via import <nixpkgs-release-14.12>
I get
error: file 'nixpkgs-release-14.12' was not found in the Nix search path
[1] https://nixos.org/nixos/nix-pills/inputs-design-pattern.html
[2] https://github.com/NixOS/nix-pills/issues/107
[3] https://github.com/NixOS/nix-pills/issues/107#issuecomment-470992383
I hacked it via
import (fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-14.12.tar.gz)
Works, but I'm not sure it's the optimal solution.
You have to download it with the nix-channel --update command, but that won't work because the channel has been removed. You'll have to use a newer channel and run --update.
You can do it by checking out the version you want with git in a directory and setting NIX_PATH. Otherwise, it's either not available and/or your other packages in the newer channel would supersede it. You'd have to call your command with NIX_PATH=$mynixcheckoutdir prepended.

I changed my nixos channel to unstable, why are my packages still not updated?

How do I update my packages from the nixos-18.09 channel to the nixos-unstable channel? I am trying to do this as I need to use updated versions of some packages.
Following the directions in the nixos manual, I have added the nixos-unstable channel https://nixos.org/channels/nixos-unstable, removed the nixos-18.09 channel https://nixos.org/channels/nixos-18.09,
and run $ nix-channel --update and $ sudo nixos-rebuild switch --upgrade.
Still, however, my installed packages are the old versions. I have also tried $ nix-env -u '*' and still have the old versions. What else do I need to do to update my packages to the nixos-unstable versions?
I am trying to do this as I need to use updated versions of some
packages.
You can use packages from different channels. So possibly keeping your default channel as stable, and just referencing particular packages from the unstable channel might be a better solution.
I have the following import in my config (using google-chrome from the channel I have assigned to unstable):
{ config, pkgs, ... }:
let
unstable = import <unstable> {
config = config.nixpkgs.config;
};
in
{
environment.systemPackages = with pkgs; [
awscli
google-cloud-sdk
kubectl
libreoffice
# ...
unstable.google-chrome
];
}
As to why your packages are not using the unstable channel, probably because they reference the nixos channel which you did not seem to modify (you seemed to only add a channel named nixos-unstable, but nothing referenced this channel name most probably).
To change to the unstable NixOS channel:
Become root; You want to change the root user's Nix channel: su
Remove the NixOS channel: nix-channel --remove nixos
Add the NixOS unstable channel and ensure it's named nixos: nix-channel --add nixos https://nixos.org/channels/nixos-unstable
Then you can upgrade as you normally would. For example, nixos-rebuild --upgrade boot will first update the NixOS channel and then install packages from the updated channel. Upon reboot you'd be updated.
Note: I advise against doing nixos-rebuild --upgrade switch, especially when changing channels, simply because NixOS won't restart all services anyway. Therefore the switch would be incomplete.
If you want to change all the packages to unstable
{ pkgs, ... }#args:
let
pkgsUrl = "https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz";
pkgs = import (builtins.fetchTarball pkgsUrl) {};
in
{...}

What nix channel is subscribed to by default

I installed the nix package manager on my machine (macOS 10.12.6 Sierra) several weeks ago.
I want to update mylocal nixpkgs collection to bring it in sync with any upstream updates in the channel. My understanding is that this can be achieved by running: nix-channel --update. However when I run this I get the following output:
unpacking channels...
created 0 symlinks in user environment
Suggesting no expressions were updated in the channels my system is subscribed to. If I run nix-channel --list I don't see any channels listed. What channel is my system subscribed to by default? and should I expect it to be listed?
Is it the case that generally nix-channel --update will only produce local changes if I have modified the channels I'm subscribed to or if I'm subscribed to the unstable channel?
The following might be specific to OSX:
Nix channels are managed on a per-user basis (source). nix-channel --list's output is empty because by default you are not subscribed to any channels - only root is subscribed to nixpkgs-unstable (source).
You can run nix-channel --list as root to see his subscriptions. But
do not run it through sudo (see: https://github.com/NixOS/nix/issues/1548) and
do not use a simple sudo su because it doesn't load /etc/profile (source) and thus will not have the nix env variables set up (variables-setup).
Working example:
user$ nix-channel --list
user$ sudo su -
root# nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable
Your nix-channel --update suggests that you have zero channels in your channel list, not zero packages. You can see your channel configuration with nix-channel --list. You probably need to configure a channel.
The Nix install script currently configures a single channel with the name nixpkgs:
"$nix/bin/nix-channel" --add https://nixos.org/channels/nixpkgs-unstable
So that's the default channel and it should be listed in nix-channel --list after installation, until you nix-channel --remove nixpkgs or rm ~/.nix-channels.
The nix-channel command only updates your Nix expressions. It will not update any package installations. It will only affect future invocations of nix-build, nix-env and everything else that uses $NIX_PATH. (It resembles apt-get update in this respect, or brew update, except nix-channel will not update the installed version of Nix.)

How to undo `nix-channel --update`

After clean installation via NixOps, I logined to machine and executed
# nix-channel --update
This pulled ~130Mb of nixpkgs into my /nix/store. How can I undo this?
On a normal Nix or NixOS install, if you've updated your channels by accident with nix-channel --update, you can undo this by running
nix-channel --rollback
See the manual for more details.
On a NixOps installation, channels are configured but not actually used. See the other answer for that.
On a NixOps installation it seems that channels are configured, but aren't actually used by NixOps. This means that running nix-channel --update will create the very first generation, and nix-channel --rollback won't work as that can't roll back past the first generation.
The following seems to work for clearing out this information.
Do not run this on a Nix or NixOS install! This is just for NixOps.
rm /nix/var/nix/profiles/per-user/root/channels-1-link
rm /nix/var/nix/profiles/per-user/root/channels
rm ~/.nix-defexpr/channels
nix-collect-garbage -d

Resources