Nix shell and nix profile not using stable branch?

Fellow nixos + flake users, I have set nixpkgs version to 23.11 stable release in flake.nix but whenever I use nix shell or nix profile or nix-env even, they all seem to be using unstable branch.

I know there are workaround for that, like setting flake registry and whatnot, but I want to know why it isn’t by default to use the flake inputs and the given nixpkgs version? or will it be implemented in the near future?

Setting the registry is not a workaround. The registry is the canonical place that the nix 2.0 commands take the version of nixpkgs (and any other unspecified input) to use from.

The short answer is just that NixOS != nix, and NixOS doesn’t currently set reasonable defaults for nix’ flake configuration. This should probably be improved eventually, but hasn’t been because it’s not very high priority and full official support for flakes isn’t really on the horizon yet.

If you try nix registry list you’ll see a whole bunch of default registry entries, in fact, one of them will map nixpkgs to github:NixOS/nixpkgs/nixpkgs-unstable.

These defaults are disputed, because a good number of them really don’t seem relevant (dwarffs, really?). I’d personally expect that the defaults will be reviewed before anyone considers actually stabilizing flakes, and you’d not be the first to mention them being a bit odd on discourse.

In fact, the registry itself is disputed, because it kind of adds back a layer of non-declarative dependency resolution, somewhat defying the purpose of flakes in the first place (though I’ve found the escape hatch to be useful, personally).

Now, NixOS could choose to change the defaults. Personally, I think we should change the defaults to set nixpkgs to whatever flake input you have named nixpkgs, but for the moment the flake integration in NixOS is generally nearly nonexistent. We could really improve things by adding a flake-inputs arg to lib.nixosSystem, and have it automatically apply overlays and add inputs to _module.args and such. I keep meaning to propose such a change, but I’ve not really had the mindspace and don’t really know how to start proposing these kinds of things. It’d probably start with a PR, frankly.

For the moment, you’ll see lots of variations of these lines in many people’s NixOS configurations to make things map the way you would expect them to be: dotfiles/nixos-config/default.nix at ef1a08cf30ab648f799c7369847fe935df7e7a93 · TLATER/dotfiles · GitHub

nix-env will use the old NIX_PATH variable to resolve the nixpkgs path. You probably still have a channel set from before you were using flakes, or it falls back to yet another default. My config also contains a line changing nixPath which fixes that.