Building older versions of libraries

I’ve recently been exploring using nix shell, rather than docker, to create a development environment. One issue I quickly ran into was being able to load up older versions of libraries. This is often necessary if you need to support old code.

For example, suppose I want to use opencv 4.5.4, instead of the current version, opencv 4.7.0. In theory, I can change the version of opencv that gets built by using overrideAttrs. However, when I change the version and source attributes (see example below), I get an error because the patches aren’t compatible with this older version of opencv. I don’t know a way to change the patches directly, since they’re inside mkDerivation and thus inaccessible with overrideAttrs (I think?).

Is there an established way of doing this? Should I load in an older version of the opencv nix file?


An example of what I’m doing:

opencv = (pkgs.opencv.override (previous: {

enablePython = true;})).overrideAttrs (oldAttrs: {
  nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [pkgs.ant pkgs.jdk];
  version = "4.5.4";

  src = pkgs.fetchFromGitHub {
    owner = "opencv";
    repo = "opencv";
    rev = "4.5.4";
    sha256 = "sha256-eIESkc/yYiZZ5iY4t/rAPd+jfjuMYR3srCBC4fO3g70=";

  contribSrc = pkgs.fetchFromGitHub {
    owner = "opencv";
    repo = "opencv_contrib";
    rev = "4.5.4";
    sha256 = "sha256-RkCIGukZ8KJkmVZQAZTWdVcVKD2I3NcfGShcqzKhQD0=";

  testDataSrc = pkgs.fetchFromGitHub {
    owner = "opencv";
    repo = "opencv_extra";
    rev = "4.5.4";
    sha256 = "sha256-Ak0sTr4s7tPqRLcuIe+5NVGMz8SwTifYU7vBu1uH4rk=";


You could try loading an older version of the file, or copying the current file and modifying it to your needs. You could also add a nixpkgs input at a revision that contains the package you want, that way you know it will still be compatible with its dependencies.

Thanks. Yeah, using an older version of nixpkgs seems like the most straightforward approach. That way you know all of your packages are consistent. Of course it’s limiting in that you can’t mix and match different versions of different packages, so in some cases I guess you can do more complicated things like copy the text over and make edits as needed.

That all makes sense, I just wasn’t sure if there was a particular established way to do things. Thanks.

You can mix and match a bit by including multiple nixpkgs versions and installing different packages from different ones. There are a few examples of people installing packages from stable and unstable at the same time. If you need a tool to find the right revision for your package, this was posted today:

Yeah, I was thinking about different nixpkgs versions also. I guess where that could get messy is when each package pulls in dependencies from its own version of nixpkgs, so for example you could have two java projects using different versions of the jdk.

nixhub looks cool, thanks for the heads up.