···6677Compared to plain Nix, it adds documentation, type checking and composition or extensibility.
8899-NOTE: This chapter is new and not complete yet. For a gentle introduction to the module system, in the context of NixOS, see [Writing NixOS Modules](https://nixos.org/manual/nixos/unstable/index.html#sec-writing-modules) in the NixOS manual.
99+::: {.note}
1010+This chapter is new and not complete yet. For a gentle introduction to the module system, in the context of NixOS, see [Writing NixOS Modules](https://nixos.org/manual/nixos/unstable/index.html#sec-writing-modules) in the NixOS manual.
1111+:::
1212+10131114## `lib.evalModules` {#module-system-lib-evalModules}
12151313-Evaluate a set of modules. The result is a set with the attributes:
1616+Evaluate a set of modules. This function is typically only used once per application (e.g. once in NixOS, once in Home Manager, ...).
14171518### Parameters {#module-system-lib-evalModules-parameters}
16191720#### `modules` {#module-system-lib-evalModules-param-modules}
18211919-A list of modules. These are merged together using various methods to form the final configuration.
2222+A list of modules. These are merged together to form the final configuration.
2323+<!-- TODO link to section about merging, TBD -->
20242125#### `specialArgs` {#module-system-lib-evalModules-param-specialArgs}
22262327An attribute set of module arguments that can be used in `imports`.
24282525-This is in contrast to `config._module.args`, which is only available within the module fixpoint, which does not exist before all imports are resolved.
2929+This is in contrast to `config._module.args`, which is only available after all `imports` have been resolved.
26302731#### `specialArgs.class` {#module-system-lib-evalModules-param-specialArgs-class}
28322929-If the `class` attribute is set in `specialArgs`, the module system will rejected modules with a different `class`.
3333+If the `class` attribute is set in `specialArgs`, the module system will reject modules with a different `class`.
30343131-This improves the error message that users will encounter when they import an incompatible module that was designed for a different class of configurations.
3535+The `class` value should be in lower [camel case](https://en.wikipedia.org/wiki/Camel_case).
32363333-The `class` value should be in camelcase, and, if applicable, it should match the prefix of the attributes used in (experimental) flakes. Some examples are:
3737+If applicable, the `class` should match the "prefix" of the attributes used in (experimental) [flakes](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#description). Some examples are:
34383535- - `nixos`: NixOS modules
3939+ - `nixos` as in `flake.nixosModules`
3640 - `nixosTest`: modules that constitute a [NixOS VM test](https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests)
4141+<!-- We've only just started with `class`. You're invited to add a few more. -->
37423843#### `prefix` {#module-system-lib-evalModules-param-prefix}
39444045A list of strings representing the location at or below which all options are evaluated. This is used by `types.submodule` to improve error reporting and find the implicit `name` module argument.
41464247### Return value {#module-system-lib-evalModules-return-value}
4848+4949+The result is an attribute set with the following attributes:
43504451#### `options` {#module-system-lib-evalModules-return-value-options}
45524646-The nested set of all option declarations.
5353+The nested attribute set of all option declarations.
47544855#### `config` {#module-system-lib-evalModules-return-value-config}
49565050-The nested set of all option values.
5757+The nested attribute set of all option values.
51585259#### `type` {#module-system-lib-evalModules-return-value-type}
53605454-A module system type representing the module set as a submodule, to be extended by configuration from the containing module set.
6161+A module system type. This type is an instance of `types.submoduleWith` containing the current [`modules`](#module-system-lib-evalModules-param-modules).
55625656-This is also available as the module argument `moduleType`.
6363+The option definitions that are typed with this type will extend the current set of modules, like [`extendModules`](#module-system-lib-evalModules-return-value-extendModules).
6464+6565+However, the value returned from the type is just the [`config`](#module-system-lib-evalModules-return-value-config), like any submodule.
6666+6767+This type is also available to the [`modules`](#module-system-lib-evalModules-param-modules) as the module argument `moduleType`.
57685869#### `extendModules` {#module-system-lib-evalModules-return-value-extendModules}
59706060-A function similar to `evalModules` but building on top of the module set. Its arguments, `modules` and `specialArgs` are added to the existing values.
7171+A function similar to `evalModules` but building on top of the already passed [`modules`](#module-system-lib-evalModules-param-modules). Its arguments, `modules` and `specialArgs` are added to the existing values.
61726262-Using `extendModules` a few times has no performance impact as long as you only reference the final `options` and `config`.
6363-If you do reference multiple `config` (or `options`) from before and after `extendModules`, performance is the same as with multiple `evalModules` invocations, because the new modules' ability to override existing configuration fundamentally requires a new fixpoint to be constructed.
7373+The real work of module evaluation happens while computing the values in `config` and `options`, so multiple invocations of `extendModules` have a particularly small cost, as long as only the final `config` and `options` are evaluated.
7474+7575+If you do reference multiple `config` (or `options`) from before and after `extendModules`, evaluation performance is the same as with multiple `evalModules` invocations, because the new modules' ability to override existing configuration fundamentally requires constructing a new `config` and `options` fixpoint.
64766565-This is also available as a module argument.
7777+This functionality is also available to modules as the `extendModules` module argument.
66786779#### `_module` {#module-system-lib-evalModules-return-value-_module}
68806981A portion of the configuration tree which is elided from `config`. It contains some values that are mostly internal to the module system implementation.
8282+8383+<!-- TODO: when markdown migration is complete, make _module docs visible again and reference _module docs. Maybe move those docs into this chapter? -->