on main help sign in
Add Nix desk and glob builds
commit351b0ff7068917e7e47f19a56d1c8970bc7a6a69 authoriko <mail@iko.soy> dateSun 1 Nov 2026, 16:17 (3 days ago) parent7f41c41
3 files changed+308
bin/make_glob.py+208 flake.lock+27 flake.nix+73
bin/make_glob.pynew file+208
@@ -0,0 +1,208 @@
1+#!/usr/bin/env python3
2+
3+import hashlib
4+import sys
5+from pathlib import Path
6+from urllib.parse import quote
7+
8+from noun import Cell, deep, intbytes, mug
9+
10+MIME_TYPES = {
11+ "css": "text/css",
12+ "gif": "image/gif",
13+ "html": "text/html",
14+ "ico": "image/x-icon",
15+ "jpeg": "image/jpeg",
16+ "jpg": "image/jpeg",
17+ "js": "application/javascript",
18+ "json": "application/json",
19+ "png": "image/png",
20+ "svg": "image/svg+xml",
21+ "ttf": "font/ttf",
22+ "webmanifest": "application/manifest+json",
23+ "woff": "font/woff",
24+ "woff2": "font/woff2",
25+}
26+BASE32 = "0123456789abcdefghijklmnopqrstuv"
27+
28+
29+class BitWriter:
30+ def __init__(self):
31+ self.output = bytearray()
32+ self.buffer = 0
33+ self.used = 0
34+ self.cursor = 0
35+
36+ def write(self, value, count):
37+ if count == 0:
38+ return
39+ self.buffer |= value << self.used
40+ self.used += count
41+ self.cursor += count
42+ byte_count = self.used // 8
43+ if byte_count:
44+ self.output.extend(self.buffer.to_bytes((self.used + 7) // 8, "little")[:byte_count])
45+ self.buffer >>= byte_count * 8
46+ self.used -= byte_count * 8
47+
48+ def finish(self):
49+ if self.used:
50+ self.output.append(self.buffer)
51+ self.buffer = 0
52+ self.used = 0
53+ return bytes(self.output)
54+
55+
56+def jam_bytes(value):
57+ writer = BitWriter()
58+ references = {}
59+
60+ def save(noun):
61+ references[noun] = writer.cursor
62+
63+ def mat(atom):
64+ if atom == 0:
65+ writer.write(1, 1)
66+ return
67+ atom_length = atom.bit_length()
68+ length_size = atom_length.bit_length()
69+ writer.write(1 << length_size, length_size + 1)
70+ writer.write(atom_length & ((1 << (length_size - 1)) - 1), length_size - 1)
71+ writer.write(atom, atom_length)
72+
73+ def back(reference):
74+ writer.write(3, 2)
75+ mat(reference)
76+
77+ def serialize(noun):
78+ duplicate = references.get(noun)
79+ if deep(noun):
80+ if duplicate is not None:
81+ back(duplicate)
82+ return
83+ save(noun)
84+ writer.write(1, 1)
85+ writer.write(0, 1)
86+ serialize(noun.head)
87+ serialize(noun.tail)
88+ return
89+ if duplicate is not None:
90+ atom_size = noun.bit_length()
91+ reference_size = duplicate.bit_length()
92+ if atom_size >= reference_size:
93+ back(duplicate)
94+ return
95+ save(noun)
96+ writer.write(0, 1)
97+ mat(noun)
98+
99+ serialize(value)
100+ return writer.finish()
101+
102+
103+def cord(value):
104+ return int.from_bytes(value.encode(), "little")
105+
106+
107+def path_noun(parts):
108+ result = 0
109+ for part in reversed(parts):
110+ result = Cell(cord(part), result)
111+ return result
112+
113+
114+def dor(a, b):
115+ if a == b:
116+ return True
117+ if deep(a):
118+ if not deep(b):
119+ return False
120+ if a.head == b.head:
121+ return dor(a.tail, b.tail)
122+ return dor(a.head, b.head)
123+ if deep(b):
124+ return True
125+ return a < b
126+
127+
128+def gor(a, b):
129+ a_mug = mug(a)
130+ b_mug = mug(b)
131+ return dor(a, b) if a_mug == b_mug else a_mug < b_mug
132+
133+
134+def map_put(tree, entry):
135+ if tree == 0:
136+ return Cell(entry, Cell(0, 0))
137+ node = tree.head
138+ if entry.head == node.head:
139+ return Cell(entry, tree.tail)
140+ left = tree.tail.head
141+ right = tree.tail.tail
142+ if gor(entry.head, node.head):
143+ return Cell(node, Cell(map_put(left, entry), right))
144+ return Cell(node, Cell(left, map_put(right, entry)))
145+
146+
147+def shas(salt, serialized):
148+ value_hash = int.from_bytes(hashlib.sha256(serialized).digest(), "little")
149+ length = max(32, len(intbytes(salt)))
150+ digest = hashlib.sha256((salt ^ value_hash).to_bytes(length, "little")).digest()
151+ return int.from_bytes(digest, "little")
152+
153+
154+def sham(serialized):
155+ hashed = shas(cord("sham"), serialized)
156+ mask = (1 << 128) - 1
157+ return (hashed & mask) ^ (hashed >> 128)
158+
159+
160+def scow_uv(value):
161+ if value == 0:
162+ return "0v0"
163+ digits = []
164+ while value:
165+ value, digit = divmod(value, 32)
166+ digits.append(BASE32[digit])
167+ digits = "".join(reversed(digits))
168+ first_length = (len(digits) - 1) % 5 + 1
169+ groups = [digits[:first_length]]
170+ groups.extend(digits[i:i + 5] for i in range(first_length, len(digits), 5))
171+ return "0v" + ".".join(groups)
172+
173+
174+def file_entry(root, file_path):
175+ relative = file_path.relative_to(root)
176+ parts = []
177+ for part in relative.parts:
178+ parts.extend(quote(segment, safe="-_~") for segment in part.split("."))
179+ extension = relative.suffix.removeprefix(".").lower()
180+ if extension not in MIME_TYPES:
181+ raise ValueError(f"no MIME type configured for {relative}")
182+ data = file_path.read_bytes()
183+ mime = Cell(path_noun(MIME_TYPES[extension].split("/")), Cell(len(data), int.from_bytes(data, "little")))
184+ return Cell(path_noun(parts), mime)
185+
186+
187+def make_glob(source, destination):
188+ root = Path(source)
189+ files = sorted((path for path in root.rglob("*") if path.is_file()), key=lambda path: path.as_posix())
190+ if not (root / "index.html").is_file():
191+ raise ValueError("glob source must contain index.html")
192+ glob = 0
193+ for file_path in files:
194+ glob = map_put(glob, file_entry(root, file_path))
195+ serialized = jam_bytes(glob)
196+ glob_hash = scow_uv(sham(serialized))
197+ output = Path(destination)
198+ output.mkdir(parents=True, exist_ok=True)
199+ filename = f"glob-{glob_hash}.glob"
200+ (output / filename).write_bytes(serialized)
201+ (output / "hash").write_text(glob_hash + "\n")
202+ print(filename)
203+
204+
205+if __name__ == "__main__":
206+ if len(sys.argv) != 3:
207+ raise SystemExit(f"usage: {sys.argv[0]} SOURCE_DIRECTORY OUTPUT_DIRECTORY")
208+ make_glob(sys.argv[1], sys.argv[2])
flake.locknew file+27
@@ -0,0 +1,27 @@
1+{
2+ "nodes": {
3+ "nixpkgs": {
4+ "locked": {
5+ "lastModified": 1793341285,
6+ "narHash": "sha256-Y1CiZ4SoBUjLQyQM/pY+rS2dXD3CHLPstF0lmOc73D4=",
7+ "owner": "NixOS",
8+ "repo": "nixpkgs",
9+ "rev": "1c89083e451e891152365b799595cf7790a43325",
10+ "type": "github"
11+ },
12+ "original": {
13+ "owner": "NixOS",
14+ "ref": "nixos-unstable",
15+ "repo": "nixpkgs",
16+ "type": "github"
17+ }
18+ },
19+ "root": {
20+ "inputs": {
21+ "nixpkgs": "nixpkgs"
22+ }
23+ }
24+ },
25+ "root": "root",
26+ "version": 7
27+}
flake.nixnew file+73
@@ -0,0 +1,73 @@
1+{
2+ description = "Astrolabe Urbit desk";
3+
4+ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
5+
6+ outputs = { self, nixpkgs }:
7+ let
8+ systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
9+ forAllSystems = nixpkgs.lib.genAttrs systems;
10+ in
11+ {
12+ packages = forAllSystems (system:
13+ let
14+ pkgs = import nixpkgs { inherit system; };
15+ nounPy = pkgs.fetchurl {
16+ name = "noun.py";
17+ url = "https://raw.githubusercontent.com/urbit/tools/f9cda82a7699cad0c1c0f8c031a59e93f028e548/pkg/pynoun/noun.py";
18+ hash = "sha256-vSKQ/UuSmGTANfW5c3I9lQcUFycvqmu5gqysFuP5SAA=";
19+ };
20+ ui = pkgs.buildNpmPackage {
21+ pname = "astrolabe-ui";
22+ version = "0.3.0";
23+ src = ./ui;
24+ nodejs = pkgs.nodejs_22;
25+ npmDepsHash = "sha256-jCkfgNgBmq0mKjmXBbPwdpzBR/E09OgS9XD6O9hRHfw=";
26+ makeCacheWritable = true;
27+ npmFlags = [ "--legacy-peer-deps" ];
28+ npmBuildScript = "build";
29+
30+ installPhase = ''
31+ runHook preInstall
32+ cp -r dist $out
33+ runHook postInstall
34+ '';
35+ };
36+ in
37+ rec {
38+ inherit ui;
39+ desk = pkgs.stdenvNoCC.mkDerivation {
40+ pname = "astrolabe-desk";
41+ version = "1.0.7";
42+ dontUnpack = true;
43+ nativeBuildInputs = [ pkgs.rsync ];
44+
45+ installPhase = ''
46+ runHook preInstall
47+ mkdir -p $out
48+ rsync -r --copy-links --exclude-from=${./config/ignore_files_on_install.txt} ${./base-desk}/ $out/
49+ rsync -r --copy-links --exclude-from=${./config/ignore_files_on_install.txt} ${./docs-dev-desk}/ $out/
50+ rsync -r --copy-links --exclude-from=${./config/ignore_files_on_install.txt} ${./desk}/ $out/
51+ runHook postInstall
52+ '';
53+ };
54+ glob = pkgs.stdenvNoCC.mkDerivation {
55+ pname = "astrolabe-glob";
56+ version = "0.3.0";
57+ dontUnpack = true;
58+ nativeBuildInputs = [
59+ (pkgs.python3.withPackages (ps: [ ps.mmh3 ps.bitstring ]))
60+ ];
61+
62+ installPhase = ''
63+ runHook preInstall
64+ mkdir -p $TMPDIR/pynoun
65+ ln -s ${nounPy} $TMPDIR/pynoun/noun.py
66+ PYTHONPATH=$TMPDIR/pynoun python ${./bin/make_glob.py} ${ui} $out
67+ runHook postInstall
68+ '';
69+ };
70+ default = desk;
71+ });
72+ };
73+}