Any Nix package, live in your browser
Nix, a package manager, can now be experienced live within a web browser. Try it out at trynix.dev, selecting various packages from the past and present, even those without a public cache. A shell boots in the tab, granting access to a Linux machine loaded with those Nix packages on the PATH. This innovative concept was built upon the creation of nixpkgs-multiverse, which indexes every version of every package nixpkgs has ever shipped.
Grail provided version ranges, while omniflake allowed the addition of over sixteen thousand flakes from a single input. The key insight revealed the "fast-mode" of nixmultiverse.com, enabling users to bypass evaluation and directly access the store path. This allows users to utilize Nix's capabilities without dealing with the complexities of evaluation and building.
By asking for a package, the exact store path that Hydra built for it can be obtained, regardless of the version it ever had. If those binaries are available, they can be run directly in the browser, leading to the creation of trynix, a browser-based Nix package runner. The browser displays a Linux machine in memory, complete with a Nix store, Linux kernel in WebAssembly, and a terminal emulator.
This innovative setup allows users to run any command-line program in the store without any pre-installed dependencies. The most remarkable aspect is that users are not limited to the public cache. They can share a store path they built themselves, and anyone can boot it in their browser tab, as long as the cache is served with the access-control-allow-origin: * header, which GitHub Pages, Cachix, and cache.nixos.org support.
Importantly, the browser can access store-paths from these caches, making the browser a legitimate Nix client. The crucial piece missing was a way to run the binaries, which were initially x86-64 or aarch64 ELF executables. However, standing on the shoulders of giants, WebAssembly was employed to run real x86_64 kernels inside the browser tab.
This breakthrough enables the browser to boot a real x86_64 kernel with a Nix store, fetched using nixpkgs-multiverse. Despite the complexity, there is no server in action; everything is a static web page with publicly accessible caches. The virtual machine exists solely within the user's browser tab. This project showcases the power of Nix in managing multiple versions of the same package, enabling the booting of two versions of hello in a single machine without conflicts.
As the VM is started, more store-paths can be added while it runs, similar to adding more paths to the user's own /nix/store. The site employs tricks to make the experience feel instant, prefetching the engine and VM snapshot in the background and resuming the boot process from a previous snapshot. While not instant, the experience is fast enough to be usable.
Despite the performance improvements, running binaries under emulation is still slow, and the closure size has an upper limit of ~1.5GiB. Nevertheless, this innovative approach opens up new possibilities for using and leveraging Nix in a browser environment.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.