How to avoid dependency loops?

I have two python packages that depend on each other
when building them I get error: infinite recursion encountered

{buildPythonPackage

, pythonOlder

, setuptools

, package2
}:

buildPythonPackage {
  pname = "package1";
  version = "0.0.0";
  pyproject = true;

  disabled = pythonOlder "3.7";

  src = {};

  build-system = [
    setuptools
  ];

  dependencies = [
    package2
  ];
}
{buildPythonPackage

, pythonOlder

, setuptools

, package1
}:

buildPythonPackage {
  pname = "package2";
  version = "0.0.0";
  pyproject = true;

  disabled = pythonOlder "3.7";

  src = {};

  build-system = [
    setuptools
  ];

  dependencies = [
    package1
  ];
}

Don’t do this. Make a third package which has the code required by the other two instead

I don’t need a third package, I want to nixify the packages I already have.

You can’t do that without a third package, it should either be a dependency of other two or vice versa

Not saying something new but let me recap so that it’s maybe more obvious what is said.

It’s always a bad design to have a circular dependency, as you will never be able to bootstrap those packages and this problem is not nix related, it’s an issue when trying to build the library isolated. You need one of those already build externally to be able to build the other. Nix is just enforcing the isolation.

A common way of solving a circular dependent is to externalize the common function (which make them depend on each other is the circle), so that you can build that Library first and then can build the two other packages independently).

If the libraries are your own, you could adapt the source code, if they are not yours, youwill not be able to build from source isolated. You could grab them possibly from pip if they are available there.

1 Like

I get it now, thanks for the explanation.
unfortunately the packages are not mine, and it will take a long time to solve this problem, so I don’t think the developer will do it…

Are the packages available in Pip? If yes, it might be a valid use-case for you to package them with fetchPypi instead of building them from source.

Yes they are available in pip, but I don’t like that solution. As far as I’ve heard building from non-source is not exactly the Nix way… I’ll leave those packages aside for now