Wrap a python application with a custom python interpreter

Hi there, I am trying to wrap a python application using buildPython(Package|Application) which contains a set of hardcoded calls to python modules (python -m pip …, etc). I keep getting No module named pip errors because the wrapping script appends the bare python interpreter to the path after all other dependencies. Therefore, even if I add a python.withPackages to dependencies, calls to python will always use the default wrapped interpreter.

Is there any way to pass a custom version of python to buildPython(Package|Application) apart from using an overlay? I don’t want to pollute the environment for other packages depending on python. Some override of wrapPython might do but I havne’t been able to figure it out. Many thanks!

Exerpt from a typical wrapped script

PATH=${PATH:+':'$PATH':'} # java jdk dependency
PATH=${PATH/':''/nix/store/1fylwjqscywqb5xxsi258h0ka3pz4ax6-openjdk-17.0.11+9/bin'':'/':'}
PATH='/nix/store/1fylwjqscywqb5xxsi258h0ka3pz4ax6-openjdk-17.0.11+9/bin'$PATH
PATH=${PATH#':'}
PATH=${PATH%':'}
export PATH
PATH=${PATH:+':'$PATH':'} # custom python environment
PATH=${PATH/':''/nix/store/y0kpp6p2hm8jmdzbnaw1p63hvklspi4d-python3-3.12.6-env/bin'':'/':'}
PATH='/nix/store/y0kpp6p2hm8jmdzbnaw1p63hvklspi4d-python3-3.12.6-env/bin'$PATH
PATH=${PATH#':'}
PATH=${PATH%':'}
export PATH
PATH=${PATH:+':'$PATH':'} # default python that stops me using my custom python >:(
PATH=${PATH/':''/nix/store/wfbjq35kxs6x83c3ncpfxdyl5gbhdx4h-python3-3.12.6/bin'':'/':'}
PATH='/nix/store/wfbjq35kxs6x83c3ncpfxdyl5gbhdx4h-python3-3.12.6/bin'$PATH
PATH=${PATH#':'}
PATH=${PATH%':'}
export PATH

EDIT: I got this working by adding

let
python = python3.withPackages (ps: [ps.pip]);
in
  makeWrapperArgs  = [ "--set PATH ${lib.getExe python}:$PATH"];

As this appends my python with pip available to the path. However, I consider this an incredibly ineligant solution and am looking for something a bit better.

EDIT2: No this doesn’t work as there is a shebang !#/path/to/base-python-interpreter in .wrapped-script which causes sys.executable to be set to the wrong value.