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

2.4 How NixOS merges options by type

Lesson 11 of 28 · 6:46

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Merging module options by type
  2. 1:35 List merging order and sorting
  3. 3:25 Type combinators and other option types
Transcript

Merging module options by type

0:00 So we've just been playing around with the NixOS module library and have started to understand how to define multiple options, how they can refer to each other and stuff like that. And one very interesting and also actually fascinating feature of the module library is to be able to merge by type. So I fabricated an example here where we basically have an attribute path that's like ABC, something example-like.

0:31 And the type here is packages.lib.types.listOf, and that is a function that takes another parameter. In this case, that would be a number. And we have a module A and a module B that basically set the same attribute here. And one sets it to a list with one, one sets it to a list with two. And then we have a top-level module that basically imports both of them and then also sets this here. And the question is, when we now evaluate this whole module system, what will the parameter or the attribute path A.B.C contain?

1:08 And before I unfold the slide where we obviously show the result here, think about it for a moment. Will it be overwritten? Will it be an error? Will it be merged? And if it's merged, then how? Any idea? So, of course, it will be merged. We'll get one, two, three. But don't assume that it will be particularly sorted.

The full transcript comes with the lesson.

About this lesson

Two modules set the same option. Is that an error, does one of them win, or are the values merged? This lesson answers the question with a small quiz and a hands-on example, and shows how option types decide what happens.

In this video

  • A quiz about lists set in several modules, and the detail about their order you should not rely on
  • Type combinators like listOf, nullOr, and enum, and how they nest
  • Looking up any type and its description in the REPL

After this lesson

You know why NixOS can combine configuration from many files, and how to pick types that merge the way your users expect.