How I Rebuilt My Homelab with NixOS

Rebuilding My Homelab with NixOS — Part 1: The Overview

AI Disclosure:

AI was used to help troubleshoot and fix my Nix configuration files, as well as correct the grammar in this post.


Intro

Hello! This is my first post here!

I work as a sysadmin, and I hope you all have a nice day!

Before we get started, a quick note: I’ve already finished rebuilding my homelab. I’m writing this post to share how it went, what worked, and what didn’t.

My homelab previously ran mostly on Debian-based systems. In this post, I want to take you along for the ride and explain why I decided to rebuild my homelab using NixOS, as you might have guessed from the title.

Now, why NixOS? Well, at work, we’re looking to move more towards GitOps, and I wanted to see if NixOS could scratch that itch. I also wanted to learn a completely new way of managing packages and system configurations.

I’m planning to make this a series of posts. This first post will focus on an overview of my homelab, including the hardware, network, and services I was running before the rebuild.

In future posts, I’ll go through each host, explain how I installed and configured NixOS, and share the problems I ran into along the way.

The Plan

First, I needed a plan before I started breaking things!

  1. Inventory: Make a list of all the services I was running and where they were hosted.
  2. Hardware: Figure out which machines would run NixOS and what their roles would be.
  3. Configuration: Set up a Git repository to manage my NixOS configurations.
  4. Secrets: Figure out how to manage secrets without putting passwords or API keys in Git.
  5. Backups: Make sure my important data was backed up.

Did I follow this plan perfectly? Of course not. xD

Hardware in My Homelab

My homelab consists of four Raspberry Pis, a QNAP NAS, and an old PC. I also have a reverse proxy running as a VM on Proxmox.

Physical Hardware

Hostname Hardware CPU RAM
pi01 Raspberry Pi 4 Model B Broadcom BCM2711 2 GB
pi02 Raspberry Pi 5 Broadcom BCM2712 8 GB
pi03 Raspberry Pi 5 Broadcom BCM2712 8 GB
pi04 Raspberry Pi 5 Broadcom BCM2712 8 GB
nas02 QNAP TS-251A Intel Celeron N3060 (2C/2T) 2 GB
nas01 Old PC Intel Xeon E5-2620 v4 (8C/16T) 64 GB

Nothing too fancy, but more than enough hardware to have some fun with NixOS!

Reverse Proxy VM

My reverse proxy is hosted on a Proxmox node outside my Raspberry Pi setup.

Component Details
Hostname proxy-anne-prive
Proxmox node huygens02
CPU 4 vCPUs
RAM 8 GiB
Boot disk 64 GiB
Networking Public IPv4 and IPv6

I’ll explain what this VM does a little further down.

Network Setup

Before getting into the NixOS configuration, here’s a quick overview of how my homelab was connected before the rebuild.

For my local network, I use UniFi networking equipment, consisting of a router, three switches, and an access point.

Local Network

Device Hostname Connection
Router router01 1 GbE
Switch swh01 2.5 GbE
Switch swh02 1 GbE
Switch swhome01 1 GbE
Access Point ap01 1 GbE

VLANs

I also use VLANs to separate different types of devices on my network and keep things a bit more organized.

VLAN Name VLAN ID Purpose
network-gear 1 Network infrastructure
servers 10 Servers and homelab services
wifi 11 Wi-Fi devices
cameras 12 Security cameras
clients 13 Client devices

Nothing too complicated, just some basic network segmentation to keep things tidy.

WireGuard and Reverse Proxy

Before the rebuild, all four Raspberry Pis were connected to my reverse proxy through WireGuard tunnels.

The reverse proxy runs as a VM on Proxmox and has both a public IPv4 and IPv6 address. I use UFW (Uncomplicated Firewall) to manage the firewall rules.

This setup allowed me to expose my self-hosted services through the reverse proxy without directly exposing the Raspberry Pis to the internet.

Nginx handled incoming requests and forwarded them to the appropriate services over the WireGuard tunnels.

During the rebuild, I also moved from my old WireGuard setup to Headscale, but I’ll cover that in another post!

Services Running in My Homelab Before NixOS

Now that we’ve covered the hardware and network, let’s take a look at what I was running before switching to NixOS.

And yes, there were quite a few services. xD

pi01 — Raspberry Pi 4 (2 GB RAM)

Before the rebuild, pi01 was running Debian-based Linux with the following services:

