diff options
author | Tom McLaughlin <tom@codedown.io> | 2022-10-03 22:06:28 -0600 |
---|---|---|
committer | Tom McLaughlin <tom@codedown.io> | 2023-02-07 16:16:07 -0800 |
commit | d1a2a16a3a0220e8f571c07001a2ada4a664c492 (patch) | |
tree | e832701e737104cd36eba2fe6b3a839882c1e411 /pkgs/build-support/binary-cache | |
parent | c5ce8986dff8e06ace05a0158fe617707228ac25 (diff) | |
download | nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar.gz nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar.bz2 nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar.lz nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar.xz nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.tar.zst nixpkgs-d1a2a16a3a0220e8f571c07001a2ada4a664c492.zip |
Introduce mkBinaryCache function
Diffstat (limited to 'pkgs/build-support/binary-cache')
-rw-r--r-- | pkgs/build-support/binary-cache/default.nix | 40 | ||||
-rw-r--r-- | pkgs/build-support/binary-cache/make-binary-cache.py | 43 |
2 files changed, 83 insertions, 0 deletions
diff --git a/pkgs/build-support/binary-cache/default.nix b/pkgs/build-support/binary-cache/default.nix new file mode 100644 index 00000000000..577328cad92 --- /dev/null +++ b/pkgs/build-support/binary-cache/default.nix @@ -0,0 +1,40 @@ +{ stdenv, buildPackages }: + +# This function is for creating a flat-file binary cache, i.e. the kind created by +# nix copy --to file:///some/path and usable as a substituter (with the file:// prefix). + +# For example, in the Nixpkgs repo: +# nix-build -E 'with import ./. {}; mkBinaryCache { rootPaths = [hello]; }' + +{ name ? "binary-cache" +, rootPaths +}: + +stdenv.mkDerivation { + inherit name; + + __structuredAttrs = true; + + exportReferencesGraph.closure = rootPaths; + + preferLocalBuild = true; + + PATH = "${buildPackages.coreutils}/bin:${buildPackages.jq}/bin:${buildPackages.python3}/bin:${buildPackages.nix}/bin:${buildPackages.xz}/bin"; + + builder = builtins.toFile "builder" '' + . .attrs.sh + + export out=''${outputs[out]} + + mkdir $out + mkdir $out/nar + + python ${./make-binary-cache.py} + + # These directories must exist, or Nix might try to create them in LocalBinaryCacheStore::init(), + # which fails if mounted read-only + mkdir $out/realisations + mkdir $out/debuginfo + mkdir $out/log + ''; +} diff --git a/pkgs/build-support/binary-cache/make-binary-cache.py b/pkgs/build-support/binary-cache/make-binary-cache.py new file mode 100644 index 00000000000..16dd8a7e96b --- /dev/null +++ b/pkgs/build-support/binary-cache/make-binary-cache.py @@ -0,0 +1,43 @@ + +import json +import os +import subprocess + +with open(".attrs.json", "r") as f: + closures = json.load(f)["closure"] + +os.chdir(os.environ["out"]) + +nixPrefix = os.environ["NIX_STORE"] # Usually /nix/store + +with open("nix-cache-info", "w") as f: + f.write("StoreDir: " + nixPrefix + "\n") + +def dropPrefix(path): + return path[len(nixPrefix + "/"):] + +for item in closures: + narInfoHash = dropPrefix(item["path"]).split("-")[0] + + xzFile = "nar/" + narInfoHash + ".nar.xz" + with open(xzFile, "w") as f: + subprocess.run("nix-store --dump %s | xz -c" % item["path"], stdout=f, shell=True) + + fileHash = subprocess.run(["nix-hash", "--base32", "--type", "sha256", item["path"]], capture_output=True).stdout.decode().strip() + fileSize = os.path.getsize(xzFile) + + # Rename the .nar.xz file to its own hash to match "nix copy" behavior + finalXzFile = "nar/" + fileHash + ".nar.xz" + os.rename(xzFile, finalXzFile) + + with open(narInfoHash + ".narinfo", "w") as f: + f.writelines((x + "\n" for x in [ + "StorePath: " + item["path"], + "URL: " + finalXzFile, + "Compression: xz", + "FileHash: sha256:" + fileHash, + "FileSize: " + str(fileSize), + "NarHash: " + item["narHash"], + "NarSize: " + str(item["narSize"]), + "References: " + " ".join(dropPrefix(ref) for ref in item["references"]), + ])) |