1
2import json
3import os
4import subprocess
5
6with open(".attrs.json", "r") as f:
7 closures = json.load(f)["closure"]
8
9os.chdir(os.environ["out"])
10
11nixPrefix = os.environ["NIX_STORE"] # Usually /nix/store
12
13with open("nix-cache-info", "w") as f:
14 f.write("StoreDir: " + nixPrefix + "\n")
15
16def dropPrefix(path):
17 return path[len(nixPrefix + "/"):]
18
19for item in closures:
20 narInfoHash = dropPrefix(item["path"]).split("-")[0]
21
22 xzFile = "nar/" + narInfoHash + ".nar.xz"
23 with open(xzFile, "w") as f:
24 subprocess.run("nix-store --dump %s | xz -c" % item["path"], stdout=f, shell=True)
25
26 fileHash = subprocess.run(["nix-hash", "--base32", "--type", "sha256", item["path"]], capture_output=True).stdout.decode().strip()
27 fileSize = os.path.getsize(xzFile)
28
29 # Rename the .nar.xz file to its own hash to match "nix copy" behavior
30 finalXzFile = "nar/" + fileHash + ".nar.xz"
31 os.rename(xzFile, finalXzFile)
32
33 with open(narInfoHash + ".narinfo", "w") as f:
34 f.writelines((x + "\n" for x in [
35 "StorePath: " + item["path"],
36 "URL: " + finalXzFile,
37 "Compression: xz",
38 "FileHash: sha256:" + fileHash,
39 "FileSize: " + str(fileSize),
40 "NarHash: " + item["narHash"],
41 "NarSize: " + str(item["narSize"]),
42 "References: " + " ".join(dropPrefix(ref) for ref in item["references"]),
43 ]))