Service Purpose
PostgreSQL (psql) Relational database
Valkey In-memory data store
SearXNG Privacy-focused metasearch engine
MQTT Message broker

Not bad for a little Raspberry Pi 4 with only 2 GB of RAM! xD

pi02 — Raspberry Pi 5 (8 GB RAM)

Before the rebuild, pi02 was running quite a few self-hosted services.

Service Purpose
Signal K Marine data server
n8n Workflow automation
Mastodon Self-hosted social network
Nextcloud File synchronization and cloud storage
Garage S3-compatible object storage
ArchiveBox Web archiving
Joplin Server Note synchronization
Redlib Privacy-friendly Reddit frontend
Iocaine AI tarpit
FreshRSS RSS feed reader

Quite a lot for a little Raspberry Pi 5!

pi03 — Raspberry Pi 5 (8 GB RAM)

Before the rebuild, pi03 was running a mix of messaging, home automation, and other self-hosted services.

Service Purpose
Matter Server Matter smart home device integration
WhatsApp WhatsApp integration
ntfy Push notification service
Data Data storage
Home Assistant Home automation platform
Synapse Matrix homeserver
Iocaine AI tarpit
Vaultwarden Self-hosted password manager
Logs Logging service
ESPHome ESP device management
Matter Matter-related service
TadoLocal Local Tado integration
Signal Signal integration
Discord Discord integration
signal-cli Signal command-line integration

Apparently, I decided that 8 GB of RAM was enough to run half the internet. xD

pi04 — Raspberry Pi 5 (8 GB RAM)

Before the rebuild, pi04 was running a mix of automation, archiving, and other self-hosted services.

Service Purpose
n8n Workflow automation
Mastodon Self-hosted social network
ArchiveBox Web archiving
Iocaine AI tarpit
Signal K Marine data server

And yes, some services appear on multiple hosts.

A Quick Note About Iocaine

You might have noticed Iocaine appearing on multiple machines.

Iocaine is an AI tarpit designed to protect servers from unwanted AI crawlers. Instead of serving useful content, it can lead crawlers through generated pages, wasting their time and resources.

And yes, the name is a reference to The Princess Bride. xD

Reverse Proxy — Proxmox VM

Besides the Raspberry Pis, I also had several services running on my reverse proxy VM.

This machine acts as the public entry point for my self-hosted services.

Service Purpose
Nginx Reverse proxy for self-hosted services
Stalwart Mail server
WireGuard Easy (wg-easy) WireGuard VPN management
Element Call Self-hosted Matrix calling
ACME TLS certificate management
UFW Firewall management

Nginx handled incoming HTTP and HTTPS traffic and forwarded requests to the appropriate services through WireGuard.

I also used Stalwart for email, Element Call for Matrix calls, and UFW to control which ports were accessible from the internet.

This setup worked pretty well, but I eventually decided to change how I managed the VPN connections.

More about that in a future post!

What About the NAS Servers?

You might have noticed that I haven’t covered nas01 and nas02 in the service overview.

They’re also part of my homelab, but I want to keep this first post focused on the Raspberry Pis, networking, and the services involved in the rebuild.

I’ll cover the NAS setup in more detail in a future post.

Why Rebuild Everything?

At this point, you might be wondering why I wanted to rebuild everything if it was already working.

Well, there were a few reasons.

The biggest one was that I wanted to learn NixOS and get more experience with declarative system configuration.

With my old Debian-based setup, I had configurations spread across different machines. I wanted to experiment with managing more of my infrastructure through configuration files stored in Git.

Since we’re also looking at GitOps more seriously at work, this seemed like a good opportunity to experiment with those ideas at home.

And, of course, sometimes you just want to rebuild something because you can. xD

What’s Next?

Now that you’ve seen the hardware, network, and services I started with, it’s time to get into the actual NixOS rebuild.

In the next posts, I’ll cover how I installed and configured NixOS on my Raspberry Pis, how I organized my configuration files, and what I learned along the way.

I’ll also go into more detail about managing secrets, moving from WireGuard to Headscale, and figuring out how to manage everything through Git.

There were definitely some things that didn’t go according to plan, but that’s part of the fun of running a homelab!

One post at a time, otherwise this would turn into a book. xD

Thanks for reading my first post!

If you have any questions, suggestions, or tips about NixOS, feel free to share them. I’m still learning, and I’m always happy to discover better ways of doing things.

3 Likes

