Overriding python version and package scope of a specific package

Hello,

Suppose that some package is defined in nixpkgs’ top-level/all-packages.nix as follows:

{
  something = python312Packages.callPackage ../something.nix { };
}

Is there a way for the end-user to override the version of the Python interpreter and whole Python package scope used when calling that specific package?

For example, for using Python version 3.11 instead of 3.12,
I’m looking for the same effects as replacing that line with:

{
  something = python311Packages.callPackage ../something.nix { };
}

but without forking nixpkgs.

I tried with:

(pkgs.something.override pkgs.python311Packages)

but this results in:

error: function 'anonymous lambda' called with unexpected argument 'args

I just had more or less the same need, I wanted to run kodi with python 3.11 as dependency instead of the default python3Packages, which defaults to 3.12 because of a bug.

I used something like

{ pkgs, ... }:
let
    kodi-py311 = pkgs.kodi-gbm.override { python3Packages = pkgs.python311Packages; };
in
{
  # use kodi-py311 here...
}

Obviously this means that I needed to build kodi from source, but it worked like a charm solved my issue.

I hope this could help you in some way.