nix-community / poetry2nix

Convert poetry projects to nix automagically [maintainer=@adisbladis,@cpcloud]
MIT License
824 stars 430 forks source link

How to override setuptools <70? #1729

Open heimalne opened 1 month ago

heimalne commented 1 month ago

I have an older torch==1.10.1 package that needs an older setuptools<=70 version due to breaking changes.

With setuptools==70.0.0 i get the following error message, which isn't NixOS or Mac related (See https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/15863#issuecomment-2125026282):

File ".../lib/python3.9/site-packages/torch/torch_version.py", line 3, in <module>
from pkg_resources import packaging  # type: ignore[attr-defined]
<class 'ModuleNotFoundError'> No module named 'pkg_resources'

Describe the issue

However, pinning an older setuptools = { version = "<70" } in pyproject.toml and poetry.lock doesn't download that version. Apparently, either nixpkgs or poetry2nix overrides and sneaks in a newer 70.0.0.post0, which then later leads to an error with nix build:

Executing pythonRuntimeDepsCheck
Checking runtime dependencies for mypackage.whl
  - setuptools<70 not satisfied by version 70.0.0.post0

How would you debug this and override the setuptools version that lands into the poetry2nix venv?

Overriding the Nixpkgs python packages doesn't seem to work and overriding poetry2nix package set doesn't as well:

let
  myPython = let
      packageOverrides = final: prev: {
        setuptools = prev.setuptools.overridePythonAttrs(old: rec {
                pname = "setuptools";
                version = "0.69.5";
                doCheck = false;
                src = final.fetchPypi {
                  inherit pname version;
                  sha256 = "0yk6xl885b91dmlhlsap7x78hk2rdr879fln9anbq6k4ca42djb6";
                };
        });
      };
  in python.override {inherit packageOverrides; self = python;};
in poetry2nix.mkPoetryApplication {
    projectDir = ./.;
    python = myPython;   <-----------------------------------------------------
    overrides = poetry2nix.overrides.withDefaults (final: prev: {
      setuptools = prev.setuptools.overridePythonAttrs(old: rec { <------------------------------------
        pname = "setuptools";
        version = "69.5.1";
        # copied from https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/setuptools/default.nix
        src = fetchFromGitHub {
            owner = "pypa";
            repo = "setuptools";
            rev = "refs/tags/v${version}";
            hash = "sha256-LXF3R9zfWylvihP2M8N94/IlgifwxUwKJFhtvcXEPB0=";
        };
      });
   };
}

Additional context

pyproject.toml:

torch = { version = "1.10.1" }
setuptools = { version = "<70" }
heimalne commented 1 month ago

Maybe related to https://github.com/nix-community/poetry2nix/issues/1616 discussing how to separate build (i think poetry-core needs setuptools 70) from runtime dependencies.

TyberiusPrime commented 1 month ago

I'm pretty sure the setuptools being used comes from nixpkgs.

davidschrooten commented 1 month ago

Same issue here as well, trying to downgrade setuptools < 70.0.0:

 Attempting uninstall: setuptools
    Found existing installation: setuptools 70.0.0.post0
    Not uninstalling setuptools at /nix/store/niacps3c0qx734l2sw9fgxl5gjm365xs-python3.11-setuptools-70.0.0/lib/python3.11/site-packages, outside environment /home/david/Dev/python/ann-experiments/venv
    Can't uninstall 'setuptools'. No files were found to uninstall.

But it gets blocked by nix...

TyberiusPrime commented 1 month ago

Quite often the setup tools incompatibility is minor and a quick patch to the package wanting a setup tools < 70 is all that is needed. Sometimes it's an entirely imaginary upper bound, and all you need is to patch it. (Not saying that's the case for torch though)

This is somewhat common in the python ecosystem, where the upper bounds need to be defined on release and can't be updated later on

davidschrooten commented 1 month ago

Quite often the setup tools incompatibility is minor and a quick patch to the package wanting a setup tools < 70 is all that is needed. Sometimes it's an entirely imaginary upper bound, and all you need is to patch it. (Not saying that's the case for torch though)

This is somewhat common in the python ecosystem, where the upper bounds need to be defined on release and can't be updated later on

It’s requesting an import that isn’t there in setuptools 70.x. There is no version constraint.