WIP: store, narinfo, sign, nar, cache, closure packages

This commit is contained in:
Sprite committed 2026-11-24 21:49:20 +00:00
1 parent 4e7ce11973
commit 988c942952
28 files changed
+1432

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
import {
"experiments/moon-nix/store",
"moonbitlang/core/string",
}
+99
View File
@@ -0,0 +1,99 @@
///|
/// A parsed `.narinfo` file as served by a Nix binary cache.
#derive(Eq, Debug)
pub(all) struct NarInfo {
store_path : String // full path, e.g. /nix/store/<hash>-hello-2.8.4
url : String // relative to the cache, e.g. nar/<h>.nar.xz
compression : String // none | xz | zstd | bzip2 ...
file_hash : String // sha256:<nixbase32> of the compressed file
file_size : Int64
nar_hash : String // sha256:<nixbase32> of the uncompressed NAR
nar_size : Int64
references : Array[String] // base names (<hash>-<name>), may include self
deriver : String? // base name of the .drv
sigs : Array[String] // name:base64
ca : String?
}
///|
fn parse_i64(key : String, v : String) -> Int64 raise @store.NixError {
@string.parse_int64(v) catch {
_ => raise Parse("narinfo: bad integer for \{key}: '\{v}'")
}
}
///|
/// Parse the text of a narinfo file. Required keys: StorePath, URL,
/// NarHash, NarSize, References. `Compression` defaults to "bzip2" when
/// absent (per the Nix spec); unsupported values fail at decompression time.
/// Duplicate keys (except `Sig`, which may repeat) and malformed lines are
/// errors.
pub fn NarInfo::parse(text : String) -> NarInfo raise @store.NixError {
let seen : Map[String, String] = Map([])
let sigs : Array[String] = []
for line in text.split("\n") {
let line = line.trim_end(chars="\r").to_owned()
if line.length() == 0 {
continue
}
guard line.find(": ") is Some(i) else {
raise Parse("narinfo: malformed line (no ': '): \{line}")
}
let key = line[0:i].to_owned()
let value = line[i + 2:].to_owned()
if key == "Sig" {
sigs.push(value)
continue
}
if seen.contains(key) {
raise Parse("narinfo: duplicate key \{key}")
}
seen[key] = value
}
fn req(k : String) -> String raise @store.NixError {
match seen.get(k) {
Some(v) => v
None => raise Parse("narinfo: missing required key \{k}")
}
}
let store_path = req("StorePath")
// Validate the path shape loudly.
ignore(@store.StorePath::parse(store_path))
let file_hash = match seen.get("FileHash") {
Some(v) => v
None => ""
}
let file_size = match seen.get("FileSize") {
Some(v) => parse_i64("FileSize", v)
None => 0L
}
let references = match seen.get("References") {
Some(v) =>
v
.split(" ")
.filter(fn(s) { s.length() > 0 })
.map(fn(s) { s.to_owned() })
.collect()
None => raise Parse("narinfo: missing required key References")
}
for r in references {
ignore(@store.StorePath::parse_base(r))
}
{
store_path,
url: req("URL"),
compression: match seen.get("Compression") {
Some(c) => c
None => "bzip2"
},
file_hash,
file_size,
nar_hash: req("NarHash"),
nar_size: parse_i64("NarSize", req("NarSize")),
references,
deriver: seen.get("Deriver"),
sigs,
ca: seen.get("CA"),
}
}
+38
View File
@@ -0,0 +1,38 @@
///|
let hello_narinfo : String =
#|StorePath: /nix/store/8padfyi6z9ipfq35j86krpvpvx3j3r3v-hello-2.8.4
#|URL: nar/0rn6mlcldbs3x4jwd7j8x2j1xf3v8ydn08icm4by312cw7db8z33.nar.zst
#|Compression: zstd
#|FileHash: sha256:0rvh1h413bl7rl9hbifzvjwjyzfbqy4q969xpg42ns43fajggazs
#|FileSize: 10561
#|NarHash: sha256:0rn6mlcldbs3x4jwd7j8x2j1xf3v8ydn08icm4by312cw7db8z33
#|NarSize: 101224
#|References: 1q857rs68rs3gijls9wdnsjqnwnv5zd8-glibc-2.44-57 w79cl5jdx1hggkmy4miwhb1j9w1pb7qg-gcc-16.2.0-lib
#|Deriver: fwcmbj1s9g7w3ss4nn774dvvq4was180-hello-2.8.4.drv
#|Sig: cache.nixos.org-1:LPtBVTcqdkiUD6vGPmFjn0LEIkg0N5cg7xwbIPa9z9g81p9gUxGJQElBPNDhI6MD0Jt3AyCK5rb1Buy609kXCQ==
#|
///|
test "parse real hello narinfo" {
let ni = NarInfo::parse(hello_narinfo)
inspect(
ni.store_path,
content="/nix/store/8padfyi6z9ipfq35j86krpvpvx3j3r3v-hello-2.8.4",
)
inspect(ni.compression, content="zstd")
inspect(ni.file_size, content="10561")
inspect(ni.nar_size, content="101224")
assert_eq(ni.references.length(), 2)
assert_eq(ni.sigs.length(), 1)
assert_eq(
ni.deriver,
Some("fwcmbj1s9g7w3ss4nn774dvvq4was180-hello-2.8.4.drv"),
)
}
///|
test "reject malformed narinfo" {
assert_true(@store.raises(() => NarInfo::parse("StorePath /nix/store/x")))
assert_true(@store.raises(() => NarInfo::parse("URL: a\nURL: b\n")))
assert_true(@store.raises(() => NarInfo::parse("URL: a\n")))
}
+32
View File
@@ -0,0 +1,32 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/moon-nix/narinfo"
import {
"experiments/moon-nix/store",
"moonbitlang/core/debug",
}
// Values
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) struct NarInfo {
store_path : String
url : String
compression : String
file_hash : String
file_size : Int64
nar_hash : String
nar_size : Int64
references : Array[String]
deriver : String?
sigs : Array[String]
ca : String?
}
pub fn NarInfo::parse(String) -> Self raise @store.NixError
// Type aliases
// Traits