summary refs log tree commit diff
path: root/pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/development/tools/poetry2nix/poetry2nix/lib.nix')
-rw-r--r--pkgs/development/tools/poetry2nix/poetry2nix/lib.nix85
1 files changed, 51 insertions, 34 deletions
diff --git a/pkgs/development/tools/poetry2nix/poetry2nix/lib.nix b/pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
index bdd30cbffa8..cb1cd8af131 100644
--- a/pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
+++ b/pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
@@ -1,4 +1,4 @@
-{ lib, pkgs }:
+{ lib, pkgs, stdenv }:
 let
   inherit (import ./semver.nix { inherit lib ireplace; }) satisfiesSemver;
   inherit (builtins) genList length;
@@ -93,17 +93,19 @@ let
   );
 
 
-  # Fetch the wheels from the PyPI index.
-  # We need to first get the proper URL to the wheel.
+  # Fetch from the PyPI index.
+  # At first we try to fetch the predicated URL but if that fails we
+  # will use the Pypi API to determine the correct URL.
   # Args:
   #   pname: package name
   #   file: filename including extension
+  #   version: the version string of the dependency
   #   hash: SRI hash
   #   kind: Language implementation and version tag
-  fetchWheelFromPypi = lib.makeOverridable (
-    { pname, file, hash, kind, curlOpts ? "" }:
+  fetchFromPypi = lib.makeOverridable (
+    { pname, file, version, hash, kind, curlOpts ? "" }:
     let
-      version = builtins.elemAt (builtins.split "-" file) 2;
+      predictedURL = predictURLFromPypi { inherit pname file hash kind; };
     in
     (pkgs.stdenvNoCC.mkDerivation {
       name = file;
@@ -111,7 +113,7 @@ let
         pkgs.curl
         pkgs.jq
       ];
-      isWheel = true;
+      isWheel = lib.strings.hasSuffix "whl" file;
       system = "builtin";
 
       preferLocalBuild = true;
@@ -119,36 +121,35 @@ let
         "NIX_CURL_FLAGS"
       ];
 
-      predictedURL = predictURLFromPypi { inherit pname file hash kind; };
-      inherit pname file version curlOpts;
+      inherit pname file version curlOpts predictedURL;
 
-      builder = ./fetch-wheel.sh;
+      builder = ./fetch-from-pypi.sh;
 
       outputHashMode = "flat";
       outputHashAlgo = "sha256";
       outputHash = hash;
+
+      passthru = {
+        urls = [ predictedURL ]; # retain compatibility with nixpkgs' fetchurl
+      };
     })
   );
 
-  # Fetch the artifacts from the PyPI index. Since we get all
-  # info we need from the lock file we don't use nixpkgs' fetchPyPi
-  # as it modifies casing while not providing anything we don't already
-  # have.
-  #
-  # Args:
-  #   pname: package name
-  #   file: filename including extension
-  #   hash: SRI hash
-  #   kind: Language implementation and version tag https://www.python.org/dev/peps/pep-0427/#file-name-convention
-  fetchFromPypi = lib.makeOverridable (
-    { pname, file, hash, kind }:
-    if lib.strings.hasSuffix "whl" file then fetchWheelFromPypi { inherit pname file hash kind; }
-    else
-      pkgs.fetchurl {
-        url = predictURLFromPypi { inherit pname file hash kind; };
-        inherit hash;
-      }
+  fetchFromLegacy = lib.makeOverridable (
+    { python, pname, url, file, hash }:
+    pkgs.runCommand file
+      {
+        nativeBuildInputs = [ python ];
+        impureEnvVars = lib.fetchers.proxyImpureEnvVars;
+        outputHashMode = "flat";
+        outputHashAlgo = "sha256";
+        outputHash = hash;
+      } ''
+      python ${./fetch_from_legacy.py} ${url} ${pname} ${file}
+      mv ${file} $out
+    ''
   );
+
   getBuildSystemPkgs =
     { pythonPackages
     , pyProject
@@ -156,12 +157,10 @@ let
     let
       missingBuildBackendError = "No build-system.build-backend section in pyproject.toml. "
         + "Add such a section as described in https://python-poetry.org/docs/pyproject/#poetry-and-pep-517";
-      buildSystem = lib.attrByPath [ "build-system" "build-backend" ] (throw missingBuildBackendError) pyProject;
-      drvAttr = moduleName (builtins.elemAt (builtins.split "\\.|:" buildSystem) 0);
+      requires = lib.attrByPath [ "build-system" "requires" ] (throw missingBuildBackendError) pyProject;
+      requiredPkgs = builtins.map (n: lib.elemAt (builtins.match "([^!=<>~[]+).*" n) 0) requires;
     in
-    if buildSystem == "" then [ ] else (
-      [ pythonPackages.${drvAttr} or (throw "unsupported build system ${buildSystem}") ]
-    );
+    builtins.map (drvAttr: pythonPackages.${drvAttr} or (throw "unsupported build system requirement ${drvAttr}")) requiredPkgs;
 
   # Find gitignore files recursively in parent directory stopping with .git
   findGitIgnores = path:
@@ -196,11 +195,28 @@ let
         inherit src;
       };
     };
+
+  # Maps Nixpkgs CPU values to target machines known to be supported for manylinux* wheels.
+  # (a.k.a. `uname -m` output from CentOS 7)
+  #
+  # This is current as of manylinux2014 (PEP-0599), and is a superset of manylinux2010 / manylinux1.
+  # s390x is not supported in Nixpkgs, so we don't map it.
+  manyLinuxTargetMachines = {
+    x86_64 = "x86_64";
+    i686 = "i686";
+    aarch64 = "aarch64";
+    armv7l = "armv7l";
+    powerpc64 = "ppc64";
+    powerpc64le = "ppc64le";
+  };
+
+  # Machine tag for our target platform (if available)
+  targetMachine = manyLinuxTargetMachines.${stdenv.targetPlatform.parsed.cpu.name} or null;
 in
 {
   inherit
     fetchFromPypi
-    fetchWheelFromPypi
+    fetchFromLegacy
     getManyLinuxDeps
     isCompatible
     readTOML
@@ -209,5 +225,6 @@ in
     cleanPythonSources
     moduleName
     getPythonVersion
+    targetMachine
     ;
 }