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