NixOS 101 Course / Section 5: NixOS containers
Premium

5.1 Declarative NixOS containers

Lesson 20 of 28 · 6:45

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to NixOS Containers
  2. 0:48 Wrapping a Service in a Container Configuration
  3. 2:06 Rebuilding and Verifying the Containerized Service
  4. 3:51 Using machinectl and Shell Access Inside the Container
  5. 5:06 Container Use Cases and Private Networking
Transcript

Introduction to NixOS Containers

0:00 In this relatively short section, I want to show you NixOS containers, which are really useful to have like nicely and composable systems. Let's have a quick look. So what does this mean? You might already know container technology from like Docker, systemd-nspawn and so on. And these basically just like the Nix sandbox built on Linux namespaces. And in general, on a NixOS configuration, when you don't run containerized, you have your configuration module and then you have some kind of service configuration, right?

0:35 You can take the same configuration and in this example, it would just run on your normal system and simply wrap it in this block here, where you say containers.webserver, webserver

Wrapping a Service in a Container Configuration

0:48 would be just a given name and then autoStart so that the container also starts automatically. privateNetwork false means that the container would get the same network as the host system. You can also give the private network, but then the example would be much longer. And then we put the same lines that we had here, like random NixOS configuration, basically into that block here. And then you can rebuild the system and have it all running in a container. Let's have a look at how this looks like and how it works.

The full transcript comes with the lesson.

About this lesson

Wrap a piece of your NixOS configuration in a few extra lines, and it runs in its own lightweight container. This short lesson moves the Hello as a Service module from the host into a declarative NixOS container and shows how to look inside.

In this video

  • The containers block, and what autoStart and privateNetwork do
  • Moving a service into a container without changing the service module
  • Spotting the container in systemctl status and opening a shell inside it
  • Where to read on for private networks, routing, and firewalls between containers

After this lesson

You can isolate services on one host, for example several instances of the same web server, with the declarative configuration you already use.