Is there a way to “inject” a dependency into a build or to override a dependency?

I am trying to use the gymnasium library with the box2d feature as a dependency in a hatch project.
This feature depends on box2d-py via pyproject.toml. This package uses setuptools with setup.py to build, but relies on a tool called swig being installed.
My operating system has a swig package, but it is a newer version that no longer supports a legacy feature required by box2d-py.
Luckily, there is a PyPI binary package called swig that provides the specific version of swig.
How do I force box2d-py to be built using the PyPI swig package?
An obvious solution is to have local patched versions of box2d-py and gymnasium, but this is suboptimal.
At some point during this process, pip or hatch builds box2d-py and fails.
I would like for it to use the PyPI swig package at this stage.

Is this possible in some way?

So, pip has a way to disable “build isolation”. So you could create a new fresh virtual environment, install this swig package that you want in that environment with pip as usual, then install box2-py with pip in that same environment but with disabled “build isolation”, so that when box2-py gets built the swig in the environment is taken into account. From then on, pip should have a built version of box2-py in its build cache and you can go back to trying to install gymnasium as you wanted to do in the beginning. Maybe I have the steps wrong, but in principle I think something like this should work.

References: