NixOS Infrastructure-as-Code — Set it up once, switch it on anywhere

NixOSInfrastructure as CodeNix FlakesReproducible SystemsPlatform Engineering
Image 1
Image 2
Image 3
Image 4
Image 5

Slides (PDF)

The problem

Setting up a PC by hand cost me most of a day. The shell, Docker, SSH hardening, games. Then the same again on the second PC.

And again after every reinstall.

Example: two PCs

This example is made up to show the idea, with my rough estimates. Two PCs, four modules, set up by hand:

ModuleDesktopLaptop
Shell + prompt2 h1 h
Docker2 h1 h
SSH hardening1 h1 h
Games2 h—
Total7 h3 h

Now each module is written once in the flake. Each PC says yes or no with one line:

ModuleDesktopLaptop
Shell + promptonon
Dockeronon
SSH hardeningonon
Gamesonoff

The desktop and the laptop share the shell, Docker and the SSH hardening. Only the desktop gets the games.

When I change a module, both PCs pick it up on the next update. So a fix can't be forgotten on the other machine.

How it works

Zoom
  • One defaults file. Every switch lives in lib/defaults.nix. Almost all are off.
  • Profiles turn things on. A machine profile inherits a type base and sets only what it needs.
  • Modules only read switches. A module never asks which machine it's on. GPU code checks the GPU type, never the machine name.

A module is plain Nix. The Steam module, for example:

Adding a machine is a type base plus the switches it needs.

What's shared, what's not

ConcernWhere it's on
Firewallevery profile
Backupsdesktops and laptops
Disk encryptiondesktop, laptop, VPS and NAS (not WSL)

How it deploys

  • Every push is built by a build farm.
  • The servers and a few other machines deploy weekly on a schedule, with a post-deploy check and automatic rollback.
  • My own desktop and laptop deploy when I click.

More on the deploy side in deploy-sh-guardrail.

Today

WhatCount
Machine profiles10 (desktops, laptops, a Windows WSL setup, a VPS, a NAS)
Switches in lib/defaults.nix219, 204 off by default
Tracked .nix files354
Commitsover 3,000 since July 2024

It started as a fork of LibrePhoenix's nixos-config. The switches, the profile inheritance, the server profiles and the deploy tooling came after.

Stack

NixOS, Nix flakes, Home Manager, NixOS-WSL, systemd, Sway, Stylix, LUKS, Restic, fail2ban, Bash, Git, git-crypt.

Status

The repo is private. It runs daily on my workstations, laptops, a production VPS and a ZFS NAS.

Related: my-homelab (what runs on top of it) and vps-wireguard (the network that connects it).