AIFeed

Software purpose built for Nix

Original text(via Lobsters)

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.

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.

Change my mind meme. A man sits at a table on a campus lawn behind a sign that reads "nixpkgs is Wine for FHS software. Change my mind".
Change my mind meme. A man sits at a table on a campus lawn behind a sign that reads "nixpkgs is Wine for FHS software. Change my mind".

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.

We ported the software with Nix. Nobody ported it for Nix.

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

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.


        $ nix build nixpkgs#ansible

$ file result/bin/ansible
result/bin/ansible: a /nix/store/8favl12d…-bash-5.3p9/bin/bash -e script, ASCII text executable

$ wc -l < result/bin/ansible
111
      

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:


        #!/nix/store/9qkpw966…-python3-3.13.18/bin/python3.13
# -*- coding: utf-8 -*-
import sys;import site;import functools;sys.argv[0] = '/nix/store/1y48rvcj…-python3.13-ansible-core-2.21.1/bin/ansible';functools.reduce(lambda k, p: site.addsitedir(p, k), ['/nix/store/1y48rvcj…-python3.13-ansible-core-2.21.1/lib/python3.13/site-packages','/nix/store/yqrymwyh…-python3.13-ansible-13.7.0/lib/python3.13/site-packages','/nix/store/j0diqhis…-python3.13-passlib-1.9.3/lib/python3.13/site-packages', … 75 more … ], site._init_pathinfo())
      

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. 🐌

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.

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.

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.

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.

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.

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.

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.

RuntimeSearchesSomebody’s fix
ELFRUNPATH × NEEDEDShrinkwrap , Guix’s per-package loader cache
JVMCLASSPATHJarIndex , AppCDS
Pythonsys.pathsys.meta_path finders, PEP 552
Nodenode_modules , walking up to /Yarn Plug’n’Play
Ruby$LOAD_PATH × gemsbootsnap
ShellPATHresholve

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.

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.

We need software purpose-built for Nix. 🩷

The text above was extracted automatically from the source page and may miss images, tables or code blocks. The original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