Rebuilding My Homelab with NixOS — Part 2: Meet Húrin

AI Disclosure:

AI was used to help troubleshoot and fix my Nix configuration files, as well as correct the grammar in this post.


Intro

Hello again!

Welcome to Part 2 of my homelab rebuild series!

In Part 1, I went over my old homelab setup, including the hardware, networking, and services I was running before switching to NixOS.

Now it’s time to get into the fun part: actually getting NixOS running!

In this post, I’ll focus on Húrin, my Raspberry Pi 4 with 2 GB of RAM, which was previously known as pi01.

I’ll go through the bootloader problems I encountered, how I organized my NixOS configuration, and how I manage the machine remotely over SSH.

And, of course, things didn’t go perfectly. But where’s the fun in a homelab project if nothing breaks? xD

Meet Húrin

Húrin is a Raspberry Pi 4 Model B with 2 GB of RAM.

Before the rebuild, it was called pi01 and was running a Debian-based system.

It hosted several services, including:

  • PostgreSQL
  • Valkey
  • SearXNG
  • MQTT

Not bad for a little Raspberry Pi 4!

During the rebuild, I decided to give my machines new names and start managing them through NixOS.

My main goals were to keep the configuration modular, manage services declaratively, and make it easier to reproduce the setup if something went wrong.

Sounds simple enough, right? xD

The First Challenge: U-Boot and USB Booting

Of course, I ran into my first problem before I could even get NixOS running properly.

Húrin uses a USB HDD caddy for storage instead of relying on a microSD card.

Originally, I wanted to use U-Boot as part of the boot process.

However, during my testing, I couldn’t get the stable version of U-Boot I tried to work properly with my USB HDD caddy.

The Raspberry Pi 4 supports USB booting, but that doesn’t necessarily mean every USB storage enclosure will work with every bootloader configuration.

I spent some time testing and troubleshooting, but I couldn’t get it working with my setup.

The Solution: Raspberry Pi Bootloader

Instead of continuing to fight with U-Boot, I decided to try another approach: using the Raspberry Pi firmware bootloader to boot Linux without U-Boot.

And guess what? It worked! xD

With this approach, I was finally able to boot NixOS from my USB HDD caddy.

This doesn’t mean U-Boot can’t boot from USB on a Raspberry Pi 4. It just wasn’t working with my particular setup during testing.

However, there was one downside.

The Trade-Off: Losing the Boot Menu

By switching away from U-Boot, I lost the ability to select previous NixOS generations from a boot menu, like you can with GRUB on a regular PC.

This means that if I deploy a broken configuration that prevents the system from booting, I can’t simply select an older generation from the boot menu.

I can still roll back to a previous generation while the system is running, but recovering from a completely broken boot is less convenient.

It’s a trade-off I’m not particularly happy about, but for now, having a system that actually boots from my USB HDD caddy is more important.

Maybe I’ll revisit U-Boot in the future.

One problem solved, one convenience lost. Welcome to homelabbing! xD

Hardware Configuration

After getting the system to boot, the next step was making sure NixOS could access the storage properly.

My generated hardware-configuration.nix includes the following modules:

boot.initrd.availableKernelModules = [
  "xhci_pci"
  "usbhid"
  "uas"
];

These provide support for USB host controllers, USB input devices, and USB Attached SCSI storage.

Since I’m using a USB HDD caddy, having the necessary USB storage support available during early boot is important.

These modules help Linux access the hardware after the kernel starts. They don’t control whether U-Boot itself can detect the USB drive.

Filesystems

My filesystem configuration looks like this:

fileSystems."/" = {
  device = "/dev/disk/by-uuid/d1c24c24-7dc1-4630-aff7-db9850bcf486";
  fsType = "ext4";
};

fileSystems."/boot" = {
  device = "/dev/disk/by-uuid/656A-9F7D";
  fsType = "vfat";
  options = [ "fmask=0022" "dmask=0022" ];
};

swapDevices = [ ];

I’m using ext4 for the root filesystem and FAT for the boot partition.

I’m still thinking about switching the root filesystem to ZFS, but I haven’t made that change yet.

I’m also referencing the partitions by UUID instead of using device names such as /dev/sda1, since device names can change.

And as you can see, I haven’t configured any swap devices.

Organizing My NixOS Configuration

One of the main reasons I wanted to try NixOS was its declarative approach to system configuration.

