Ginger Rhizome Plate from Köhler’s Medizinal-Pflanzen (1887), public domain, via Wikimedia Commons
Numerous data modeling systems allow you to consider a fleet of computers as one gestalt entity. The promise is a single source of truth describing any number of parameterized nodes, as an input for code generation allowing push-button integrations and deployments. Nix takes it to the furthest conclusion: our “model” can specify the entire operating system and program space of every node in the network, down to the exact build of every dependency.
A rhizomatic system gains a kind of introspection. The declarations live inside the system, and the system converges on them. I’ve laid out a few of the tools and techniques that I’ve adopted. If you are responsible for 2 or more computers then I think there’s a huge quality-of-life gain going down this route. Especially now that you don’t actually have to write the Nix and Bash! You just have to know what to ask for.
Who Herds the Herdrs
The relationships of humans to code and tooling are changing so quickly that I feel any in-depth workflow analysis will quickly become dated. That being said, I’m finding Herdr suiting my needs at the moment. Namely, surfacing the 3-dimensional matrix:
(REPOS) x (WORKTREES) x (AGENTS)
If you’re hardware-constrained, agent fanout can pretty quickly hog your resources. So in a rhizomatic system it might make sense to have a single Herdr instance running on the machine with the most system RAM - call it the “build machine” or .#buildr.
But your $200 refurbished HP Spectre with 8 GB of RAM can still be useful for getting work done! You can just provide it a thin wrapper package:
herd = pkgs.writeShellApplication {
name = "herd";
runtimeInputs = [ pkgs.openssh ];
text = ''
exec ssh -t buildr ${buildrHerdr} "$@"
'';
};
Now you’ve got your 1 canonical Herdr instance shared across the fleet. Cool! Granted this isn’t doing much - we’re just baking in an ssh command that uses the already-cool functionality. But this is our starting point for the remote-dev workflows of the rest of the post.
More Remote Development
Alright, so you can control development on .#buildr. What if you want to run your latest work on the Spectre? And also, what if your app is a generic FHS binary, i.e. it makes assumptions that no longer hold in NixOS? Well, it couldn’t possibly be easier. nixpkgs already has a helper to make an FHS wrapper:
myAppFhs = pkgs.buildFHSEnv {
name = "my-app-fhs";
targetPkgs =
p: with p; [
alib
...
zlib
];
runScript = pkgs.writeShellScript "myapp-fhs" ''exec "$@"'';
};
Then Spectre gets a package:
myAppRun = pkgs.writeShellApplication {
name = "my-app-run";
runtimeInputs = [
pkgs.openssh
pkgs.rsync
pkgs.coreutils
myAppFhs
];
text = ''
remote_host="buildr"
echo "my-app-run: building on $remote_host..." >&2
# ssh starts in the home directory, so the path is relative to it
if ! remote=$(ssh "$remote_host" 'cd my-app && nix build .#my-app.export --print-out-paths' | tail -n 1) || [ -z "$remote" ]; then
echo "my-app-run: build failed" >&2
exit 1
fi
local_dir="''${XDG_CACHE_HOME:-$HOME/.cache}/my-app"
mkdir -p "$local_dir"
rsync -z --partial --info=progress2 "$remote_host:$remote/bin/my-app" "$local_dir/my-app"
chmod +x "$local_dir/my-app"
exec my-app-fhs "$local_dir/my-app" "$@"
'';
};
This triggers the remote build, copies the binary over, and runs it in the FHS wrapper. That’s a nice facade for doing dev work from a machine that is too weak to really do much on its own!
Sharing the Box
Now .#buildr holds every agent in the fleet, and does all the builds, on top of whatever jobs it already had (such as being the household media server). All by virtue of having a relatively beefy 16 GB of RAM. Wait, that’s it?! Agent fanout is still a big risk here, so we’ll need to mitigate it in-place a bit.
Download More RAM
Well, first we ought to download some more RAM. zstd gets about 4.5:1 on this machine, so 7.7 GB of swapped pages costs about 1.7 GB of real memory:
zramSwap = {
enable = true;
memoryPercent = 50;
};
boot.kernel.sysctl = {
# make the kernel prefer a compressed swap over throwing away page cache
"vm.swappiness" = 180;
# ...and turn off readahead, which only pays off on a real disk.
"vm.page-cluster" = 0;
};
Cap Services at the systemd Level
This is a nice parameter available at the service level, which simply fences off the green, yellow, and red zones for whatever you’re running. Making sure that the media server doesn’t interfere with the rest of the system:
systemd.services.jellyfin.serviceConfig = {
MemoryHigh = "2G";
MemoryMax = "3G";
};
Make Build Slots In Projects
zram only serves to soften spikes - if our agents spin out many parallel builds, they can still gum things up. There’s any number of ways to solve this. We could start herdr in its own systemd scope and use the same technique from the last section. I elected instead to use a handful of lock files in the runtime directory, outside the project tree, so they’re shared by every worktree and no daemon or coordinator is involved:
take_slot() {
local dir="${XDG_RUNTIME_DIR:-/tmp}/myapp-build-slots"
mkdir -p "$dir"
while :; do
for i in $(seq 1 "${MYAPP_BUILD_SLOTS:-3}"); do
exec {slot_fd}>"$dir/$i.lock"
flock -n "$slot_fd" && return 0
exec {slot_fd}>&-
done
sleep 1
done
}
Then either introduce a convention and hope your agent follows it, or enforce it by injecting a wrapped version of your builder into the agent’s dev shell:
buildGated = pkgs.writeShellApplication {
name = "wrapped-builder";
runtimeInputs = [ pkgs.util-linux ];
text = ''
# take_slot as defined above
take_slot
exec ${real-builder} "$@"
'';
};
If an agent tries to build on a busy system, from its perspective it simply takes longer than normal as it waits for a slot to free up. The builder inherits the lock’s file descriptor, so the slot frees itself however the run ends, even if it crashes or the out-of-memory killer takes it. One caveat: a builder that leaves a background daemon running hands the descriptor to it, and the slot stays taken until that daemon exits.
Summary
I’ve tried to ground the idea of a rhizomatic system in concrete examples. NixOS allows us to treat a group of computers as a single programmable unit. To do so we need to consider both the way they inter-relate, and how each one functions on its own to be an effective member. The main thesis to take away is that it’s fun and easy to do things in NixOS!