Is there a way to set the default channel for nix-env -u / pin packages to a channel as user?
In detail:
I often install packages as user, and sometimes need them from different channels (release, unstable, older releases), so I have several channels:
$ nix-channel --list
nixos https://nixos.org/channels/nixos-23.05
old https://nixos.org/channels/nixos-22.11
unstable https://nixos.org/channels/nixos-unstable
Now:
If I install a package, it should be taken from the channel nixos by default (except if I specify a different channel). This works well by using
But when I upgrade, the packages should only update to newer versions of the same channel, so:
A package from channel old should not upgrade to a version of channel nixos or unstable.
This should be possible by pinning – but is there a simple way to “pin a package to its channel”?
Especially, nix-env -u must not upgrade all packages to newer versions from unstable, but only to newer versions from channel nixos.
(Except packages installed from channel unstable – they should upgrade to newer versions from channel unstable.)
nix-env cannot do this as its manifest does not record how something was installed. This also means that nix-env often replaces derivations with completely different ones that happen to share the same pname when updating.
nix profile is better (it records the flake ref that was used to insall something and uses this ref when updating), but where nix really shines is declerative packages management.
Is there at least a way to tell nix-env to only update to newer versions of a specific channel?
Or is there any way to show which package was installed from which channel?
Then I could at least create a workaround…
About nix profile:
Since by default the nix command (and so also nix profile etc.) is disabled, and so probably not recommended, I’m currently trying not to use the nix command:
$ nix profile
error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override
Is there at least a way to tell nix-env to only update to newer versions of a specific channel?
yes, you can use nix-env -f <nixos> -u. You can use a custom default expression that does not expose other channels or doesnt have the recurseIntoAttrs-attribute for them.
Or is there any way to show which package was installed from which channel?
As I said, nix-env does not collect such information, the best you can do is instantiate suitable derivations from every nixpkgs revision you’ve ever touched and check which one yields the desired store-path. (Unless you have already run nix-env -u. In this case the information can no longer be recovered unless you have logged your shell history or something like that).
Since by default the nix command (and so also nix profile etc.) is disabled, and so probably not recommended,
Using nix-env for imperative package management is (in my humble opinion) even less recommended than using the nix command / flakes as it’s fundamentally not suitable for that task whereas nix-command / flakes are just subject to change.