···
1
-
<chapter xmlns="http://docbook.org/ns/docbook"
2
-
xmlns:xlink="http://www.w3.org/1999/xlink"
3
-
xmlns:xi="http://www.w3.org/2001/XInclude"
5
-
xml:id="sec-writing-modules">
6
-
<title>Writing NixOS Modules</title>
8
-
NixOS has a modular system for declarative configuration. This system
9
-
combines multiple <emphasis>modules</emphasis> to produce the full system
10
-
configuration. One of the modules that constitute the configuration is
11
-
<filename>/etc/nixos/configuration.nix</filename>. Most of the others live in
14
-
xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/modules"><filename>nixos/modules</filename></link>
15
-
subdirectory of the Nixpkgs tree.
18
-
Each NixOS module is a file that handles one logical aspect of the
19
-
configuration, such as a specific kind of hardware, a service, or network
20
-
settings. A module configuration does not have to handle everything from
21
-
scratch; it can use the functionality provided by other modules for its
22
-
implementation. Thus a module can <emphasis>declare</emphasis> options that
23
-
can be used by other modules, and conversely can <emphasis>define</emphasis>
24
-
options provided by other modules in its own implementation. For example, the
27
-
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/security/pam.nix"><filename>pam.nix</filename></link>
28
-
declares the option <option>security.pam.services</option> that allows other
31
-
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/networking/ssh/sshd.nix"><filename>sshd.nix</filename></link>)
32
-
to define PAM services; and it defines the option
33
-
<option>environment.etc</option> (declared by
35
-
xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/etc/etc.nix"><filename>etc.nix</filename></link>)
36
-
to cause files to be created in <filename>/etc/pam.d</filename>.
38
-
<para xml:id="para-module-syn">
40
-
linkend="sec-configuration-syntax"/>, we saw the following structure
43
-
{ config, pkgs, ... }:
45
-
{ <replaceable>option definitions</replaceable>
48
-
This is actually an <emphasis>abbreviated</emphasis> form of module that only
49
-
defines options, but does not declare any. The structure of full NixOS
50
-
modules is shown in <xref linkend='ex-module-syntax' />.
52
-
<example xml:id='ex-module-syntax'>
53
-
<title>Structure of NixOS Modules</title>
55
-
{ config, pkgs, ... }: <co xml:id='module-syntax-1' />
59
-
[ <replaceable>paths of other modules</replaceable> <co xml:id='module-syntax-2' />
63
-
<replaceable>option declarations</replaceable> <co xml:id='module-syntax-3' />
67
-
<replaceable>option definitions</replaceable> <co xml:id='module-syntax-4' />
72
-
The meaning of each part is as follows.
74
-
<callout arearefs='module-syntax-1'>
76
-
This line makes the current Nix expression a function. The variable
77
-
<varname>pkgs</varname> contains Nixpkgs (by default, it takes the
78
-
<varname>nixpkgs</varname> entry of <envar>NIX_PATH</envar>, see the <link
79
-
xlink:href="https://nixos.org/manual/nix/stable/#sec-common-env">Nix
80
-
manual</link> for further details), while <varname>config</varname>
81
-
contains the full system configuration. This line can be omitted if there
82
-
is no reference to <varname>pkgs</varname> and <varname>config</varname>
86
-
<callout arearefs='module-syntax-2'>
88
-
This list enumerates the paths to other NixOS modules that should be
89
-
included in the evaluation of the system configuration. A default set of
90
-
modules is defined in the file
91
-
<filename>modules/module-list.nix</filename>. These don't need to be added
95
-
<callout arearefs='module-syntax-3'>
97
-
The attribute <varname>options</varname> is a nested set of
98
-
<emphasis>option declarations</emphasis> (described below).
101
-
<callout arearefs='module-syntax-4'>
103
-
The attribute <varname>config</varname> is a nested set of
104
-
<emphasis>option definitions</emphasis> (also described below).
110
-
<xref linkend='locate-example' /> shows a module that handles the regular
111
-
update of the “locate” database, an index of all files in the file
112
-
system. This module declares two options that can be defined by other modules
113
-
(typically the user’s <filename>configuration.nix</filename>):
114
-
<option>services.locate.enable</option> (whether the database should be
115
-
updated) and <option>services.locate.interval</option> (when the update
116
-
should be done). It implements its functionality by defining two options
117
-
declared by other modules: <option>systemd.services</option> (the set of all
118
-
systemd services) and <option>systemd.timers</option> (the list of commands
119
-
to be executed periodically by <command>systemd</command>).
121
-
<example xml:id='locate-example'>
122
-
<title>NixOS Module for the “locate” Service</title>
124
-
{ config, lib, pkgs, ... }:
129
-
cfg = config.services.locate;
131
-
options.services.locate = {
132
-
enable = mkOption {
136
-
If enabled, NixOS will periodically update the database of
137
-
files used by the <command>locate</command> command.
141
-
interval = mkOption {
144
-
example = "hourly";
146
-
Update the locate database at this interval. Updates by
147
-
default at 2:15 AM every day.
149
-
The format is described in
150
-
<citerefentry><refentrytitle>systemd.time</refentrytitle>
151
-
<manvolnum>7</manvolnum></citerefentry>.
155
-
# Other options omitted for documentation
159
-
systemd.services.update-locatedb =
160
-
{ description = "Update Locate Database";
161
-
path = [ pkgs.su ];
164
-
mkdir -m 0755 -p $(dirname ${toString cfg.output})
166
-
--localuser=${cfg.localuser} \
167
-
${optionalString (!cfg.includeStore) "--prunepaths='/nix/store'"} \
168
-
--output=${toString cfg.output} ${concatStringsSep " " cfg.extraFlags}
172
-
systemd.timers.update-locatedb = mkIf cfg.enable
173
-
{ description = "Update timer for locate database";
174
-
partOf = [ "update-locatedb.service" ];
175
-
wantedBy = [ "timers.target" ];
176
-
timerConfig.OnCalendar = cfg.interval;
182
-
<xi:include href="../from_md/development/option-declarations.section.xml" />
183
-
<xi:include href="../from_md/development/option-types.section.xml" />
184
-
<xi:include href="../from_md/development/option-def.section.xml" />
185
-
<xi:include href="../from_md/development/assertions.section.xml" />
186
-
<xi:include href="../from_md/development/meta-attributes.section.xml" />
187
-
<xi:include href="../from_md/development/importing-modules.section.xml" />
188
-
<xi:include href="../from_md/development/replace-modules.section.xml" />
189
-
<xi:include href="../from_md/development/freeform-modules.section.xml" />
190
-
<xi:include href="../from_md/development/settings-options.section.xml" />