# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie # 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"; }; # The toolchain is the official 0.17.0 release binary, packaged by the # overlay, rather than nixpkgs' Zig. zig = { url = "git+https://git.jcollie.dev/jeff/zig-overlay.git"; inputs = { nixpkgs.follows = "nixpkgs"; zon2nix.follows = "zon2nix"; }; }; zon2nix = { url = "github:jcollie/zon2nix"; inputs = { nixpkgs.follows = "nixpkgs"; }; }; }; outputs = { self, nixpkgs, zig, 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; # The official 0.17.0 release binary, packaged by the overlay, rather # than nixpkgs' Zig. zigFor = pkgs: zig.packages.${pkgs.stdenv.hostPlatform.system}."0.17.0"; # 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 --17 --nix=build.zig.zon.nix build.zig.zon # # It has to list the lazy dependencies too. uucode is lazy, but the # WebAssembly module always has `uri_unicode` in it, 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"; zig_0_17 = zigFor pkgs; }; # 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. It always has 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.4.0"; src = self; nativeBuildInputs = [ (zigFor pkgs) ]; dontConfigure = true; dontInstall = true; buildPhase = '' runHook preBuild export ZIG_GLOBAL_CACHE_DIR="$TMPDIR/zig-global" zig build wasm \ --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 == 0, f"{skipped} skipped: nothing in the suite is conditional" 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 ( '' in dom ), dom # Parsed into decoded segments... assert '["a",".","b","..","c/d"]' 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 ]; }; } ); }; }