Skip to content

python3Packages.torch-bin: fix Python dependencies issues - #123

Merged
danieldk merged 1 commit into
mainfrom
check-and-relax-torch-bin-deps
Oct 28, 2025
Merged

python3Packages.torch-bin: fix Python dependencies issues#123
danieldk merged 1 commit into
mainfrom
check-and-relax-torch-bin-deps

Conversation

@danieldk

Copy link
Copy Markdown
Member

Python packages in Nix do keep their wheel metadata. This metadata is not used when the packages are used normally in Nix. But the metadata is used by e.g. pip in a venv with Nix-managed packages.

The torch-bin derivations copy the metadata from the original wheel. The original Torch wheels have dependencies on CUDA/OneAPI packaged as Python packages. This becomes an issue when we want to use Torch inside a venv, such as kernel-builder development shells. When we pip install something (like a kernel), pip realizes that the dependencies are missing and attempts to install CUDA/OneAPI. However, this is both unnecessary (Nix already provides these frameworks) and typically fails.

This PR addresses this issue by specifying what dependencies should be removed from the metadata. Nix has the pythonRelaxDepsHook hook for this. However it fires to early, because it is made for from-source builds. So we vendor this hook as pythonRelaxWheelDepsHook.

The new hook only addresses part of the problem, since new dependencies might be added in the future, and if we miss them we'll silently have the same issue again. So in addition to removing unneeded dependencies, we also want to make sure that all remaining dependencies are satisfied. To do this, we vendor the pythonRuntimeDepsCheckHook as pythonWheelDepsCheckHook and modify it to work with binary wheels. With this hook set up, we will be warned of future dependency additions (both needed and unneeded).

Python packages in Nix do keep their wheel metadata. This metadata is
not used when the packages are used normally in Nix. But the metadata is
used by e.g. `pip` in a venv with Nix-managed packages.

The torch-bin derivations copy the metadata from the original wheel.
The original Torch wheels have dependencies on CUDA/OneAPI packaged as
Python packages. This becomes an issue when we want to use Torch inside
a venv, such as kernel-builder development shells. When we `pip install`
something (like a kernel), pip realizes that the dependencies are
missing and attempts to install CUDA/OneAPI. However, this is both
unnecessary (Nix already provides these frameworks) and typically fails.

This PR addresses this issue by specifying what dependencies should be
removed from the metadata. Nix has the `pythonRelaxDepsHook` hook for
this. However it fires to early, because it is made for from-source
builds. So we vendor this hook as `pythonRelaxWheelDepsHook`.

The new hook only addresses part of the problem, since new dependencies
might be added in the future, and if we miss them we'll silently have
the same issue again. So in addition to removing unneeded dependencies,
we also want to make sure that all remaining dependencies are satisfied.
To do this, we vendor the `pythonRuntimeDepsCheckHook` as
`pythonWheelDepsCheckHook` and modify it to work with binary wheels.
With this hook set up, we will be warned of future dependency additions
(both needed and unneeded).
@@ -0,0 +1,93 @@
# shellcheck shell=bash

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These scripts come from nixpkgs with some small changes to work with binary packages.

@drbh drbh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice fix, LGTM

@danieldk
danieldk merged commit 281d885 into main Oct 28, 2025
1 check passed
@danieldk
danieldk deleted the check-and-relax-torch-bin-deps branch October 28, 2025 06:42

@MekkCyber MekkCyber left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants