NixOS modules: Difference between revisions
imported>Mth m Mth moved page NixOS Modules to Module |
imported>Mth No edit summary |
||
Line 1: | Line 1: | ||
'''Modules''' are files combined by NixOS to produce the full system configuration. A module contains a Nix expression. It ''declares'' options for other modules to ''define'' (give a value). It processes them and defines options declared in other modules.<ref>{{manual:nixos|sec=#sec-writing-modules|chapter=Chapter 42. Writing NixOS Modules}}</ref> | |||
For example, {{ic|/etc/nixos/configuration.nix}} is a module. Most other modules are in {{Nixpkgs Link|nixos/modules}}. | |||
== | == Structure == | ||
Modules have the following syntax: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
imports = [ | imports = [ | ||
# | # paths to other modules | ||
]; | ]; | ||
options = { | options = { | ||
# | # option declarations | ||
}; | }; | ||
config = { | config = { | ||
# | # option definitions | ||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
There is a shorthand for modules without any declarations: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { | ||
require = [ | require = [ | ||
# | # paths to other modules | ||
]; | ]; | ||
# | # option definitions | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Function === | |||
A module can be turned into a function accepting an attribute set. | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{config, pkgs, ...}: | { config, pkgs, ... }: | ||
{ | { | ||
# ... | # ... | ||
Line 54: | Line 46: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
It may require the attribute set to contain: | |||
<dl> | |||
<dt>[[NixOS:config_argument|<code>config</code>]]</dt> | |||
<dd>The configuration of the entire system.</dd> | |||
<dt><code>options</code></dt> | |||
<dd>All option declarations refined with all definition and declaration references.</dd> | |||
<dt><code>pkgs</code></dt> | |||
<dd>The attribute set extracted from the Nix package collection and enhanced with the <code>nixpkgs.config</code> option.</dd> | |||
<dt><code>modulesPath</code></dt> | |||
<dd>The location of the <code>module</code> directory of NixOS.</dd> | |||
</dl> | |||
=== Imports === | |||
Imports are paths to other NixOS modules that should be included in the evaluation of the system configuration. A default set of modules is defined in {{Nixpkgs Link|nixos/modules/module-list.nix}}. These don't need to be added in the import list. | |||
{ | |||
=== Declarations === | |||
Declarations specify a module's external interfaces. | |||
{ | <syntaxhighlight lang=nix> | ||
optionName = mkOption { | |||
# ... | # ... | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Example | They are created with {{ic|mkOption}}, a function accepting a set with following attributes:<ref>{{Nixpkgs Link|lib/options.nix#L21-L54}}</ref><ref>{{manual:nixos|sec=#sec-option-declarations|chapter=42.1. Option Declarations}}</ref> | ||
<dl> | |||
<dt><code>type</code></dt> | |||
<dd><p> | |||
The type of the option. It may | |||
be omitted, but that’s not advisable since it may lead to errors that | |||
are hard to diagnose. | |||
</p></dd> | |||
<dt><code>default</code></dt> | |||
<dd><p> | |||
The default value used if no value is defined by any module. A default is | |||
not required; but if a default is not given, then users of the module | |||
will have to define the value of the option, otherwise an error will be | |||
thrown. | |||
</p></dd> | |||
<dt><code class="varname">example</code></dt> | |||
<dd><p> | |||
An example value that will be shown in the NixOS manual. | |||
</p></dd> | |||
<dt><code>description</code></dt> | |||
<dd><p> | |||
A textual description of the option, in DocBook format, that will be | |||
included in the NixOS manual. | |||
</p></dd> | |||
</dl> | |||
== Rationale == | |||
Modules were introduced to allow extending NixOS without modifying its source code.<ref>[https://nixos.org/nix-dev/2008-November/001467.html <nowiki>[Nix-dev] NixOS: New scheme</nowiki>]</ref> They also allow splitting up <code>configuration.nix</code>, making the system configuration easier to maintain and to reuse. | |||
== Example == | |||
put into <code>hello.nix</code> in the same folder as your <code>configuration.nix</code>. | put into <code>hello.nix</code> in the same folder as your <code>configuration.nix</code>. | ||
Line 181: | Line 209: | ||
== References == | == References == | ||
<references /> | |||
== | == See also == | ||
* Debian [http://wiki.debian.org/PackageConfigUpgrade Config::Model]: | * [[NixOS:extend_NixOS]] | ||
target configuration upgrades by abstracting the option of the configuration. Each file is a tree structure where leaves are values defined with an interpreted type. The interpreters are defined for each meta-configuration files name *.conf. Configuration files does not seems to interact with each other to make consistent configuration. They provide an UI for editing their configuration file. | * [[NixOS:Properties]] | ||
* [https://discourse.nixos.org/t/best-resources-for-learning-about-the-nixos-module-system NixOS discourse, "Best resources for learning about the NixOS module system?"] | |||
* Debian [http://wiki.debian.org/PackageConfigUpgrade Config::Model]: target configuration upgrades by abstracting the option of the configuration. Each file is a tree structure where leaves are values defined with an interpreted type. The interpreters are defined for each meta-configuration files name *.conf. Configuration files does not seems to interact with each other to make consistent configuration. They provide an UI for editing their configuration file. | |||
[[Category:Configuration]] | [[Category:Configuration]] | ||
[[Category:Reference]] | [[Category:Reference]] | ||
[[Category:NixOS]] | [[Category:NixOS]] |