How to switch to a stable channel and install using nix-env without needing the -f option?

I am using the NIx package manager (without NixOS). The default channel is nixpkgs-unstable release nixpkgs-24.05pre583512.01737456dfe4. I changed it to a stable channel (nixos-20.09 release nixos-20.09.4407.1c1f5649bb9) like this:

$ nix-channel --list  # Show the default channel.
nixpkgs https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --add https://nixos.org/channels/nixos-20.09  # Add stable channel.
$ nix-channel --list
nixos-20.09 https://nixos.org/channels/nixos-20.09
nixpkgs https://nixos.org/channels/nixpkgs-unstable
$ nix-channel --update
unpacking channels...
created 2 symlinks in user environment
$ nix-channel --remove nixpkgs  # Remove the default channel.
uninstalling 'nixpkgs-21.05pre272788.870dbb751f4'
building '/nix/store/h11za3c6ja8hgrlkcs8nznib02wlp9cs-user-environment.drv'...
$ nix-channel --list
nixos-20.09 https://nixos.org/channels/nixos-20.09

To install GNU Hello, I tried:

$ nix-env -iA nixpkgs.hello
error: attribute 'nixpkgs' in selection path 'nixpkgs.hello' not found

I also tried:

$ nix-env -iA nixos-20.09.hello
error: attribute 'nixos-20' in selection path 'nixos-20.09.hello' not found

I finally succeeded in installing GNU Hello by running:

$ nix-env -f '<nixos-20.09>' -iA hello

Iā€™m wondering: if I have switched the default unstable channel to a stable channel, how do I avoid the need to specify -f '<nixos-20.09>' every time I run nix-env to install software?

At the risk of confusing you by throwing more unfamiliar ideas at you at a time when you are already dealing with plenty (in which case, just ignore me for now, and come back to the idea later), I would recommend not using nix-env -i, and look into using home manager instead.

nix-env -i is already a big step forward from other state-mutating package managers, but home-manager allows you to manage your environment in a declarative way, and this brings you many more of the advantages that Nix has to offer!

Use nix-channel --add https://nixos.org/channels/nixos-20.09 nixpkgs to name the channel, then --remove nixos-20.09, --update and then you can use your new stable default.

1 Like