With my old Debian-based setup, I had configuration files and services spread across different machines.

I wanted to experiment with managing everything through Nix files stored in Git.

Another thing I wanted to avoid was having one massive configuration.nix containing everything.

So I decided to split my configuration into smaller modules.

Imports

Here’s how the imports in Húrin’s configuration.nix look:

imports = [
  ../../modules/common/base.nix
  ../../modules/common/admin.nix
  ./services/postgresql.nix
  ./services/headscale.nix
  ./services/nginx.nix
  ./services/backup.nix
];

This allows me to separate common system settings from host-specific services.

For example, base.nix contains common system settings, while admin.nix manages my administrator configuration and stores my authorized SSH public keys.

PostgreSQL, Headscale, Nginx, and backups each have their own service modules.

Managing SSH Keys with admin.nix

Another thing I wanted to manage declaratively was SSH access.

Instead of manually adding my SSH public keys to every Raspberry Pi, I keep them in my admin.nix module.

This allows me to manage which keys are authorized to access my machines from a single configuration file.

Since the module can be imported by multiple hosts, I don’t have to configure the same SSH keys manually on every machine.

And if I ever need to add or remove a key, I can update the module and deploy the new configuration.

One less thing to manage manually!

Of course, I’m only storing SSH public keys in my NixOS configuration. My private SSH keys stay private and are not committed to Git.

I find this approach much easier to manage than putting everything in one massive configuration file.

If I want to change something related to PostgreSQL, I know exactly where to look.

And if I break something, at least I know which file to blame. xD

Managing NixOS Over SSH

Another thing worth mentioning is that I manage my NixOS configurations remotely over SSH.

Instead of sitting in front of each Raspberry Pi and configuring everything manually, I can work on my configurations from another machine and use SSH as part of my deployment workflow.

This is especially useful when managing multiple Raspberry Pis.

However, there’s one thing I have to be careful about: deploying a broken configuration.

For example, if I accidentally mess up the network or SSH configuration, I could lock myself out of the machine.

And because I switched away from U-Boot, I no longer have the convenient boot menu for selecting an older NixOS generation.

That means I have to be a little more careful when making changes that could affect booting or network access.

Otherwise, I might end up physically connecting to the Raspberry Pi to figure out what I broke.

Which, knowing me, will probably happen eventually. :wink:

Basic System Configuration

Let’s take a look at some of the basic settings.

Hostname and Architecture

For Húrin, I configured the following:

networking.hostName = "hurin";
networking.useDHCP = lib.mkDefault true;

nixpkgs.hostPlatform = "aarch64-linux";

The hostname is set to hurin, and the system uses the aarch64-linux architecture because it’s running on a Raspberry Pi 4.

I also use DHCP, but I’ve configured a DHCP reservation on my router so Húrin always gets the same IP address.

Binary Caches

Now, here’s a fun story. xD

When I first started experimenting with NixOS on Húrin, I did my first build without configuring any additional binary caches.

And well… that poor Raspberry Pi 4 with only 2 GB of RAM spent 24 hours building the system!

Yes, you read that right. 24 hours.

After that experience, I decided that maybe asking a little Raspberry Pi to compile everything wasn’t the greatest idea.

So I added some binary caches to my NixOS configuration:

nix.settings = {
  trusted-users = [ "root" "anne" ];

  substituters = [
    "https://cache.nixos.org"
    "https://nixos-raspberrypi.cachix.org"
  ];

  trusted-public-keys = [
    "nixos-raspberrypi.cachix.org-1:4iMO9LXa8BqhU+Rpg6LQKiGa2lsNh/j2oiYLNOQ5sPI="
  ];
};

These caches allow Nix to download prebuilt packages instead of compiling everything locally, as long as compatible binaries are available.

NixOS normally uses cache.nixos.org by default, but adding the Raspberry Pi-specific cache helps avoid unnecessary local builds when suitable binaries are available.

This made a lot more sense for a Raspberry Pi with only 2 GB of RAM.

Lesson learned: Just because a Raspberry Pi can compile something doesn’t mean you should make it do so for 24 hours.

System Packages

I also installed some tools that I like having available:

environment.systemPackages = with pkgs; [
  git
  nano
  btop
  curl
  dnsutils
  postgresql_17
  sqlite
  restic
];

Nothing too special here, just some tools for managing and troubleshooting the system.

I use Git for version control, btop for monitoring, and tools like curl and dnsutils for troubleshooting.

