From 6c0b85cf3abefb59502093f4183cbf49a2f774a0 Mon Sep 17 00:00:00 2001 From: DavHau Date: Sun, 16 May 2021 15:13:39 +0700 Subject: buildPythonPackage: add support for conda --- .../interpreters/python/conda/default.nix | 25 ++++++++++++++++ pkgs/development/interpreters/python/default.nix | 2 ++ .../python/hooks/conda-install-hook.sh | 27 +++++++++++++++++ .../interpreters/python/hooks/conda-unpack-hook.sh | 18 +++++++++++ .../interpreters/python/hooks/default.nix | 15 ++++++++++ pkgs/development/interpreters/python/tests.nix | 35 +++++++++++++++++++++- 6 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 pkgs/development/interpreters/python/conda/default.nix create mode 100644 pkgs/development/interpreters/python/hooks/conda-install-hook.sh create mode 100644 pkgs/development/interpreters/python/hooks/conda-unpack-hook.sh (limited to 'pkgs/development/interpreters/python') diff --git a/pkgs/development/interpreters/python/conda/default.nix b/pkgs/development/interpreters/python/conda/default.nix new file mode 100644 index 00000000000..5249ccce0d9 --- /dev/null +++ b/pkgs/development/interpreters/python/conda/default.nix @@ -0,0 +1,25 @@ +{ pkgs }: { + + # List of libraries that are needed for conda binary packages. + # When installing a conda binary package, just extend + # the `buildInputs` with `condaAutopatchLibs`. + condaPatchelfLibs = builtins.map (p: p.lib or p) ([ + pkgs.alsaLib + pkgs.cups + pkgs.gcc-unwrapped + pkgs.libGL + ] ++ (with pkgs.xorg; [ + libSM + libICE + libX11 + libXau + libXdamage + libXi + libXrender + libXrandr + libXcomposite + libXcursor + libXtst + libXScrnSaver]) + ); +} diff --git a/pkgs/development/interpreters/python/default.nix b/pkgs/development/interpreters/python/default.nix index 328ed715bae..30134a05628 100644 --- a/pkgs/development/interpreters/python/default.nix +++ b/pkgs/development/interpreters/python/default.nix @@ -44,6 +44,8 @@ with pkgs; toPythonModule toPythonApplication buildSetupcfg + condaInstallHook + condaUnpackHook eggUnpackHook eggBuildHook eggInstallHook diff --git a/pkgs/development/interpreters/python/hooks/conda-install-hook.sh b/pkgs/development/interpreters/python/hooks/conda-install-hook.sh new file mode 100644 index 00000000000..af9ed60a038 --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/conda-install-hook.sh @@ -0,0 +1,27 @@ +# Setup hook to use in case a conda binary package is installed +echo "Sourcing conda install hook" + +condaInstallPhase(){ + echo "Executing condaInstallPhase" + runHook preInstall + + # There are two different formats of conda packages. + # It either contains only a site-packages directory + # or multiple top level directories. + siteDir=@pythonSitePackages@ + if [ -e ./site-packages ]; then + mkdir -p $out/$siteDir + cp -r ./site-packages/* $out/$siteDir + else + cp -r . $out + rm $out/env-vars + fi + + runHook postInstall + echo "Finished executing condaInstallPhase" +} + +if [ -z "${installPhase-}" ]; then + echo "Using condaInstallPhase" + installPhase=condaInstallPhase +fi diff --git a/pkgs/development/interpreters/python/hooks/conda-unpack-hook.sh b/pkgs/development/interpreters/python/hooks/conda-unpack-hook.sh new file mode 100644 index 00000000000..6204c13b722 --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/conda-unpack-hook.sh @@ -0,0 +1,18 @@ +# Setup hook to use in case a conda binary package is fetched +echo "Sourcing conda unpack hook" + +condaUnpackPhase(){ + echo "Executing condaUnpackPhase" + runHook preUnpack + + # use lbzip2 for parallel decompression (bz2 is slow) + lbzip2 -dc -n $NIX_BUILD_CORES $src | tar --exclude='info' -x + + # runHook postUnpack # Calls find...? + echo "Finished executing condaUnpackPhase" +} + +if [ -z "${unpackPhase-}" ]; then + echo "Using condaUnpackPhase" + unpackPhase=condaUnpackPhase +fi diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 1a64c79232b..1a0618225a3 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -16,6 +16,21 @@ let setuppy = ../run_setup.py; in rec { + condaInstallHook = callPackage ({ gnutar, lbzip2 }: + makeSetupHook { + name = "conda-install-hook"; + deps = [ gnutar lbzip2 ]; + substitutions = { + inherit pythonSitePackages; + }; + } ./conda-install-hook.sh) {}; + + condaUnpackHook = callPackage ({}: + makeSetupHook { + name = "conda-unpack-hook"; + deps = []; + } ./conda-unpack-hook.sh) {}; + eggBuildHook = callPackage ({ }: makeSetupHook { name = "egg-build-hook.sh"; diff --git a/pkgs/development/interpreters/python/tests.nix b/pkgs/development/interpreters/python/tests.nix index cfba04da938..872123338f8 100644 --- a/pkgs/development/interpreters/python/tests.nix +++ b/pkgs/development/interpreters/python/tests.nix @@ -121,4 +121,37 @@ let # in assert myPackages.foobar == myPackages.numpy; myPackages.python.withPackages(ps: with ps; [ foobar ]); }; -in lib.optionalAttrs (stdenv.hostPlatform == stdenv.buildPlatform ) (environmentTests // integrationTests // overrideTests) + condaTests = let + requests = callPackage ({ + autoPatchelfHook, + fetchurl, + pythonCondaPackages, + }: + python.pkgs.buildPythonPackage { + pname = "requests"; + version = "2.24.0"; + format = "other"; + src = fetchurl { + url = "https://repo.anaconda.com/pkgs/main/noarch/requests-2.24.0-py_0.tar.bz2"; + sha256 = "02qzaf6gwsqbcs69pix1fnjxzgnngwzvrsy65h1d521g750mjvvp"; + }; + nativeBuildInputs = [ autoPatchelfHook ] ++ (with python.pkgs; [ + condaUnpackHook condaInstallHook + ]); + buildInputs = [ + pythonCondaPackages.condaPatchelfLibs + ]; + propagatedBuildInputs = with python.pkgs; [ + chardet idna urllib3 certifi + ]; + } + ) {}; + pythonWithRequests = requests.pythonModule.withPackages (ps: [ requests ]); + in + { + condaExamplePackage = runCommand "import-requests" {} '' + ${pythonWithRequests.interpreter} -c "import requests" > $out + ''; + }; + +in lib.optionalAttrs (stdenv.hostPlatform == stdenv.buildPlatform ) (environmentTests // integrationTests // overrideTests // condaTests) -- cgit 1.4.1