Making configuration conditional on nixpkgs version

I’m trying to set up a configuration.nix which supports two different versions of nixpkgs (21.11 and unstable). The first try was to simply run it, which showed that some configuration options have changed since 21.11. So I moved only the configuration differences into a 21.11.nix and unstable.nix, and now I want to import one of these depending on the value of an external NIXPKGS_VERSION environment variable. How do I do that? Putting (./. + "/${config.environment.variables.NIXPKGS_VERSION}.nix") in the imports list doesn’t work, because it results in infinite recursion. I also tried setting NIXOS_EXTRA_MODULE_PATH, but that doesn’t seem to work the way I understood the documentation.

Alternatively, is there some way I can get the nixpkgs version within configuration.nix, so that I can do something like mkIf config.nixpkgs == "21.11" { environment.systemPackages = [ pkgs.exfat-utils ]; };

There is lib.trivial.version and friends. They’re documented in the nixpkgs manual, but it’s a chore to link sub-sub-sub-headers so I’ll leave you to searching that name in this section yourself: Nixpkgs 23.11 manual | Nix & NixOS :wink:

FYI, I ended up doing this:

some-set = {
} // (
  if (config.system.nixos.release == "21.11") then {
  } else {

and this:

some-list = [
] ++ (
  if (config.system.nixos.release == "21.11")
  then [
  ] else [

FYI, referring to config.system.nixos.release in one of the imports causes an infinite recursion error, somehow. I had to instead refer to lib.trivial.version.

1 Like