Search code examples
nixnixos

How to get the current nixos version (for example 23.05) in a nixos config?


Is there a method to retrieve the current NixOS version within a Nixos configuration.nix? I'm in the process of upgrading from NixOS 20.03 to 23.05 and I'd like to maintain a single configuration that is compatible with both versions.

Due to some package versions being exclusive to either version, I require conditional logic, like an if or switch construct, based on the NixOS version to allow me to specify which version of a package to use in a given Nixos version/system.

I guess a workaround is to just run nixos-version > /etc/nixos/nixos-version and read this as a normal file. But if there is a way to do this natively in the config that would be ideal.


Solution

  • I've used this pattern:

    {
      fonts =
        if (config.system.nixos.release == "23.05")
        then {
          fonts = [
            pkgs.nerdfonts
            pkgs.noto-fonts
          ];
        }
        else {
          packages = [
            pkgs.nerdfonts
            pkgs.noto-fonts
          ];
        };
    }