2014-08-24 18:18:18 +01:00
|
|
|
|
<section xmlns="http://docbook.org/ns/docbook"
|
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
|
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
|
|
|
version="5.0"
|
|
|
|
|
xml:id="sec-configuration-file">
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<title>NixOS Configuration File</title>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
|
|
|
|
The NixOS configuration file generally looks like this:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
{ <replaceable>option definitions</replaceable>
|
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
The first line (<literal>{ config, pkgs, ... }:</literal>) denotes that this
|
|
|
|
|
is actually a function that takes at least the two arguments
|
|
|
|
|
<varname>config</varname> and <varname>pkgs</varname>. (These are explained
|
|
|
|
|
later.) The function returns a <emphasis>set</emphasis> of option definitions
|
|
|
|
|
(<literal>{ <replaceable>...</replaceable> }</literal>). These definitions
|
|
|
|
|
have the form <literal><replaceable>name</replaceable> =
|
|
|
|
|
<replaceable>value</replaceable></literal>, where
|
|
|
|
|
<replaceable>name</replaceable> is the name of an option and
|
|
|
|
|
<replaceable>value</replaceable> is its value. For example,
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
2018-04-05 09:43:56 +01:00
|
|
|
|
{ <xref linkend="opt-services.httpd.enable"/> = true;
|
2018-04-17 00:19:55 +01:00
|
|
|
|
<xref linkend="opt-services.httpd.adminAddr"/> = "alice@example.org";
|
2019-11-04 21:24:55 +00:00
|
|
|
|
<link linkend="opt-services.httpd.virtualHosts">services.httpd.virtualHosts.localhost.documentRoot</link> = "/webroot";
|
2014-08-24 18:18:18 +01:00
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
defines a configuration with three option definitions that together enable
|
|
|
|
|
the Apache HTTP Server with <filename>/webroot</filename> as the document
|
|
|
|
|
root.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
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
|
|
|
|
|
<varname>services</varname> that contains a set named
|
|
|
|
|
<varname>httpd</varname>, which in turn contains an option definition named
|
|
|
|
|
<varname>enable</varname> with value <literal>true</literal>. This means that
|
|
|
|
|
the example above can also be written as:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
{ services = {
|
|
|
|
|
httpd = {
|
|
|
|
|
enable = true;
|
|
|
|
|
adminAddr = "alice@example.org";
|
2019-11-04 21:24:55 +00:00
|
|
|
|
virtualHosts = {
|
|
|
|
|
localhost = {
|
|
|
|
|
documentRoot = "/webroot";
|
|
|
|
|
};
|
|
|
|
|
};
|
2014-08-24 18:18:18 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
which may be more convenient if you have lots of option definitions that
|
|
|
|
|
share the same prefix (such as <literal>services.httpd</literal>).
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
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>),
|
|
|
|
|
<command>nixos-rebuild</command> will give an error like:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<screen>
|
2015-03-07 13:43:23 +00:00
|
|
|
|
The option `services.httpd.enable' defined in `/etc/nixos/configuration.nix' does not exist.
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</screen>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Likewise, values in option definitions must have a correct type. For
|
|
|
|
|
instance, <option>services.httpd.enable</option> 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:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<screen>
|
|
|
|
|
The option value `services.httpd.enable' in `/etc/nixos/configuration.nix' is not a boolean.
|
|
|
|
|
</screen>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
|
|
|
|
Options have various types of values. The most important are:
|
|
|
|
|
<variablelist>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Strings
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
|
|
|
|
Strings are enclosed in double quotes, e.g.
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-networking.hostName"/> = "dexter";
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Special characters can be escaped by prefixing them with a backslash
|
|
|
|
|
(e.g. <literal>\"</literal>).
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Multi-line strings can be enclosed in <emphasis>double single
|
|
|
|
|
quotes</emphasis>, e.g.
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-networking.extraHosts"/> =
|
2014-08-24 18:18:18 +01:00
|
|
|
|
''
|
|
|
|
|
127.0.0.2 other-localhost
|
|
|
|
|
10.0.0.1 server
|
|
|
|
|
'';
|
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
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>"</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
|
2018-05-02 00:57:09 +01:00
|
|
|
|
xlink:href="https://nixos.org/nix/manual/#ssec-values">here</link>.
|
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Booleans
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
|
|
|
|
These can be <literal>true</literal> or <literal>false</literal>, e.g.
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-networking.firewall.enable"/> = true;
|
|
|
|
|
<xref linkend="opt-networking.firewall.allowPing"/> = false;
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Integers
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
|
|
|
|
For example,
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-boot.kernel.sysctl"/>."net.ipv4.tcp_keepalive_time" = 60;
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
(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.)
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Sets
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Sets were introduced above. They are name/value pairs enclosed in braces,
|
|
|
|
|
as in the option definition
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-fileSystems"/>."/boot" =
|
2014-08-24 18:18:18 +01:00
|
|
|
|
{ device = "/dev/sda1";
|
|
|
|
|
fsType = "ext4";
|
2015-10-21 18:37:14 +01:00
|
|
|
|
options = [ "rw" "data=ordered" "relatime" ];
|
2014-08-24 18:18:18 +01:00
|
|
|
|
};
|
|
|
|
|
</programlisting>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Lists
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
The important thing to note about lists is that list elements are
|
|
|
|
|
separated by whitespace, like this:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-boot.kernelModules"/> = [ "fuse" "kvm-intel" "coretemp" ];
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
|
|
|
|
List elements can be any other type, e.g. sets:
|
|
|
|
|
<programlisting>
|
|
|
|
|
swapDevices = [ { device = "/dev/disk/by-label/swap"; } ];
|
|
|
|
|
</programlisting>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
<varlistentry>
|
2018-06-01 02:03:51 +01:00
|
|
|
|
<term>
|
|
|
|
|
Packages
|
|
|
|
|
</term>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
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 <varname>pkgs</varname>. Typical uses:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-environment.systemPackages"/> =
|
2014-08-24 18:18:18 +01:00
|
|
|
|
[ pkgs.thunderbird
|
|
|
|
|
pkgs.emacs
|
|
|
|
|
];
|
|
|
|
|
|
2018-10-23 17:22:14 +01:00
|
|
|
|
<xref linkend="opt-services.postgresql.package"/> = pkgs.postgresql_10;
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
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"/>.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</listitem>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</varlistentry>
|
|
|
|
|
</variablelist>
|
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</section>
|