NixOS 101 Course / Section 3: Your own NixOS service modules
Premium

3.3 Exercise: add an openFirewall option to your service module

Lesson 15 of 28 · 26:56

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Exercise Overview and VM Setup
  2. 3:08 How the NixOS VM Boots Quickly
  3. 10:11 Adding the hello Service Option
  4. 14:39 Opening the Firewall Port
  5. 21:58 Inspecting Configurations in the REPL
Transcript

Exercise Overview and VM Setup

0:00 So in the last video I already had a look into what is inside this zip ball, how do we run a virtual machine with a randomly new NixOS module that we just wrote ourselves. And we have seen how we can pop up a QEMU window with our NixOS module and play around with it. Now let's get to the actual exercise. The exercise is split into two parts. First part is when we run a nix-build that creates this result folder with bin/run-nixos-vm inside. There is a shell script.

0:32 We didn't really look into that yet and that is the first part of the exercise. So please open this script and then ask yourself why does rebuilding a whole VM go so quickly? So we have seen in the video before that when we change the NixOS configuration to enable or disable something and rebuild the system, it takes a few seconds. So it did take a few seconds, but only a few seconds. If you were using software like Vagrant or other tools that create virtual machines from some kind of description for you, then this would take much, much longer because it would have to rebuild the whole system and then create a new image and creating a new image after the old one that kind of takes more time than the few seconds that we had before.

1:21 And this is really important because changing some virtual machine, running it, finding out that it doesn't do what you like, changing it again, rerunning it, if that goes quickly, then this is a great iteration cycle for you with nice quick feedback. And that is also part of the NixOS test driver, as we will see later. Not in this video, in one of the later ones, right? And then after we found out why it builds so quickly, we're going to change the actual module.

The full transcript comes with the lesson.

About this lesson

Practice time with two tasks: First you open the generated VM script and work out why rebuilding a whole NixOS VM takes only seconds. Then you add an openFirewall option to the Hello as a Service module, just like many nixpkgs services offer, using nothing but search.nixos.org and what you have learned so far.

In this video

  • A puzzle about VM rebuild speed. The answer might change how you think about VMs
  • Designing an option for administrators that simply want to allow the firewall to open without knowing or hardcoding the configured port
  • The trap people fall into when they write conditional configuration inside an attribute set
  • Three idiomatic ways to express conditional configuration changes
  • Checking the result in the REPL instead of booting the VM again

After this lesson

You have extended a NixOS service on your own, and you know the idioms that keep conditional module code short and readable.

Links: