forked from DavHau/nix-pypi-fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
76 lines (73 loc) · 2.19 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
fetcherSrc ? ./.,
pkgs,
...
}:
with pkgs;
with builtins;
with lib;
let
normalizeName = name: (replaceStrings ["_"] ["-"] (toLower name));
nameToBucket = name:
let
pkg_name = normalizeName name;
pkg_name_first_char = elemAt (stringToCharacters pkg_name) 0;
bucket_hash_full = hashString "sha256" pkg_name;
in
elemAt (stringToCharacters bucket_hash_full) 0
+ elemAt (stringToCharacters bucket_hash_full) 1;
releaseInfo = name: ver:
let
pkg_name = normalizeName name;
bucket = nameToBucket name;
release = (fromJSON (readFile (fetcherSrc + /pypi + "/${bucket}.json")))."${pkg_name}"."${ver}";
in
{
inherit pkg_name release;
pkg_name_first_char = elemAt (stringToCharacters pkg_name) 0;
};
# collect the list of package names inside a derivation for faster reading and proper caching
allNamesJsonFile = runCommand "all-package-names" { buildInputs = [ python3 ]; } ''
${python3}/bin/python -c '
import json
from os import environ
buckets = []
for a in "0123456789abcdef":
for b in "0123456789abcdef":
buckets.append(a + b)
all_names = []
for bucket in buckets:
with open("${fetcherSrc}/pypi/" + bucket + ".json") as f:
all_names += list(json.load(f).keys())
with open(environ.get("out"), "w") as out:
json.dump(all_names, out)
'
'';
allNames = fromJSON (readFile allNamesJsonFile);
in
rec {
inherit allNames;
fetchPypi = fetchPypiSdist;
fetchPypiSdist = pkg: ver:
with releaseInfo pkg ver;
let
sha256 = elemAt release."sdist" 0;
filename = elemAt release."sdist" 1;
url = "https://files.pythonhosted.org/packages/source/" + pkg_name_first_char + "/" + pkg_name
+ "/" + filename;
in
pkgs.fetchurl {
inherit url sha256;
};
fetchPypiWheel = pkg: ver: fn:
with releaseInfo pkg ver;
let
sha256 = elemAt release."wheels"."${fn}" 0;
pyver = elemAt release."wheels"."${fn}" 1;
url = "https://files.pythonhosted.org/packages/" + pyver + "/" + pkg_name_first_char + "/"
+ pkg_name + "/" + fn;
in
pkgs.fetchurl {
inherit url sha256;
};
}