mnix: Nix binary-cache client PoC in MoonBit

Store path model + nixbase32, narinfo parser, ed25519 signature
verification, NAR reader/writer, closure computation, xz/zstd NAR fetch
with hash verification, atomic store unpack, profile management, Hydra-based
name resolution, and the mnix CLI. README documents usage, the /nix/store
limitation, and what a fuller implementation needs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-24 22:09:44 +00:00
1 parent 82c08d45b1
commit 7ed4bc8e51
9 files changed
+359 -12

No files matched your search

+101
View File
@@ -0,0 +1,101 @@
///|
// Produced by the reference tools: `xz -9e` and `zstd -19` over "mnix xz interop vector\n"
let xz_vector : Bytes = b"\xfd\x37\x7a\x58\x5a\x00\x00\x04\xe6\xd6\xb4\x46\x04\xc0\x1b\x17\x21\x01\x1c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x26\x02\x80\x23\x01\x00\x16\x6d\x6e\x69\x78\x20\x78\x7a\x20\x69\x6e\x74\x65\x72\x6f\x70\x20\x76\x65\x63\x74\x6f\x72\x0a\x00\x00\x52\x9c\xd4\x07\xd7\xbc\x89\xa2\x00\x01\x37\x17\xd8\x90\x52\x33\x1f\xb6\xf3\x7d\x01\x00\x00\x00\x00\x04\x59\x5a"
///|
let zstd_vector : Bytes = b"\x28\xb5\x2f\xfd\x24\x17\xb9\x00\x00\x6d\x6e\x69\x78\x20\x78\x7a\x20\x69\x6e\x74\x65\x72\x6f\x70\x20\x76\x65\x63\x74\x6f\x72\x0a\x14\xec\xed\x50"
///|
let plain : Bytes = b"mnix xz interop vector\n"
///|
test "decompress reference xz output" {
assert_eq(decompress("xz", xz_vector), plain)
}
///|
test "decompress reference zstd output" {
assert_eq(decompress("zstd", zstd_vector), plain)
}
///|
test "decompress none passes through; unknown fails loudly" {
assert_eq(decompress("none", plain), plain)
assert_true(@store.raises(() => decompress("bzip2", plain)))
assert_true(@store.raises(() => decompress("xz", plain))) // not xz data
}
///|
fn info_for(raw : Bytes, compression : String) -> @narinfo.NarInfo {
{
store_path: "/nix/store/8padfyi6z9ipfq35j86krpvpvx3j3r3v-demo-1",
url: "nar/x.nar.\{compression}",
compression,
file_hash: @store.sha256_nix32(raw),
file_size: raw.length().to_int64(),
nar_hash: @store.sha256_nix32(plain),
nar_size: plain.length().to_int64(),
references: [],
deriver: None,
sigs: [],
ca: None,
}
}
///|
test "verify_nar_download accepts consistent download" {
assert_eq(verify_nar_download(info_for(xz_vector, "xz"), xz_vector), plain)
assert_eq(
verify_nar_download(info_for(zstd_vector, "zstd"), zstd_vector),
plain,
)
}
///|
test "verify_nar_download rejects tampering" {
let ni = info_for(zstd_vector, "zstd")
// corrupted download bytes -> FileHash mismatch
let bad = Bytes::from_array(
FixedArray::makei(zstd_vector.length(), fn(i) {
if i == zstd_vector.length() - 1 {
(zstd_vector[i].to_int() ^ 1).to_byte()
} else {
zstd_vector[i]
}
}),
)
assert_true(@store.raises(() => verify_nar_download(ni, bad)))
// wrong NarHash
assert_true(
@store.raises(() => {
verify_nar_download(
{ ..ni, nar_hash: @store.sha256_nix32(b"other"), },
zstd_vector,
)
}),
)
// wrong NarSize
assert_true(
@store.raises(() => {
verify_nar_download({ ..ni, nar_size: 3L, }, zstd_vector)
}),
)
// wrong FileSize
assert_true(
@store.raises(() => {
verify_nar_download({ ..ni, file_size: 3L, }, zstd_vector)
}),
)
}
///|
test "redirect location resolution" {
inspect(
resolve_location("https://a.org/x/y", "https://b.org/z"),
content="https://b.org/z",
)
inspect(
resolve_location("https://a.org/x/y", "/z"),
content="https://a.org/z",
)
}