Restic is also included, but I’ll get into that later.

Networking and Firewall

Another thing I wanted to manage declaratively was the firewall.

Instead of manually configuring firewall rules and hoping I remembered everything, I wanted those rules to be part of my NixOS configuration.

Here’s what I have in configuration.nix:

let
  lanSubnet = "192.168.10.0/24";
in
{
  networking.firewall = {
    enable = true;
    allowedTCPPorts = [ 80 443 ];
    allowedUDPPorts = [ 3478 ];

    extraCommands = ''
      iptables -A nixos-fw \
        -s ${lanSubnet} \
        -p tcp --dport 5432 \
        -j nixos-fw-accept
    '';
  };
}

The firewall allows TCP ports 80 and 443, along with UDP port 3478.

I also added a rule to allow PostgreSQL connections on port 5432 from my server subnet. I’ve redacted part of the subnet address in the explanation for this post.

Having these rules defined in NixOS means I can keep track of them alongside the rest of my configuration.

It’s one less thing I have to configure manually after reinstalling a machine.

Services on Húrin

With NixOS running, I could start configuring the services.

Húrin currently imports the following service modules:

Service Purpose
PostgreSQL Relational database
Headscale Self-hosted control server for Tailscale clients
Nginx Web server and reverse proxy
Backup Backup configuration

Compared to the old Debian setup, the configuration is now organized into individual Nix modules.

I wanted to keep the services separate so I could maintain them more easily.

Headscale: Moving Away from Raw WireGuard

One of the bigger changes during my rebuild was moving away from manually managing WireGuard tunnels.

Previously, all my Raspberry Pis connected to my public reverse proxy using WireGuard.

While this worked fine, managing the individual WireGuard configurations was becoming a bit of a hassle.

I wanted something easier to manage, especially with multiple machines in my homelab.

That’s when I decided to give Headscale a try.

Headscale is a self-hosted alternative to the Tailscale control server. It allows me to manage my devices in a mesh VPN without having to manually configure individual WireGuard tunnels.

The nice thing is that Tailscale still uses WireGuard underneath, so I’m not completely moving away from WireGuard itself. I’m just changing how I manage it.

And since I was already rebuilding everything with NixOS, I figured this was the perfect time to make the switch.

Less time managing VPN configurations, more time breaking other things! xD

Backups: From Ansible to Restic

Another thing I changed during the rebuild was my backup setup.

Previously, I was managing my backups using Ansible.

During the move to NixOS, I decided to switch to Restic and move my backup configuration into NixOS as well.

The idea was to keep my backup configuration alongside the rest of my system configuration rather than managing it separately.

Húrin has its own backup.nix module, and Restic is included in the system packages.

However, I’m not going to explain the entire backup setup here.

I want to dedicate the final post in this series entirely to backups, including how I configured Restic, why I moved away from my Ansible-based setup, and what I learned.

After all, backups deserve their own post.

Especially when you’re rebuilding your entire homelab and breaking things along the way.

What Did I Learn?

Getting NixOS running on Húrin taught me a few things.

First, just because something should work doesn’t mean it will work with your particular hardware.

The whole U-Boot and USB HDD caddy adventure was a good example of that.

Second, I really like the idea of managing my system through configuration files.

Having my services, packages, and firewall rules defined in Nix makes it much easier to see how the machine is configured.

Third, managing machines remotely over SSH is convenient, but you have to be careful about changes that could leave a machine unreachable.

Especially when you don’t have a boot menu to fall back to an older configuration.

And finally, I learned that making a Raspberry Pi 4 with 2 GB of RAM build a system for 24 hours probably isn’t the best use of its limited resources. xD

What’s Next?

That’s it for Húrin!

In this post, I went over the bootloader problems I encountered, how I got NixOS running from my USB HDD caddy, and how I organized the configuration.

I also covered how I manage the machine remotely over SSH and some of the trade-offs I had to make.

In the next posts, I’ll continue with the other machines in my homelab and explain how I configured their services.

I’ll also cover my move from raw WireGuard to Headscale and how I manage my NixOS configurations through Git.

And to finish the series, I’ll dedicate a full post to my Restic backup setup.

One machine down, a few more to go! xD

Thanks for reading, and see you in the next post!

If you have any suggestions or tips about NixOS on Raspberry Pis, feel free to share them. I’m still learning, and I’m always happy to discover better ways of doing things.

2 Likes