Imperative vs declarative nixos-containers have different NixOS versions


I have a weird issue…

After upgrading to 22.05, whenever I create a nixos-container using

nixos-container create foo

I still get a container with nixos-21.11.

When I create a declarative container using the configuration, I get a nixos-22.05 container.

(The declarative container is built on my local machine and then pushed onto the remote machine, I am currently using colmena for deployment, but still, I am very confused why I continue to get 21.11 containers)

It is really annoying, because i really want to create a non-declarative 22.05 container to try out some things, which I then want to use in my declarative 22.05 container…

Does anyone know this problem? So far I tried setting the system.stateVersion inside both the host as well as the container configuration to 22.05 but I don’t really know where to start… I also did a reboot

Any help would be greatly appreciated!

The nixos-container command uses (abridged) nix-env -f <nixpkgs/nixos> by default, which will point to whatever version of nixpkgs is configured in your channels.

If you’ve switched to flakes, or maybe are running this as your user rather than root (who has a separate set of channels), it’s likely that your channels haven’t been updated, and that it’s therefore using an outdated channel to create your containers.

Try nix-channel --list and sudo nix-channel --list, and see if any of them still point to 21.11.

The fix is of course to update whatever channel is responsible for the outdated NixOS (and perhaps remove a channel to prevent this in the future).

You can override the nixpkgs used by nixos-container using --nixos-path, too, if you’d like to keep that outdated channel around for some reason.

That, or you can switch to using flakes for your containers which don’t have any of these problems to begin with.

For reference, system.stateVersion doesn’t specify the version of NixOS to use. Rather, it defines what version of NixOS you were using when you first created that config. This value is then used by various modules to check what version of their configuration your config is built for - this allows for stuff like automatic migration of databases, or just not breaking config where it’s not really necessary.

It shouldn’t really be changed, unless you know that all your configuration has been updated (and that none of the modules you use rely on the old version number to figure out where their data is). Unless the update notes say otherwise, of course.

It definitely won’t help you upgrade NixOS either way :slight_smile: