nixpkgs/pkgs/build-support/fetchpypilegacy/default.nix
adisbladis d52b3a7cc0 fetchPypiLegacy: init PyPi legacy API fetcher
This fetcher is to be used with PyPi mirrors exposing the "legacy" API, such as devpi.

A variant of this fetcher has been used in poetry2nix for years and
has served us well there to support private PyPi mirrors and Devpi.

Example usage:
``` nix
fetchPypiLegacy {
  file = "urllib3-1.26.2.tar.gz";
  hash = "sha256:19188f96923873c92ccb987120ec4acaa12f0461fa9ce5d3d0772bc965a39e08";
  pname = "urllib3";
  url = "https://pypi.org/simple";
}
```

cc @lewo who wrote the this originally
cc contributors @rskew @gmacon @jperras @Smaug123
2024-02-17 17:11:59 +13:00

45 lines
1.0 KiB
Nix

# Fetch from PyPi legacy API as documented in https://warehouse.pypa.io/api-reference/legacy.html
{ runCommand
, lib
, python3
}:
{
# package name
pname,
# Package index
url ? null,
# Multiple package indices to consider
urls ? [ ],
# filename including extension
file,
# SRI hash
hash,
}:
let
urls' = urls ++ lib.optional (url != null) url;
pathParts = lib.filter ({ prefix, path }: "NETRC" == prefix) builtins.nixPath;
netrc_file =
if (pathParts != [ ])
then (lib.head pathParts).path
else "";
in
# Assert that we have at least one URL
assert urls' != [ ]; runCommand file
{
nativeBuildInputs = [ python3 ];
impureEnvVars = lib.fetchers.proxyImpureEnvVars;
outputHashMode = "flat";
outputHashAlgo = null;
outputHash = hash;
NETRC = netrc_file;
passthru = {
urls = urls';
};
}
''
python ${./fetch-legacy.py} ${lib.concatStringsSep " " (map (url: "--url ${lib.escapeShellArg url}") urls')} --pname ${pname} --filename ${file}
mv ${file} $out
''