NixOS 101 Course / Section 2: NixOS configuration and the module system
Premium

2.3 Exploring the NixOS module system in the REPL

Lesson 10 of 28 · 11:23

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Introduction to writing NixOS modules
  2. 0:55 Defining options and types
  3. 2:51 Using config and fixing recursion
  4. 4:46 Merging multiple modules and overrides
  5. 7:18 Type errors, typos, and imports
  6. 9:35 Inspecting option definitions and locations
Transcript

Introduction to writing NixOS modules

0:00 Okay, so the slides that we had a look at in the last video where we showed the general structure of a module, that will always be true forever, but it was a little bit theoretical. So let's write our own modules. I just started an empty folder and we just put NixOS aside and then we have a pure look at just the NixOS modules library. So let's have a look. We say, let's call that module ABC because we'll give it variables ABC and then this is an empty module now and we can now run the REPL on nixpkgs.

0:43 And now we can say packages.lib.evalModules and then we can say modules equals ./abc.nix, like this.

Defining options and types

0:57 And now what we get is an output object, let's make the font size a bit smaller, and that contains all kinds of things that we didn't really ask for. So we can say X equals and then X dot and then inspect that. X dot config now contains nothing and we can basically start now adding option types here. So we can say, okay, we will need the lib object anyway.

The full transcript comes with the lesson.

About this lesson

You don't need a full NixOS system to learn how modules work. In this lesson we evaluate modules on their own with lib.evalModules in the Nix REPL, declare options from scratch, and watch the module system check, merge, and trace them.

In this video

  • Evaluating a module outside of NixOS, and what the result contains
  • Shopping for option types, from positive numbers to ports
  • Options that refer to each other through config, and the fixed point behind it
  • The type errors and typo suggestions you get for free
  • Finding out which file set a value

After this lesson

You have a small playground to test your understanding of the module system, and you can predict how options behave before you build a whole system.