Recent findings suggest that most software presumes that everything around it can change. This is an idea I’m radically challenging with some of my work such as in reducing the startup overhead for dynamically linked binaries.

Nix is beautiful and immutable. Once built, nothing changes. Any small change you make cascades up through the dependency graph, triggering rebuilds of everything that depends on it, like some fractal pattern unfolding through the store.

Despite this lovely property, most software is not designed to take advantage of it.

Nixpkgs is Wine for FHS software.

It has been revealed that despite the largess of packages in Nixpkgs, most of them still rely on the compatibility layer to function correctly within the store-model. They are written for a filesystem with /usr/lib and /etc, where files get replaced underneath you, and we run them on Nix through a compatibility layer.

We need to stop writing software WITH Nix but rather FOR Nix.

What is the Wine compatibility layer for FHS software? It is the substituteInPlace, wrapProgram, patchShebangs, and patchelf tools that adapt software expecting a mutable filesystem to the immutable Nix store.

Sources indicate that we ported the software with Nix. Nobody ported it for Nix.

Sources indicate that why am I making such a distinction? The difference between “with” and “for” Nix can be profound.

Recent findings suggest that software written for Nix can assume immutability, predictable paths, and a fully known dependency graph. It can skip the compatibility layer entirely, leading to faster startup times, simpler code, and fewer runtime surprises.

Let’s look at one real program first: ansible from nixpkgs.

In a recent development, in order to run ansible, Nixpkgs builds a wrapper script that sets up the environment correctly. It prepends 18 directories to PATH and performs other necessary environment adjustments before invoking the real entry point.

That “entry point” is the upstream Python script with one line injected by wrap-python.nix:.

Sources indicate that nixpkgs adds 78 site-packages directories to sys.path in that single line. Python will then search each of those directories for the modules it needs, one by one. 🐌.

Industry observers note that this is despite us knowing exactly where every module lives at build time. 🤦.

We will see shortly how Nix can leverage that knowledge to avoid all this redundant searching.

Recent findings suggest that nothing is unique to Python here. Every language has some equivalent to sys.path or PATH that it searches for modules, libraries, or binaries at runtime.

According to recent reports, I ran strace over the --version flag of fifteen programs from nixpkgs and counted the file lookups that came back ENOENT. This is a measure of the redundant work each program does while trying to locate its dependencies.

In a recent development, we are doomed to repeat needless work. jekyll asks the filesystem for a file that is not there 8,167 times before it prints its version, and cloc fails 95% of its lookups. The waste grows with every dependency, and on a slow or networked filesystem each of those misses is a round trip.

The problem goes beyond just redundant lookups. It is compounded by the fact that many programs do not trust their own caches and repeatedly re-validate files that are guaranteed not to change, leading to unnecessary computation and I/O overhead.

Industry observers note that A classic example is Python’s handling of compiled bytecode. Even though the source files are in the immutable /nix/store, Python still opens and reads them to verify that the cached .pyc files are up to date, resulting in unnecessary I/O.

aws --version for instance re-reads and hashes 869 source files, only to confirm that the .pyc files it already has are still valid.

Sources indicate that aside Python already has the fix: PEP 552 defines an unchecked hash mode for sources that cannot change. Nixpkgs does not use it, because SOURCE_DATE_EPOCH makes compileall default to the checked mode.

According to recent reports, the idea of doing needless work is not hard to grasp. Nothing here is incredibly novel and most already have solutions in place for their respective ecosystems.

Sources indicate that the twist with Nix is that these tables can be precomputed and guaranteed to be correct, because the inputs are immutable. There is no need for invalidation logic or runtime checks. They reside in the /nix/store and are inputs like any other.

Industry observers note that nix changed how we build software. It has barely changed the software we build.

We need to think beyond using Nix purely for its reproducibility guarantees and start designing software that is purpose-built for the immutability and precomputed knowledge that the Nix store provides.

Those who cannot remember the past are condemned to repeat it. Our programs cannot remember their own build, so they repeat it on every start.

Industry observers note that we need software purpose-built for Nix. 🩷.

📌 Read also:

→ Read original article