downgrade or upgrade a package in NixOS

As you may know (or not, if you forgot), packages are pinned to nixpkgs, so you may not have the latest version of a package, e.g. your favorite neovim.

Or worst, you have a bug or an incompatibility issue with a package pinned by nixpkgs.

In Flakes, package versions and hash values are directly tied to the git commit, of their flake input. To modify the package version, you need to lock the git, commit of the flake input.

Here’s an example of how you can add multiple nixpkgs inputs, each using a, different git commit or branch:

{
  description = "NixOS/home-manager configuration";
 
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
    nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
    # ...
  };
 
  # ...
  outputs = {
    self,
    nixpkgs,
    nixpkgs-unstable,
    home-manager,
    ...
  } @ inputs:
    # ...
    nixosConfigurations = {
      "${systemSettings.hostname}" = nixpkgs.lib.nixosSystem {
        specialArgs = {
          pkgs-unstable = import nixpkgs-unstable { };
          inherit fileExplorer;
          inherit systemSettings;
          inherit userSettings;
          inherit inputs;
        };
        modules = [./nixos/configuration.nix];
      };
    };
}

Then in your package, you can use this pinned nixpkgs:

{ pkgs-unstable, ... }: {
  programs.neovim = {
    enable = true;
    package = pkgs-unstable.neovim;
  };
}

Don’t forget to apply your change in your home-manager afterwards!

You can find the nixpkgs version/hash that contains your package at a specific version using https://lazamar.co.uk/nix-versions/.

Source: https://nixos-and-flakes.thiscute.world/nixos-with-flakes/downgrade-or-upgrade-packages