1 from functools
import partial
3 from multiprocessing
import Pool
5 from pathlib
import Path
9 def dropPrefix(path
, nixPrefix
):
10 return path
[len(nixPrefix
+ "/") :]
13 def processItem(item
, nixPrefix
, outDir
):
14 narInfoHash
= dropPrefix(item
["path"], nixPrefix
).split("-")[0]
16 xzFile
= outDir
/ "nar" / f
"{narInfoHash}.nar.xz"
17 with
open(xzFile
, "wb") as f
:
19 f
"nix-store --dump {item['path']} | xz -c",
27 ["nix-hash", "--base32", "--type", "sha256", "--flat", xzFile
],
34 fileSize
= os
.path
.getsize(xzFile
)
36 finalXzFileName
= Path("nar") / f
"{fileHash}.nar.xz"
37 os
.rename(xzFile
, outDir
/ finalXzFileName
)
39 with
open(outDir
/ f
"{narInfoHash}.narinfo", "wt") as f
:
40 f
.write(f
"StorePath: {item['path']}\n")
41 f
.write(f
"URL: {finalXzFileName}\n")
42 f
.write("Compression: xz\n")
43 f
.write(f
"FileHash: sha256:{fileHash}\n")
44 f
.write(f
"FileSize: {fileSize}\n")
45 f
.write(f
"NarHash: {item['narHash']}\n")
46 f
.write(f
"NarSize: {item['narSize']}\n")
47 f
.write(f
"References: {' '.join(dropPrefix(ref, nixPrefix) for ref in item['references'])}\n")
51 outDir
= Path(os
.environ
["out"])
52 nixPrefix
= os
.environ
["NIX_STORE"]
53 numWorkers
= int(os
.environ
.get("NIX_BUILD_CORES", "4"))
55 with
open(os
.environ
["NIX_ATTRS_JSON_FILE"], "r") as f
:
56 closures
= json
.load(f
)["closure"]
58 os
.makedirs(outDir
/ "nar", exist_ok
=True)
60 with
open(outDir
/ "nix-cache-info", "w") as f
:
61 f
.write(f
"StoreDir: {nixPrefix}\n")
63 with
Pool(processes
=numWorkers
) as pool
:
64 worker
= partial(processItem
, nixPrefix
=nixPrefix
, outDir
=outDir
)
65 pool
.map(worker
, closures
)
68 if __name__
== "__main__":