Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
{ description = "A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.";
inputs = { nixpkgs = { url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst"; }; zon2nix = { url = "github:jcollie/zon2nix"; inputs = { nixpkgs.follows = "nixpkgs"; }; };
};
outputs = { self, nixpkgs, zon2nix, ... }: let inherit (nixpkgs) lib; linuxSystems = builtins.filter ( system: (lib.systems.elaborate system).isLinux ) lib.systems.flakeExposed; makePackages = system: import nixpkgs { inherit system; # The browser check needs Chromium, which is unfree. config.allowUnfree = true; }; forAllSystems = lib.genAttrs linuxSystems;
# Zig 0.16.0 cannot build a test executable in fuzz mode: on the # `-ffuzz` path its test runner hands what `@errorReturnTrace()` # returned -- a `builtin.StackTrace` -- to `std.debug.writeStackTrace`, # which wants a `debug.StackTrace`. The function next door takes # exactly the type in hand. Only one file of the 217 MB library # changes, so make a farm of symlinks with that file replaced rather # than rebuilding the compiler. # # `--replace-fail` is the safety: the day Zig ships the fix the # pattern is not found, the build fails here rather than silently # patching something else, and all of this can go. # # It buys the fuzzer and not its coverage: nothing in this release # populates the table of program counters, so `--fuzz=N` searches # blindly and then fails on the coverage report. `zig build fuzz-run` # is the loop that works, and `tools/fuzz.zig` says what it does # instead. fuzzZigLib = pkgs: pkgs.runCommand "zig-0.16.0-lib-fuzz-fix" { } '' cp -rs --no-preserve=mode ${pkgs.zig_0_16}/lib/zig $out chmod -R u+w $out rm $out/compiler/test_runner.zig cp --no-preserve=mode ${pkgs.zig_0_16}/lib/zig/compiler/test_runner.zig $out/compiler/ substituteInPlace $out/compiler/test_runner.zig \ --replace-fail 'std.debug.writeStackTrace(trace, stderr)' \ 'std.debug.writeErrorReturnTrace(trace, stderr)' '';
# The patch lives on the `-ffuzz` path alone, so pointing every # invocation at the patched library costs an ordinary build nothing. zigFor = pkgs: pkgs.runCommand "zig-0.16.0-fuzzable" { nativeBuildInputs = [ pkgs.makeWrapper ]; } '' mkdir -p $out/bin makeWrapper ${pkgs.zig_0_16}/bin/zig $out/bin/zig \ --set ZIG_LIB_DIR ${fuzzZigLib pkgs} '';
# What `zig build --system` wants: one directory per dependency, named # by the hash `build.zig.zon` records for it. Given this, the build # fetches nothing, which is what lets it run in the Nix sandbox -- # `--system` does not merely offer the directory, it forbids fetching, # so a dependency missing from here is a build error naming the package # rather than a network attempt that fails somewhere less legible. # # Generated from `build.zig.zon` by zon2nix, and regenerated rather than # edited whenever a dependency is added, removed or moved: # # nix develop -c zon2nix --16 --nix=build.zig.zon.nix build.zig.zon # # It has to list the lazy dependencies too. `-Dunicode=true` makes # uucode not lazy at all in practice, and a lazy dependency that is # reached for under `--system` and is not there fails the same way an # eager one does. zigPackagesFor = pkgs: pkgs.callPackage ./build.zig.zon.nix { name = "zig-uri-packages"; };
# The whole of what has to be served to use the library from a page: # `uri.wasm`, the `uri.js` that drives it, the demo, and the in-browser # test runner. Built with Unicode support, since IDNA and IRI display # are much of the reason to want this in a browser at all. webFor = pkgs: pkgs.stdenv.mkDerivation { pname = "zig-uri-web"; version = "0.1.0"; src = self;
nativeBuildInputs = [ (zigFor pkgs) ]; dontConfigure = true; dontInstall = true;
buildPhase = '' runHook preBuild export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-global" zig build wasm \ -Dunicode=true \ --system ${zigPackagesFor pkgs} \ --cache-dir "$TMPDIR/zig-cache" \ --prefix "$out" runHook postBuild '';
meta = { description = "zig-uri compiled to WebAssembly, with its JavaScript binding"; license = lib.licenses.mit; }; }; in { packages = forAllSystems ( system: let pkgs = makePackages system; in { default = webFor pkgs; web = webFor pkgs; } );
# `nix flake check` boots a virtual machine, serves the bundle inside it # and runs the binding's own test suite through a real browser engine. # A browser is the one thing node cannot stand in for: module # resolution, `WebAssembly.instantiateStreaming` and the MIME types a # server sends are all only exercised for real here. checks = forAllSystems ( system: let pkgs = makePackages system; web = webFor pkgs; in { browser = pkgs.testers.runNixOSTest { name = "zig-uri-browser";
nodes.machine = { # Chromium wants room; the default 1 GB is not enough for it to # get as far as running a page. virtualisation.memorySize = 3072; virtualisation.cores = 2;
environment.systemPackages = [ pkgs.chromium pkgs.curl ];
# nginx serves `.wasm` as `application/wasm` out of its own MIME # table, which is what `WebAssembly.instantiateStreaming` insists # on -- and the `uri.js` fallback for servers that get it wrong # is then never reached, so the check below asserts the header # rather than trusting it. services.nginx = { enable = true; virtualHosts."localhost" = { root = "${web}/web"; }; }; };
testScript = '' import re
machine.wait_for_unit("nginx.service") machine.wait_for_open_port(80)
def dump(url): """Load a page in headless Chromium and return its DOM once settled.""" return machine.succeed( "chromium" " --headless=new" " --no-sandbox" " --disable-gpu" " --disable-dev-shm-usage" " --user-data-dir=/tmp/chromium" " --virtual-time-budget=60000" f" --dump-dom {url}" )
with subtest("the module is served as application/wasm"): headers = machine.succeed("curl -sSI http://localhost/uri.wasm") assert "application/wasm" in headers.lower(), headers
with subtest("the binding's own suite passes in a browser"): dom = dump("http://localhost/browser-test.html") found = re.search(r"RESULT pass=(\d+) fail=(\d+) skip=(\d+)", dom) assert found, f"the runner never reported a result:\n{dom}" passed, failed, skipped = (int(n) for n in found.groups()) print(f"browser suite: {passed} passed, {failed} failed, {skipped} skipped") assert failed == 0, dom assert passed > 30, f"only {passed} tests ran; the suite did not finish" assert skipped == 1, f"{skipped} skipped: this bundle is built with Unicode"
with subtest("the demo page renders what the library computed"): dom = dump("http://localhost/") # `--dump-dom` serializes the page's own script along with # everything else, so every assertion here has to name # rendered output rather than any string the source contains. assert ( '<footer id="status">uri.wasm loaded \u2014 Unicode support is on.</footer>' in dom ), dom # Parsed into decoded segments... assert '<td class="value">["a",".","b","..","c/d"]</td>' in dom, dom # ...normalized and escaped again... assert "https://user:@example.com:443/a/c%2Fd?q=1&flag#top" in dom, dom # ...and a relative link resolved against it. assert "https://user:@Example.COM:443/sibling?x=2" in dom, dom assert 'class="out error"' not in dom, dom ''; }; } );
devShells = forAllSystems ( system: let pkgs = makePackages system; in { default = pkgs.mkShell { name = "zig-uri"; nativeBuildInputs = [ # `zig build test-web` drives the WebAssembly module the way a # browser would; that needs a JavaScript engine, and node is the # one every CI runner already has. pkgs.nodejs # The `docs` job publishes through this, and calls it the way # every other step calls its tool -- `nix develop -c # git-pages-cli ...` -- so it has to be here or that step dies # with `exec: git-pages-cli: not found` after the documentation # has already built. pkgs.git-pages-cli pkgs.pinact pkgs.radicle-node pkgs.reuse (zigFor pkgs) zon2nix.packages.${system}.zon2nix ]; }; } ); };}