How to override a Python package using callPackage from an overlay

I’d like to override a Python package from an overlay by replacing it completely using another .nix file using callPackage. I tried to adapt the various guides like Overlays - NixOS Wiki, Using python.withPackages with nixpkgs-overlays does not work · Issue #26487 · NixOS/nixpkgs · GitHub or How I can successfully override a python lib with overlays?, leading to an overlay like this:

final: prev:
rec {
  python310 = prev.python310.override {
    packageOverrides = pyfinal: pyprev: {
      tzlocal = final.callPackage ../tzlocal.nix
      {
        inherit (final.python310.pkgs) buildPythonPackage pythonOlder setuptools wheel pytz-deprecation-shim pytest-mock pytestCheckHook;
      };
    };
  };

  python310Packages = python310.pkgs;

  tzlocal = final.callPackage ../tzlocal.nix {
    inherit (final.python310.pkgs) buildPythonPackage pythonOlder setuptools wheel pytz-deprecation-shim pytest-mock pytestCheckHook;
  };
}

This way, ../tzlocal.nix should be used instead of the file from nixpkgs. The tzlocal Python package isn’t overridden, though. When trying to install it directly as python3.10-tzlocal, the original package from nixpkgs is installed. Same goes when installing a package that depends on tzlocal. I also tried overriding python3 or python instead, but it did not make a difference.

../tzlocal.nix is processed, though. It’s just not used as a replacement for the original package file.

How to best override the Python package?

That is a really good question, I have a feeling I am having the same problem right now. Did you find a solution? @gm6k

See my reply in this thread: Overriding Python Modules - #4 by alexv. callPackage should be from python, not from nixpgks.

2 Likes

I think you just gave me a very important hint! I will investigate later.

Thank you a lot in advance :smile:

Thanks! I can confirm it finally worked using callPackage from Python, and pythonPackagesExtensions.

1 Like