Python error possibly because of symbolic links

I was doing some tests with itk-elastix, a Python interface to Elastix. This lib depends on ITK and other ITK libs. The interesting thing is that it install the lib files inside the itk folder, not inside itk-elastix. Every Python lib in Nix is stored inside a folder in the nix store and every python env is created using links to that python lib folders. In the case of ITK and ITK-elastix inside the itk package in the python env there are links to ITK, ITK-elastix and other ITK libs. Because of this behavior, I’m having a problem with ITK-elastix in NixOS. If I try to use some function from ITK-elastix this errors happen:

❯ python
Python 3.9.6 (default, Jun 28 2021, 08:57:49) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import itk
>>> itk.elastix_registration_method
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/lazy.py", line 132, in __getattribute__
    base.itk_load_swig_module(module, namespace)
  File "/nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/base.py", line 102, in itk_load_swig_module
    itk_load_swig_module(dep, namespace)
  File "/nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/base.py", line 110, in itk_load_swig_module
    l_module = loader.load(swig_module_name)
  File "/nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/base.py", line 259, in load
    l_spec.loader.exec_module(l_module)  # pytype: disable=attribute-error
  File "<frozen importlib._bootstrap_external>", line 846, in exec_module
  File "<frozen importlib._bootstrap_external>", line 982, in get_code
  File "<frozen importlib._bootstrap_external>", line 1039, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '/nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/../ITKSmoothingPython.py'
>>> 

The file ITKSmoothingPython.py exists. The folder support is a link to other folder in nix store:

ls -l /nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk | grep -i support
lrwxrwxrwx 2 root root   114 dez 31  1969 support -> /nix/store/prxnkfm8ram0jjzygn3qqyl28511bbbc-python3.9-itk-core-5.2.1.post1/lib/python3.9/site-packages/itk/support

It seems to me that this error is happening because it’s trying to find ITKSmoothingPython.py inside
/nix/store/prxnkfm8ram0jjzygn3qqyl28511bbbc-python3.9-itk-core-5.2.1.post1/lib/python3.9/site-packages/itk/ not inside /nix/store/b44fp8zxz8fmqb2r1kmzlnh8v2h8vldh-python3-3.9.6-env/lib/python3.9/site-packages/itk/support/ and that is happening because support folder is a link. I test this using Mach-nix and Poetry2nix and this problem happens in both cases. The files that I used in poetry2nix:

pyproject.toml:

[tool.poetry]
name = "test_itk"
version = "0.1.0"
description = ""
authors = ["manolo <manolo@manolo.com>"]

[tool.poetry.dependencies]
python = "^3.9"
itk-elastix = "^0.13.0"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

shell.nix:

{ pkgs ? import <nixpkgs> {} }:
let
  myAppEnv = pkgs.poetry2nix.mkPoetryEnv {
    projectDir = ./.;
    preferWheels = true;
  };
in myAppEnv.env

Is there a way to avoid this behavior of creating links?

1 Like