summary refs log tree commit diff
path: root/nixos/doc/manual/from_md/configuration/config-file.section.xml
blob: 952c6e60030211567ab1bc37399cd963dda0c287 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-configuration-file">
  <title>NixOS Configuration File</title>
  <para>
    The NixOS configuration file generally looks like this:
  </para>
  <programlisting language="bash">
{ config, pkgs, ... }:

{ option definitions
}
</programlisting>
  <para>
    The first line (<literal>{ config, pkgs, ... }:</literal>) denotes
    that this is actually a function that takes at least the two
    arguments <literal>config</literal> and <literal>pkgs</literal>.
    (These are explained later, in chapter
    <xref linkend="sec-writing-modules" />) The function returns a
    <emphasis>set</emphasis> of option definitions
    (<literal>{ ... }</literal>). These definitions have the form
    <literal>name = value</literal>, where <literal>name</literal> is
    the name of an option and <literal>value</literal> is its value. For
    example,
  </para>
  <programlisting language="bash">
{ config, pkgs, ... }:

{ services.httpd.enable = true;
  services.httpd.adminAddr = &quot;alice@example.org&quot;;
  services.httpd.virtualHosts.localhost.documentRoot = &quot;/webroot&quot;;
}
</programlisting>
  <para>
    defines a configuration with three option definitions that together
    enable the Apache HTTP Server with <literal>/webroot</literal> as
    the document root.
  </para>
  <para>
    Sets can be nested, and in fact dots in option names are shorthand
    for defining a set containing another set. For instance,
    <xref linkend="opt-services.httpd.enable" /> defines a set named
    <literal>services</literal> that contains a set named
    <literal>httpd</literal>, which in turn contains an option
    definition named <literal>enable</literal> with value
    <literal>true</literal>. This means that the example above can also
    be written as:
  </para>
  <programlisting language="bash">
{ config, pkgs, ... }:

{ services = {
    httpd = {
      enable = true;
      adminAddr = &quot;alice@example.org&quot;;
      virtualHosts = {
        localhost = {
          documentRoot = &quot;/webroot&quot;;
        };
      };
    };
  };
}
</programlisting>
  <para>
    which may be more convenient if you have lots of option definitions
    that share the same prefix (such as
    <literal>services.httpd</literal>).
  </para>
  <para>
    NixOS checks your option definitions for correctness. For instance,
    if you try to define an option that doesn’t exist (that is, doesn’t
    have a corresponding <emphasis>option declaration</emphasis>),
    <literal>nixos-rebuild</literal> will give an error like:
  </para>
  <programlisting>
The option `services.httpd.enable' defined in `/etc/nixos/configuration.nix' does not exist.
</programlisting>
  <para>
    Likewise, values in option definitions must have a correct type. For
    instance, <literal>services.httpd.enable</literal> must be a Boolean
    (<literal>true</literal> or <literal>false</literal>). Trying to
    give it a value of another type, such as a string, will cause an
    error:
  </para>
  <programlisting>
The option value `services.httpd.enable' in `/etc/nixos/configuration.nix' is not a boolean.
</programlisting>
  <para>
    Options have various types of values. The most important are:
  </para>
  <variablelist>
    <varlistentry>
      <term>
        Strings
      </term>
      <listitem>
        <para>
          Strings are enclosed in double quotes, e.g.
        </para>
        <programlisting language="bash">
networking.hostName = &quot;dexter&quot;;
</programlisting>
        <para>
          Special characters can be escaped by prefixing them with a
          backslash (e.g. <literal>\&quot;</literal>).
        </para>
        <para>
          Multi-line strings can be enclosed in <emphasis>double single
          quotes</emphasis>, e.g.
        </para>
        <programlisting language="bash">
networking.extraHosts =
  ''
    127.0.0.2 other-localhost
    10.0.0.1 server
  '';
</programlisting>
        <para>
          The main difference is that it strips from each line a number
          of spaces equal to the minimal indentation of the string as a
          whole (disregarding the indentation of empty lines), and that
          characters like <literal>&quot;</literal> and
          <literal>\</literal> are not special (making it more
          convenient for including things like shell code). See more
          info about this in the Nix manual
          <link xlink:href="https://nixos.org/nix/manual/#ssec-values">here</link>.
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>
        Booleans
      </term>
      <listitem>
        <para>
          These can be <literal>true</literal> or
          <literal>false</literal>, e.g.
        </para>
        <programlisting language="bash">
networking.firewall.enable = true;
networking.firewall.allowPing = false;
</programlisting>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>
        Integers
      </term>
      <listitem>
        <para>
          For example,
        </para>
        <programlisting language="bash">
boot.kernel.sysctl.&quot;net.ipv4.tcp_keepalive_time&quot; = 60;
</programlisting>
        <para>
          (Note that here the attribute name
          <literal>net.ipv4.tcp_keepalive_time</literal> is enclosed in
          quotes to prevent it from being interpreted as a set named
          <literal>net</literal> containing a set named
          <literal>ipv4</literal>, and so on. This is because it’s not a
          NixOS option but the literal name of a Linux kernel setting.)
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>
        Sets
      </term>
      <listitem>
        <para>
          Sets were introduced above. They are name/value pairs enclosed
          in braces, as in the option definition
        </para>
        <programlisting language="bash">
fileSystems.&quot;/boot&quot; =
  { device = &quot;/dev/sda1&quot;;
    fsType = &quot;ext4&quot;;
    options = [ &quot;rw&quot; &quot;data=ordered&quot; &quot;relatime&quot; ];
  };
</programlisting>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>
        Lists
      </term>
      <listitem>
        <para>
          The important thing to note about lists is that list elements
          are separated by whitespace, like this:
        </para>
        <programlisting language="bash">
boot.kernelModules = [ &quot;fuse&quot; &quot;kvm-intel&quot; &quot;coretemp&quot; ];
</programlisting>
        <para>
          List elements can be any other type, e.g. sets:
        </para>
        <programlisting language="bash">
swapDevices = [ { device = &quot;/dev/disk/by-label/swap&quot;; } ];
</programlisting>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>
        Packages
      </term>
      <listitem>
        <para>
          Usually, the packages you need are already part of the Nix
          Packages collection, which is a set that can be accessed
          through the function argument <literal>pkgs</literal>. Typical
          uses:
        </para>
        <programlisting language="bash">
environment.systemPackages =
  [ pkgs.thunderbird
    pkgs.emacs
  ];

services.postgresql.package = pkgs.postgresql_10;
</programlisting>
        <para>
          The latter option definition changes the default PostgreSQL
          package used by NixOS’s PostgreSQL service to 10.x. For more
          information on packages, including how to add new ones, see
          <xref linkend="sec-custom-packages" />.
        </para>
      </listitem>
    </varlistentry>
  </variablelist>
</section>