You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I made a packaging attempt here: bjornfor@63d7b5a. The major thing that's missing is how to make the python module (or libclang) find system headers. It'd be nice to have some sort of wrapper, but how to wrap a python module? (As a workaround one can wrap the python interpreter.)
@bjornfor Nix does not have global python modules by design! 🙂 You always have to use wrapper like python3.withPackages.
I don't quite understand your question though. What are system headers?
Nevertheless I've just spent a few hours playing with your attempt and the only thing missing for the new package to be usable when put to buildPythonPackage.propagatedBuildInputs of other packages are metadata.
I've just added a proper pyproject.toml and setup.cfg and installed the package using
which correctly generated required metadata into site-packages.
I'd like to use nixpkgs's buildPythonPackage or other functions though but I don't know how YET. I'll investigate further.
I don't quite understand your question though. What are system headers?
I mean files like stdio.h and iostream. I had to wrap the python interpreter to make things work (i.e. have the clang.cindex python module find those files). I used nixpkgs clang-tools/wrapper as inspiration.
Nevertheless I've just spent a few hours playing with your attempt and the only thing missing for the new package to be usable when put to buildPythonPackage.propagatedBuildInputs of other packages are metadata.
Yay!
But what metadata? My first thought was meta.{homepage,license} etc. but then it looks like you're talking about something different. Where did you put that python -m pip --no-cache-dir install ... command? Ah, you ran it manually? Why? Did you try python3.pkgs.withPackages (ps: [ ps.libclang_12 ])?
Have a look at 5b2bbd54b2052b3eef34838ef1e71db83baf0053.
You can build it by nix build .#python3Packages.libclang_12 or nix-build ./. -A python3Packages.libclang_12
and you can drop into a shell with clang bindings installed by:
I can confirm that I got clang python packages working in #137069. We can add any other versions people need once that lands!
The clang python package introduced in #137069 has a major flaw though. It does not have a default link to the libclang and using it directly fails on
clang.cindex.LibclangError: lib/libclang.so: cannot open shared object file: No such file or directory. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
@bjornfor 's approach is much better IMHO because:
It does not use the PYPI package but directly (re)uses the same source code clang compiler and libclang library is built from.
The only thing that was missing was that it must by installed by pip to the final destination and not just be cp-ed there otherwise while being directly usable by python interpret it is not usable by python's build system and cannot be used as a dependency in other packages then.
I'm trying to upgrade the CadQuery derivation, but due to a tree of dependency changes, I now also need clang's python bindings to exist.
@grawp the commit you referenced for "how to make the pip installation work" no longer exists. Do you still have WIP code to demonstrate how you got pip to work correctly? IIUC, it's the only blocker to getting @bjornfor's changes into a formal PR, so that I can start walking back up the dependency tree and upgrade the derivation I care about :)
It'd be nice to have some sort of wrapper, but how to wrap a python module? (As a workaround one can wrap the python interpreter.)
I recently wrote a python implementation of the clang wrapper and injected it to the python libclang package __init__.py. It feels a bit hack'y, because importing clang.cindex sets env vars $CPATH and $CPLUS_INCLUDE_PATH, but at least it only affects the python process importing that specific module (instead of all python processes having that module available).
You can find the implementation below. (Sorry, I don't have time to make a PR yet, but just wanted to get this out there.)
libclang-inject-header-paths.py.in:
# This code is (to be) appended to the python libclang module __init__.py file.## Pushing wrapping into the module itself, instead of the python interpreter,# minimizes leaking environment variables like $CPATH (which, when containing# *clang* headers, breaks gcc).## Still, after the libclang/cindex module has been imported, the python process# can no longer be used to invoke gcc as the $CPATH etc. envvars set here break# it. But that's luckily a rare use case.## Inspired by <nixpkgs>/pkgs/development/tools/clang-tools/wrapper.def_buildcpath(args):
"""Extract and return include paths from the given compiler include options. Signature: List[str] -> List[str] """path= []
after= []
whilelen(args) >0:
ifargs[0] =="-isystem":
args.pop(0)
path.append(args[0])
elifargs[0] =="-idirafter":
args.pop(0)
after.append(args[0])
args.pop(0)
returnpath+afterdef_get_flags(file):
"""Read file, return list of words. Signature: Path -> List[str] """returnopen(file).read().split()
def_set_compiler_env_vars():
"""Set $CPATH and $CPLUS_INCLUDE_PATH env vars from $NIX_CFLAGS_COMPILE"""importosnix_cflags_compile=os.environ.get("NIX_CFLAGS_COMPILE", "").split()
orig_cpath=os.environ.get("CPATH", "").split()
orig_cplus_include_path=os.environ.get("CPLUS_INCLUDE_PATH", "").split()
new_cpath= (
orig_cpath+_buildcpath(nix_cflags_compile+_get_flags("@clang@/nix-support/libc-cflags"))
+ ["@clang@/resource-root/include"]
)
new_cplus_include_path= (
orig_cplus_include_path+_buildcpath(
nix_cflags_compile+_get_flags("@clang@/nix-support/libcxx-cxxflags")
+_get_flags("@clang@/nix-support/libc-cflags")
)
+ ["@clang@/resource-root/include"]
)
os.environ["CPATH"] =":".join(new_cpath)
os.environ["CPLUS_INCLUDE_PATH"] =":".join(new_cplus_include_path)
_set_compiler_env_vars()
And mach-nix-overrides.nix file to apply it:
# mach-nix overrides.{pkgs}: [(pyfinal: pyprev: {# Patch/wrap libclang to find system headers. Do it in postInstall to work# even when mach-nix package provider is "wheel".## The clang version must be selected by the caller, for example:## mach-nix.mkPython {# ...# overridesPost = import ./this_file.nix { pkgs = pkgs // { clang = pkgs.clang_12; }; };# }libclang=pyprev.libclang.overridePythonAttrs(old: {postInstall=(old.postInstallor"")+'' init_file=$(find "$out" -name __init__.py) if ! [ -f "$init_file" ]; then echo "error: failed to find libclang __init__.py file to patch" >&2 exit 1 fi # Set variable(s) and run substituteAll. export clang="${pkgs.clang}" substituteAll "${./libclang-inject-header-paths.py.in}" ./inject_paths.py # Inject our wrapper code. cat ./inject_paths.py >> "$init_file" '';});})]
Activity
stale commentedon Nov 27, 2020
I marked this as stale due to inactivity. → More info
bjornfor commentedon Sep 1, 2021
I made a packaging attempt here: bjornfor@63d7b5a. The major thing that's missing is how to make the python module (or libclang) find system headers. It'd be nice to have some sort of wrapper, but how to wrap a python module? (As a workaround one can wrap the python interpreter.)
WizardUli commentedon Sep 5, 2021
@bjornfor Nix does not have global python modules by design! 🙂 You always have to use wrapper like
python3.withPackages.I don't quite understand your question though. What are system headers?
Nevertheless I've just spent a few hours playing with your attempt and the only thing missing for the new package to be usable when put to
buildPythonPackage.propagatedBuildInputsof other packages are metadata.I've just added a proper
pyproject.tomlandsetup.cfgand installed the package usingwhich correctly generated required metadata into site-packages.
I'd like to use nixpkgs's
buildPythonPackageor other functions though but I don't know how YET. I'll investigate further.bjornfor commentedon Sep 5, 2021
I mean files like
stdio.handiostream. I had to wrap the python interpreter to make things work (i.e. have theclang.cindexpython module find those files). I used nixpkgs clang-tools/wrapper as inspiration.Yay!
But what metadata? My first thought was
meta.{homepage,license}etc. but then it looks like you're talking about something different. Where did you put thatpython -m pip --no-cache-dir install ...command? Ah, you ran it manually? Why? Did you trypython3.pkgs.withPackages (ps: [ ps.libclang_12 ])?WizardUli commentedon Sep 5, 2021
Have a look at 5b2bbd54b2052b3eef34838ef1e71db83baf0053.
You can build it by
nix build .#python3Packages.libclang_12ornix-build ./. -A python3Packages.libclang_12and you can drop into a shell with clang bindings installed by:
WizardUli commentedon Sep 5, 2021
By metadata I mean:
bjornfor commentedon Sep 14, 2021
#137069 includes a "python3Packages.clang: init at 5.0 and 11.0" commit.
samuela commentedon Sep 16, 2021
I can confirm that I got clang python packages working in #137069. We can add any other versions people need once that lands!
WizardUli commentedon Sep 18, 2021
The clang python package introduced in #137069 has a major flaw though. It does not have a default link to the libclang and using it directly fails on
@bjornfor 's approach is much better IMHO because:
clangcompiler andlibclanglibrary is built from.libclangby patching a default in https://github.com/bjornfor/nixpkgs/blob/63d7b5a236889f223fbfc4b96b2a63b212dba1ae/pkgs/development/compilers/llvm/12/clang/cindex-library-path.patchThe only thing that was missing was that it must by installed by pip to the final destination and not just be cp-ed there otherwise while being directly usable by python interpret it is not usable by python's build system and cannot be used as a dependency in other packages then.
stale commentedon Apr 25, 2022
I marked this as stale due to inactivity. → More info
danderson commentedon May 15, 2022
I'm trying to upgrade the CadQuery derivation, but due to a tree of dependency changes, I now also need clang's python bindings to exist.
@grawp the commit you referenced for "how to make the pip installation work" no longer exists. Do you still have WIP code to demonstrate how you got
pipto work correctly? IIUC, it's the only blocker to getting @bjornfor's changes into a formal PR, so that I can start walking back up the dependency tree and upgrade the derivation I care about :)bjornfor commentedon May 16, 2022
Oh, that reminds me:
I recently wrote a python implementation of the clang wrapper and injected it to the python libclang package
__init__.py. It feels a bit hack'y, because importing clang.cindex sets env vars $CPATH and $CPLUS_INCLUDE_PATH, but at least it only affects the python process importing that specific module (instead of all python processes having that module available).You can find the implementation below. (Sorry, I don't have time to make a PR yet, but just wanted to get this out there.)
libclang-inject-header-paths.py.in:
And
mach-nix-overrides.nixfile to apply it:WizardUli commentedon May 16, 2022
@danderson https://codeberg.org/Uli/nix-things/src/branch/main/overlays/cq/libclang-python.nix
athre0z commentedon Nov 27, 2022
Still relevant.
python3Packages.clangandpython3Packages.libclangmentioned by previous posts no longer seem to exist.WizardUli commentedon Nov 28, 2022
Just reshuffled. See:
https://codeberg.org/Uli/nix-things/src/commit/776519e382c81b136c1d0b10d8c7b52b4acb9192/overlays/cq/python/libclang-python.nix
https://codeberg.org/Uli/nix-things/src/commit/776519e382c81b136c1d0b10d8c7b52b4acb9192/overlays/cq/python/default.nix
lilyinstarlight commentedon Nov 2, 2023
There's a
python3Packages.libclanginspired by @WizardUli's in #261113, by the waylilyinstarlight commentedon Nov 22, 2023
Can this be closed now that the
python3Packages.libclangderivation from #261113 is onmaster, or is anything else still missing?athre0z commentedon Feb 6, 2024
Not OP, but at least from my perspective this is a solved problem. Thanks for adding this! :)
lilyinstarlight commentedon Feb 6, 2024
Okay, I'll close this then. Anyone feel free to reopen if anything is missing :)