This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / tools / lazy-store / package.nix
5.4 kB 133 lines
1{ 2 lib, 3 rustPlatform, 4 glibc, 5 runCommand, 6 nix, 7 zstd, 8 xz, 9 jq, 10}: 11rustPlatform.buildRustPackage (finalAttrs: { 12 pname = "lazy-store"; 13 version = "0.1.0"; 14 src = lib.fileset.toSource { 15 root = ./.; 16 fileset = lib.fileset.unions [ 17 ./Cargo.toml 18 ./Cargo.lock 19 ./src 20 ]; 21 }; 22 cargoLock = { 23 lockFile = ./Cargo.lock; 24 outputHashes."fuse-backend-rs-0.14.0" = "sha256-pSnYq8IhfpxtnTS7bnE8GHAVP+yZ+hItChTEmGUP7xM="; 25 outputHashes."harmonia-file-core-3.3.0" = "sha256-aPNxxJLjRd+wVFyAcUnzrakC0BNj/N9rJWV2y8wpCAs="; 26 }; 27 # Static, so that once it serves /nix/store it loads nothing from there. 28 # (Not glibc.static in buildInputs: build scripts would link against it.) 29 env.RUSTFLAGS = "-C target-feature=+crt-static -L native=${glibc.static}/lib"; 30 31 doInstallCheck = true; 32 installCheckPhase = '' 33 if $READELF -l $out/bin/lazy-store | grep -q INTERP; then 34 echo "lazy-store is not static"; exit 1 35 fi 36 ($out/bin/lazy-store || true) 2>&1 | grep -q usage 37 ''; 38 39 # closure and the lower layer against a file:// cache, without FUSE: 40 # nix build .#lazy-store.tests.fetch 41 passthru.tests.fetch = 42 runCommand "lazy-store-fetch-test" 43 { 44 nativeBuildInputs = [ 45 finalAttrs.finalPackage 46 nix 47 zstd 48 xz 49 jq 50 ]; 51 } 52 '' 53 export HOME=$TMPDIR NIX_STATE_DIR=$TMPDIR/state NIX_LOG_DIR=$TMPDIR/log 54 mkdir -p src/a/sub cache/nar out closure 55 echo hello > src/a/file 56 printf '#!/bin/sh\n' > src/a/sub/run 57 chmod +x src/a/sub/run 58 ln -s file src/a/link 59 echo single > src/b 60 cp -a src/a src/c 61 62 A=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-a 63 B=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-b 64 C=cccccccccccccccccccccccccccccccc-c 65 D=dddddddddddddddddddddddddddddddd-d 66 # store path hashes are nix32, which has no e 67 E=ffffffffffffffffffffffffffffffff-f 68 ls='{"version":1,"root":{"type":"directory","entries":{ 69 "file":{"type":"regular","size":6}, 70 "link":{"type":"symlink","target":"file"}, 71 "sub":{"type":"directory","entries":{"run":{"type":"regular","size":10,"executable":true}}}}}}' 72 # zstd .ls served without Content-Encoding (sniffed); b has none 73 echo "$ls" | zstd -q > cache/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ls 74 echo "$ls" | zstd -q > cache/cccccccccccccccccccccccccccccccc.ls 75 76 nix-store --dump src/a | zstd -q > cache/nar/a.nar.zst 77 nix-store --dump src/b | xz > cache/nar/b.nar.xz 78 nix-store --dump src/c | zstd -q > cache/nar/c.nar.zst 79 info() { # name file compression hash size 80 jq -n --arg c "file://$PWD/cache" --arg u "$2" --arg z "$3" --arg h "$4" --argjson s "$5" \ 81 '{($ARGS.positional[0]): {cache: $c, url: $u, compression: $z, narHash: $h, narSize: $s, fileSize: 0}}' \ 82 --args "$1" 83 } 84 size() { nix-store --dump "$1" | wc -c; } 85 { 86 info $A nar/a.nar.zst zstd "sha256:$(nix-hash --type sha256 --base32 src/a)" "$(size src/a)" 87 info $B nar/b.nar.xz xz "sha256:$(nix-hash --type sha256 src/b)" "$(size src/b)" 88 # wrong hash: must not be unpacked 89 info $C nar/c.nar.zst zstd "sha256:$(nix-hash --type sha256 --base32 src/b)" "$(size src/c)" 90 } | jq -s add > out/paths.json 91 92 lazy-store fetch out $A $B 93 diff -r src/a out/cache/$A 94 diff src/b out/cache/$B 95 test "$(stat -c %a out/cache/$A/sub/run)" = 555 96 test "$(stat -c %a out/cache/$A/file)" = 444 97 test "$(readlink out/cache/$A/link)" = file 98 jq -e '.ls_count == 2 and .nar_count == 2' out/stats.json 99 if lazy-store fetch out $C; then 100 echo "NAR with a wrong hash was accepted"; exit 1 101 fi 102 test ! -e out/cache/$C 103 104 # a refers to b; d refers to e, which no cache has: d is left out 105 narinfo() { # name url references 106 printf 'StorePath: /nix/store/%s\nURL: %s\nCompression: zstd\nNarHash: sha256:%s\nNarSize: 8\nReferences: %s\nSig: test-1:%s\n' \ 107 "$1" "$2" "$(nix-hash --type sha256 --base32 src/b)" "$3" "$(printf %64s | base64 -w0)" \ 108 > "cache/''${1%%-*}.narinfo" 109 } 110 narinfo $A nar/a.nar.zst "$A $B" 111 narinfo $B nar/b.nar.xz "" 112 narinfo $D nar/d.nar.zst "$E" 113 printf '/nix/store/%s\n' $A $D | LAZY_STORE_CACHES="file://$PWD/cache" lazy-store closure closure 114 jq -e '.paths == 2 and .missing == ["'$E'"] and .incomplete == 1' closure/closure.json 115 jq -e 'keys == ["'$A'", "'$B'"]' closure/paths.json 116 if grep -q $D closure/db.dump; then 117 echo "path with an incomplete closure was registered"; exit 1 118 fi 119 120 # registered as .github/lazy-store.sh does, with the signatures 121 export NIX_CONFIG="experimental-features = nix-command" 122 root="local?root=$PWD/root" 123 nix-store --store "$root" --load-db < closure/db.dump 124 printf '/nix/store/%s\n' $A $B | 125 nix store copy-sigs --store "$root" -s "file://$PWD/closure/narinfo" --stdin 126 nix path-info --store "$root" --sigs /nix/store/$A | grep -q "[[:space:]]test-1:$(printf %64s | base64 -w0)$" 127 test "$(nix-store --store "$root" -q --references /nix/store/$A | wc -l)" = 2 128 touch $out 129 ''; 130 131 meta.mainProgram = "lazy-store"; 132 meta.platforms = lib.platforms.linux; 133})