If I add a channel, which one is the primary channel?

I install a basic, straightforward NixOS configuration from a 23.11 ISO, using the guided installer.
No flakes or advanced anything.
Then I do sudo nix-channel --add https://nixos.org/channels/nixpkgs-unstable unstable && sudo nix-channel --update
Now I have two channels.
Which one does my config use by default, and where is this defined?
And which one does nix-env -i use?

I am asking in order to understand the principle of the thing, because I had a slightly different case where things did not turn out as expected. I installed from 24.05 ISO, so I was on the unstable branch. Then I added the 23.11 branch. After that, I tried to remove the unstable branch, but was unable to.

It will use the one named nixpkgs or nixos by default. Any others need to be explicitly imported by doing something like import <unstable> {}

1 Like

There’s no such thing as “by default” – as @mjm noted, the channel name (e.g. nixpkgs) merely maps to the lookup path (e.g. <nixpkgs>) – so there is nothing to define.

Internally, nix-env uses an expression that contains import <nixpkgs>, so it uses the channel called nixpkgs. If no such channel is defined, there are hard-coded defaults for lookup path values.

From all of that you may conclude that it’s a mess, and I’d agree. I strongly recommend not using channels or lookup paths.

It’s actually a widespread misconception that the flake registry somehow has substantially better user experience channels. The only difference is that channel entries have to point to archives that follow the old profile format while flake registry entries have to point to archives that follow the flake format. Both store mutable entries on the local machine, which may point to something else on a different machine.

Therefore I strongly recommend using neither channels nor the flake registry, and instead to explicitly pin remote sources. With stable interfaces, one way to do that is with niv (Automatically managing remote sources with niv — nix.dev documentation) or npins (which is actively maintained, we should update that nix.dev guide); with flakes it’s the nix flake command.

1 Like

Can you declare in flake.nix to follow the latest stable channel, whatever it may be? Or do you need to change the channel stated in flake.nix when a new release comes out?