···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-boot-problems">
66+77+<title>Boot Problems</title>
88+99+<para>If NixOS fails to boot, there are a number of kernel command
1010+line parameters that may help you to identify or fix the issue. You
1111+can add these parameters in the GRUB boot menu by pressing “e” to
1212+modify the selected boot entry and editing the line starting with
1313+<literal>linux</literal>. The following are some useful kernel command
1414+line parameters that are recognised by the NixOS boot scripts or by
1515+systemd:
1616+1717+<variablelist>
1818+1919+ <varlistentry><term><literal>boot.shell_on_fail</literal></term>
2020+ <listitem><para>Start a root shell if something goes wrong in
2121+ stage 1 of the boot process (the initial ramdisk). This is
2222+ disabled by default because there is no authentication for the
2323+ root shell.</para></listitem>
2424+ </varlistentry>
2525+2626+ <varlistentry><term><literal>boot.debug1</literal></term>
2727+ <listitem><para>Start an interactive shell in stage 1 before
2828+ anything useful has been done. That is, no modules have been
2929+ loaded and no file systems have been mounted, except for
3030+ <filename>/proc</filename> and
3131+ <filename>/sys</filename>.</para></listitem>
3232+ </varlistentry>
3333+3434+ <varlistentry><term><literal>boot.trace</literal></term>
3535+ <listitem><para>Print every shell command executed by the stage 1
3636+ and 2 boot scripts.</para></listitem>
3737+ </varlistentry>
3838+3939+ <varlistentry><term><literal>single</literal></term>
4040+ <listitem><para>Boot into rescue mode (a.k.a. single user mode).
4141+ This will cause systemd to start nothing but the unit
4242+ <literal>rescue.target</literal>, which runs
4343+ <command>sulogin</command> to prompt for the root password and
4444+ start a root login shell. Exiting the shell causes the system to
4545+ continue with the normal boot process.</para></listitem>
4646+ </varlistentry>
4747+4848+ <varlistentry><term><literal>systemd.log_level=debug systemd.log_target=console</literal></term>
4949+ <listitem><para>Make systemd very verbose and send log messages to
5050+ the console instead of the journal.</para></listitem>
5151+ </varlistentry>
5252+5353+</variablelist>
5454+5555+For more parameters recognised by systemd, see
5656+<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.</para>
5757+5858+<para>If no login prompts or X11 login screens appear (e.g. due to
5959+hanging dependencies), you can press Alt+ArrowUp. If you’re lucky,
6060+this will start rescue mode (described above). (Also note that since
6161+most units have a 90-second timeout before systemd gives up on them,
6262+the <command>agetty</command> login prompts should appear eventually
6363+unless something is very wrong.)</para>
6464+6565+</section>
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-nix-gc">
66+77+<title>Cleaning the Nix Store</title>
88+99+<para>Nix has a purely functional model, meaning that packages are
1010+never upgraded in place. Instead new versions of packages end up in a
1111+different location in the Nix store (<filename>/nix/store</filename>).
1212+You should periodically run Nix’s <emphasis>garbage
1313+collector</emphasis> to remove old, unreferenced packages. This is
1414+easy:
1515+1616+<screen>
1717+$ nix-collect-garbage
1818+</screen>
1919+2020+Alternatively, you can use a systemd unit that does the same in the
2121+background:
2222+2323+<screen>
2424+$ systemctl start nix-gc.service
2525+</screen>
2626+2727+You can tell NixOS in <filename>configuration.nix</filename> to run
2828+this unit automatically at certain points in time, for instance, every
2929+night at 03:15:
3030+3131+<programlisting>
3232+nix.gc.automatic = true;
3333+nix.gc.dates = "03:15";
3434+</programlisting>
3535+3636+</para>
3737+3838+<para>The commands above do not remove garbage collector roots, such
3939+as old system configurations. Thus they do not remove the ability to
4040+roll back to previous configurations. The following command deletes
4141+old roots, removing the ability to roll back to them:
4242+<screen>
4343+$ nix-collect-garbage -d
4444+</screen>
4545+You can also do this for specific profiles, e.g.
4646+<screen>
4747+$ nix-env -p /nix/var/nix/profiles/per-user/eelco/profile --delete-generations old
4848+</screen>
4949+Note that NixOS system configurations are stored in the profile
5050+<filename>/nix/var/nix/profiles/system</filename>.</para>
5151+5252+<para>Another way to reclaim disk space (often as much as 40% of the
5353+size of the Nix store) is to run Nix’s store optimiser, which seeks
5454+out identical files in the store and replaces them with hard links to
5555+a single copy.
5656+<screen>
5757+$ nix-store --optimise
5858+</screen>
5959+Since this command needs to read the entire Nix store, it can take
6060+quite a while to finish.</para>
6161+6262+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-container-networking">
66+77+88+<title>Container Networking</title>
99+1010+<para>When you create a container using <literal>nixos-container
1111+create</literal>, it gets it own private IPv4 address in the range
1212+<literal>10.233.0.0/16</literal>. You can get the container’s IPv4
1313+address as follows:
1414+1515+<screen>
1616+$ nixos-container show-ip foo
1717+10.233.4.2
1818+1919+$ ping -c1 10.233.4.2
2020+64 bytes from 10.233.4.2: icmp_seq=1 ttl=64 time=0.106 ms
2121+</screen>
2222+2323+</para>
2424+2525+<para>Networking is implemented using a pair of virtual Ethernet
2626+devices. The network interface in the container is called
2727+<literal>eth0</literal>, while the matching interface in the host is
2828+called <literal>ve-<replaceable>container-name</replaceable></literal>
2929+(e.g., <literal>ve-foo</literal>). The container has its own network
3030+namespace and the <literal>CAP_NET_ADMIN</literal> capability, so it
3131+can perform arbitrary network configuration such as setting up
3232+firewall rules, without affecting or having access to the host’s
3333+network.</para>
3434+3535+<para>By default, containers cannot talk to the outside network. If
3636+you want that, you should set up Network Address Translation (NAT)
3737+rules on the host to rewrite container traffic to use your external
3838+IP address. This can be accomplished using the following configuration
3939+on the host:
4040+4141+<programlisting>
4242+networking.nat.enable = true;
4343+networking.nat.internalInterfaces = ["ve-+"];
4444+networking.nat.externalInterface = "eth0";
4545+</programlisting>
4646+where <literal>eth0</literal> should be replaced with the desired
4747+external interface. Note that <literal>ve-+</literal> is a wildcard
4848+that matches all container interfaces.</para>
4949+5050+</section>
+34
nixos/doc/manual/administration/containers.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-containers">
66+77+<title>Container Management</title>
88+99+<para>NixOS allows you to easily run other NixOS instances as
1010+<emphasis>containers</emphasis>. Containers are a light-weight
1111+approach to virtualisation that runs software in the container at the
1212+same speed as in the host system. NixOS containers share the Nix store
1313+of the host, making container creation very efficient.</para>
1414+1515+<warning><para>Currently, NixOS containers are not perfectly isolated
1616+from the host system. This means that a user with root access to the
1717+container can do things that affect the host. So you should not give
1818+container root access to untrusted users.</para></warning>
1919+2020+<para>NixOS containers can be created in two ways: imperatively, using
2121+the command <command>nixos-container</command>, and declaratively, by
2222+specifying them in your <filename>configuration.nix</filename>. The
2323+declarative approach implies that containers get upgraded along with
2424+your host system when you run <command>nixos-rebuild</command>, which
2525+is often not what you want. By contrast, in the imperative approach,
2626+containers are configured and updated independently from the host
2727+system.</para>
2828+2929+<xi:include href="imperative-containers.xml" />
3030+<xi:include href="declarative-containers.xml" />
3131+<xi:include href="container-networking.xml" />
3232+3333+</chapter>
3434+
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-cgroups">
66+77+<title>Control Groups</title>
88+99+<para>To keep track of the processes in a running system, systemd uses
1010+<emphasis>control groups</emphasis> (cgroups). A control group is a
1111+set of processes used to allocate resources such as CPU, memory or I/O
1212+bandwidth. There can be multiple control group hierarchies, allowing
1313+each kind of resource to be managed independently.</para>
1414+1515+<para>The command <command>systemd-cgls</command> lists all control
1616+groups in the <literal>systemd</literal> hierarchy, which is what
1717+systemd uses to keep track of the processes belonging to each service
1818+or user session:
1919+2020+<screen>
2121+$ systemd-cgls
2222+├─user
2323+│ └─eelco
2424+│ └─c1
2525+│ ├─ 2567 -:0
2626+│ ├─ 2682 kdeinit4: kdeinit4 Running...
2727+│ ├─ <replaceable>...</replaceable>
2828+│ └─10851 sh -c less -R
2929+└─system
3030+ ├─httpd.service
3131+ │ ├─2444 httpd -f /nix/store/3pyacby5cpr55a03qwbnndizpciwq161-httpd.conf -DNO_DETACH
3232+ │ └─<replaceable>...</replaceable>
3333+ ├─dhcpcd.service
3434+ │ └─2376 dhcpcd --config /nix/store/f8dif8dsi2yaa70n03xir8r653776ka6-dhcpcd.conf
3535+ └─ <replaceable>...</replaceable>
3636+</screen>
3737+3838+Similarly, <command>systemd-cgls cpu</command> shows the cgroups in
3939+the CPU hierarchy, which allows per-cgroup CPU scheduling priorities.
4040+By default, every systemd service gets its own CPU cgroup, while all
4141+user sessions are in the top-level CPU cgroup. This ensures, for
4242+instance, that a thousand run-away processes in the
4343+<literal>httpd.service</literal> cgroup cannot starve the CPU for one
4444+process in the <literal>postgresql.service</literal> cgroup. (By
4545+contrast, it they were in the same cgroup, then the PostgreSQL process
4646+would get 1/1001 of the cgroup’s CPU time.) You can limit a service’s
4747+CPU share in <filename>configuration.nix</filename>:
4848+4949+<programlisting>
5050+systemd.services.httpd.serviceConfig.CPUShares = 512;
5151+</programlisting>
5252+5353+By default, every cgroup has 1024 CPU shares, so this will halve the
5454+CPU allocation of the <literal>httpd.service</literal> cgroup.</para>
5555+5656+<para>There also is a <literal>memory</literal> hierarchy that
5757+controls memory allocation limits; by default, all processes are in
5858+the top-level cgroup, so any service or session can exhaust all
5959+available memory. Per-cgroup memory limits can be specified in
6060+<filename>configuration.nix</filename>; for instance, to limit
6161+<literal>httpd.service</literal> to 512 MiB of RAM (excluding swap)
6262+and 640 MiB of RAM (including swap):
6363+6464+<programlisting>
6565+systemd.services.httpd.serviceConfig.MemoryLimit = "512M";
6666+systemd.services.httpd.serviceConfig.ControlGroupAttribute = [ "memory.memsw.limit_in_bytes 640M" ];
6767+</programlisting>
6868+6969+</para>
7070+7171+<para>The command <command>systemd-cgtop</command> shows a
7272+continuously updated list of all cgroups with their CPU and memory
7373+usage.</para>
7474+7575+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-declarative-containers">
66+77+<title>Declarative Container Specification</title>
88+99+<para>You can also specify containers and their configuration in the
1010+host’s <filename>configuration.nix</filename>. For example, the
1111+following specifies that there shall be a container named
1212+<literal>database</literal> running PostgreSQL:
1313+1414+<programlisting>
1515+containers.database =
1616+ { config =
1717+ { config, pkgs, ... }:
1818+ { services.postgresql.enable = true;
1919+ services.postgresql.package = pkgs.postgresql92;
2020+ };
2121+ };
2222+</programlisting>
2323+2424+If you run <literal>nixos-rebuild switch</literal>, the container will
2525+be built and started. If the container was already running, it will be
2626+updated in place, without rebooting.</para>
2727+2828+<para>By default, declarative containers share the network namespace
2929+of the host, meaning that they can listen on (privileged)
3030+ports. However, they cannot change the network configuration. You can
3131+give a container its own network as follows:
3232+3333+<programlisting>
3434+containers.database =
3535+ { privateNetwork = true;
3636+ hostAddress = "192.168.100.10";
3737+ localAddress = "192.168.100.11";
3838+ };
3939+</programlisting>
4040+4141+This gives the container a private virtual Ethernet interface with IP
4242+address <literal>192.168.100.11</literal>, which is hooked up to a
4343+virtual Ethernet interface on the host with IP address
4444+<literal>192.168.100.10</literal>. (See the next section for details
4545+on container networking.)</para>
4646+4747+<para>To disable the container, just remove it from
4848+<filename>configuration.nix</filename> and run <literal>nixos-rebuild
4949+switch</literal>. Note that this will not delete the root directory of
5050+the container in <literal>/var/lib/containers</literal>.</para>
5151+5252+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-imperative-containers">
66+77+<title>Imperative Container Management</title>
88+99+<para>We’ll cover imperative container management using
1010+<command>nixos-container</command> first. You create a container with
1111+identifier <literal>foo</literal> as follows:
1212+1313+<screen>
1414+$ nixos-container create foo
1515+</screen>
1616+1717+This creates the container’s root directory in
1818+<filename>/var/lib/containers/foo</filename> and a small configuration
1919+file in <filename>/etc/containers/foo.conf</filename>. It also builds
2020+the container’s initial system configuration and stores it in
2121+<filename>/nix/var/nix/profiles/per-container/foo/system</filename>. You
2222+can modify the initial configuration of the container on the command
2323+line. For instance, to create a container that has
2424+<command>sshd</command> running, with the given public key for
2525+<literal>root</literal>:
2626+2727+<screen>
2828+$ nixos-container create foo --config 'services.openssh.enable = true; \
2929+ users.extraUsers.root.openssh.authorizedKeys.keys = ["ssh-dss AAAAB3N…"];'
3030+</screen>
3131+3232+</para>
3333+3434+<para>Creating a container does not start it. To start the container,
3535+run:
3636+3737+<screen>
3838+$ nixos-container start foo
3939+</screen>
4040+4141+This command will return as soon as the container has booted and has
4242+reached <literal>multi-user.target</literal>. On the host, the
4343+container runs within a systemd unit called
4444+<literal>container@<replaceable>container-name</replaceable>.service</literal>.
4545+Thus, if something went wrong, you can get status info using
4646+<command>systemctl</command>:
4747+4848+<screen>
4949+$ systemctl status container@foo
5050+</screen>
5151+5252+</para>
5353+5454+<para>If the container has started succesfully, you can log in as
5555+root using the <command>root-login</command> operation:
5656+5757+<screen>
5858+$ nixos-container root-login foo
5959+[root@foo:~]#
6060+</screen>
6161+6262+Note that only root on the host can do this (since there is no
6363+authentication). You can also get a regular login prompt using the
6464+<command>login</command> operation, which is available to all users on
6565+the host:
6666+6767+<screen>
6868+$ nixos-container login foo
6969+foo login: alice
7070+Password: ***
7171+</screen>
7272+7373+With <command>nixos-container run</command>, you can execute arbitrary
7474+commands in the container:
7575+7676+<screen>
7777+$ nixos-container run foo -- uname -a
7878+Linux foo 3.4.82 #1-NixOS SMP Thu Mar 20 14:44:05 UTC 2014 x86_64 GNU/Linux
7979+</screen>
8080+8181+</para>
8282+8383+<para>There are several ways to change the configuration of the
8484+container. First, on the host, you can edit
8585+<literal>/var/lib/container/<replaceable>name</replaceable>/etc/nixos/configuration.nix</literal>,
8686+and run
8787+8888+<screen>
8989+$ nixos-container update foo
9090+</screen>
9191+9292+This will build and activate the new configuration. You can also
9393+specify a new configuration on the command line:
9494+9595+<screen>
9696+$ nixos-container update foo --config 'services.httpd.enable = true; \
9797+ services.httpd.adminAddr = "foo@example.org";'
9898+9999+$ curl http://$(nixos-container show-ip foo)/
100100+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">…
101101+</screen>
102102+103103+However, note that this will overwrite the container’s
104104+<filename>/etc/nixos/configuration.nix</filename>.</para>
105105+106106+<para>Alternatively, you can change the configuration from within the
107107+container itself by running <command>nixos-rebuild switch</command>
108108+inside the container. Note that the container by default does not have
109109+a copy of the NixOS channel, so you should run <command>nix-channel
110110+--update</command> first.</para>
111111+112112+<para>Containers can be stopped and started using
113113+<literal>nixos-container stop</literal> and <literal>nixos-container
114114+start</literal>, respectively, or by using
115115+<command>systemctl</command> on the container’s service unit. To
116116+destroy a container, including its file system, do
117117+118118+<screen>
119119+$ nixos-container destroy foo
120120+</screen>
121121+122122+</para>
123123+124124+</section>
+52
nixos/doc/manual/administration/logging.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-logging">
66+77+<title>Logging</title>
88+99+<para>System-wide logging is provided by systemd’s
1010+<emphasis>journal</emphasis>, which subsumes traditional logging
1111+daemons such as syslogd and klogd. Log entries are kept in binary
1212+files in <filename>/var/log/journal/</filename>. The command
1313+<literal>journalctl</literal> allows you to see the contents of the
1414+journal. For example,
1515+1616+<screen>
1717+$ journalctl -b
1818+</screen>
1919+2020+shows all journal entries since the last reboot. (The output of
2121+<command>journalctl</command> is piped into <command>less</command> by
2222+default.) You can use various options and match operators to restrict
2323+output to messages of interest. For instance, to get all messages
2424+from PostgreSQL:
2525+2626+<screen>
2727+$ journalctl -u postgresql.service
2828+-- Logs begin at Mon, 2013-01-07 13:28:01 CET, end at Tue, 2013-01-08 01:09:57 CET. --
2929+...
3030+Jan 07 15:44:14 hagbard postgres[2681]: [2-1] LOG: database system is shut down
3131+-- Reboot --
3232+Jan 07 15:45:10 hagbard postgres[2532]: [1-1] LOG: database system was shut down at 2013-01-07 15:44:14 CET
3333+Jan 07 15:45:13 hagbard postgres[2500]: [1-1] LOG: database system is ready to accept connections
3434+</screen>
3535+3636+Or to get all messages since the last reboot that have at least a
3737+“critical” severity level:
3838+3939+<screen>
4040+$ journalctl -b -p crit
4141+Dec 17 21:08:06 mandark sudo[3673]: pam_unix(sudo:auth): auth could not identify password for [alice]
4242+Dec 29 01:30:22 mandark kernel[6131]: [1053513.909444] CPU6: Core temperature above threshold, cpu clock throttled (total events = 1)
4343+</screen>
4444+4545+</para>
4646+4747+<para>The system journal is readable by root and by users in the
4848+<literal>wheel</literal> and <literal>systemd-journal</literal>
4949+groups. All users have a private journal that can be read using
5050+<command>journalctl</command>.</para>
5151+5252+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-maintenance-mode">
66+77+<title>Maintenance Mode</title>
88+99+<para>You can enter rescue mode by running:
1010+1111+<screen>
1212+$ systemctl rescue</screen>
1313+1414+This will eventually give you a single-user root shell. Systemd will
1515+stop (almost) all system services. To get out of maintenance mode,
1616+just exit from the rescue shell.</para>
1717+1818+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-nix-network-issues">
66+77+<title>Network Problems</title>
88+99+<para>Nix uses a so-called <emphasis>binary cache</emphasis> to
1010+optimise building a package from source into downloading it as a
1111+pre-built binary. That is, whenever a command like
1212+<command>nixos-rebuild</command> needs a path in the Nix store, Nix
1313+will try to download that path from the Internet rather than build it
1414+from source. The default binary cache is
1515+<uri>http://cache.nixos.org/</uri>. If this cache is unreachable, Nix
1616+operations may take a long time due to HTTP connection timeouts. You
1717+can disable the use of the binary cache by adding <option>--option
1818+use-binary-caches false</option>, e.g.
1919+2020+<screen>
2121+$ nixos-rebuild switch --option use-binary-caches false
2222+</screen>
2323+2424+If you have an alternative binary cache at your disposal, you can use
2525+it instead:
2626+2727+<screen>
2828+$ nixos-rebuild switch --option binary-caches http://my-cache.example.org/
2929+</screen>
3030+3131+</para>
3232+3333+</section>
+44
nixos/doc/manual/administration/rebooting.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-rebooting">
66+77+<title>Rebooting and Shutting Down</title>
88+99+<para>The system can be shut down (and automatically powered off) by
1010+doing:
1111+1212+<screen>
1313+$ shutdown
1414+</screen>
1515+1616+This is equivalent to running <command>systemctl
1717+poweroff</command>.</para>
1818+1919+<para>To reboot the system, run
2020+2121+<screen>
2222+$ reboot
2323+</screen>
2424+2525+which is equivalent to <command>systemctl reboot</command>.
2626+Alternatively, you can quickly reboot the system using
2727+<literal>kexec</literal>, which bypasses the BIOS by directly loading
2828+the new kernel into memory:
2929+3030+<screen>
3131+$ systemctl kexec
3232+</screen>
3333+3434+</para>
3535+3636+<para>The machine can be suspended to RAM (if supported) using
3737+<command>systemctl suspend</command>, and suspended to disk using
3838+<command>systemctl hibernate</command>.</para>
3939+4040+<para>These commands can be run by any user who is logged in locally,
4141+i.e. on a virtual console or in X11; otherwise, the user is asked for
4242+authentication.</para>
4343+4444+</chapter>
+48
nixos/doc/manual/administration/rollback.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-rollback">
66+77+<title>Rolling Back Configuration Changes</title>
88+99+<para>After running <command>nixos-rebuild</command> to switch to a
1010+new configuration, you may find that the new configuration doesn’t
1111+work very well. In that case, there are several ways to return to a
1212+previous configuration.</para>
1313+1414+<para>First, the GRUB boot manager allows you to boot into any
1515+previous configuration that hasn’t been garbage-collected. These
1616+configurations can be found under the GRUB submenu “NixOS - All
1717+configurations”. This is especially useful if the new configuration
1818+fails to boot. After the system has booted, you can make the selected
1919+configuration the default for subsequent boots:
2020+2121+<screen>
2222+$ /run/current-system/bin/switch-to-configuration boot</screen>
2323+2424+</para>
2525+2626+<para>Second, you can switch to the previous configuration in a running
2727+system:
2828+2929+<screen>
3030+$ nixos-rebuild switch --rollback</screen>
3131+3232+This is equivalent to running:
3333+3434+<screen>
3535+$ /nix/var/nix/profiles/system-<replaceable>N</replaceable>-link/bin/switch-to-configuration switch</screen>
3636+3737+where <replaceable>N</replaceable> is the number of the NixOS system
3838+configuration. To get a list of the available configurations, do:
3939+4040+<screen>
4141+$ ls -l /nix/var/nix/profiles/system-*-link
4242+<replaceable>...</replaceable>
4343+lrwxrwxrwx 1 root root 78 Aug 12 13:54 /nix/var/nix/profiles/system-268-link -> /nix/store/202b...-nixos-13.07pre4932_5a676e4-4be1055
4444+</screen>
4545+4646+</para>
4747+4848+</section>
+24
nixos/doc/manual/administration/running.xml
···11+<part xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-running">
66+77+<title>Administration</title>
88+99+<partintro>
1010+<para>This chapter describes various aspects of managing a running
1111+NixOS system, such as how to use the <command>systemd</command>
1212+service manager.</para>
1313+</partintro>
1414+1515+<xi:include href="service-mgmt.xml" />
1616+<xi:include href="rebooting.xml" />
1717+<xi:include href="user-sessions.xml" />
1818+<xi:include href="control-groups.xml" />
1919+<xi:include href="logging.xml" />
2020+<xi:include href="cleaning-store.xml" />
2121+<xi:include href="containers.xml" />
2222+<xi:include href="troubleshooting.xml" />
2323+2424+</part>
+83
nixos/doc/manual/administration/service-mgmt.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-systemctl">
66+77+<title>Service Management</title>
88+99+<para>In NixOS, all system services are started and monitored using
1010+the systemd program. Systemd is the “init” process of the system
1111+(i.e. PID 1), the parent of all other processes. It manages a set of
1212+so-called “units”, which can be things like system services
1313+(programs), but also mount points, swap files, devices, targets
1414+(groups of units) and more. Units can have complex dependencies; for
1515+instance, one unit can require that another unit must be successfully
1616+started before the first unit can be started. When the system boots,
1717+it starts a unit named <literal>default.target</literal>; the
1818+dependencies of this unit cause all system services to be started,
1919+file systems to be mounted, swap files to be activated, and so
2020+on.</para>
2121+2222+<para>The command <command>systemctl</command> is the main way to
2323+interact with <command>systemd</command>. Without any arguments, it
2424+shows the status of active units:
2525+2626+<screen>
2727+$ systemctl
2828+-.mount loaded active mounted /
2929+swapfile.swap loaded active active /swapfile
3030+sshd.service loaded active running SSH Daemon
3131+graphical.target loaded active active Graphical Interface
3232+<replaceable>...</replaceable>
3333+</screen>
3434+3535+</para>
3636+3737+<para>You can ask for detailed status information about a unit, for
3838+instance, the PostgreSQL database service:
3939+4040+<screen>
4141+$ systemctl status postgresql.service
4242+postgresql.service - PostgreSQL Server
4343+ Loaded: loaded (/nix/store/pn3q73mvh75gsrl8w7fdlfk3fq5qm5mw-unit/postgresql.service)
4444+ Active: active (running) since Mon, 2013-01-07 15:55:57 CET; 9h ago
4545+ Main PID: 2390 (postgres)
4646+ CGroup: name=systemd:/system/postgresql.service
4747+ ├─2390 postgres
4848+ ├─2418 postgres: writer process
4949+ ├─2419 postgres: wal writer process
5050+ ├─2420 postgres: autovacuum launcher process
5151+ ├─2421 postgres: stats collector process
5252+ └─2498 postgres: zabbix zabbix [local] idle
5353+5454+Jan 07 15:55:55 hagbard postgres[2394]: [1-1] LOG: database system was shut down at 2013-01-07 15:55:05 CET
5555+Jan 07 15:55:57 hagbard postgres[2390]: [1-1] LOG: database system is ready to accept connections
5656+Jan 07 15:55:57 hagbard postgres[2420]: [1-1] LOG: autovacuum launcher started
5757+Jan 07 15:55:57 hagbard systemd[1]: Started PostgreSQL Server.
5858+</screen>
5959+6060+Note that this shows the status of the unit (active and running), all
6161+the processes belonging to the service, as well as the most recent log
6262+messages from the service.
6363+6464+</para>
6565+6666+<para>Units can be stopped, started or restarted:
6767+6868+<screen>
6969+$ systemctl stop postgresql.service
7070+$ systemctl start postgresql.service
7171+$ systemctl restart postgresql.service
7272+</screen>
7373+7474+These operations are synchronous: they wait until the service has
7575+finished starting or stopping (or has failed). Starting a unit will
7676+cause the dependencies of that unit to be started as well (if
7777+necessary).</para>
7878+7979+<!-- - cgroups: each service and user session is a cgroup
8080+8181+- cgroup resource management -->
8282+8383+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-nix-store-corruption">
66+77+<title>Nix Store Corruption</title>
88+99+<para>After a system crash, it’s possible for files in the Nix store
1010+to become corrupted. (For instance, the Ext4 file system has the
1111+tendency to replace un-synced files with zero bytes.) NixOS tries
1212+hard to prevent this from happening: it performs a
1313+<command>sync</command> before switching to a new configuration, and
1414+Nix’s database is fully transactional. If corruption still occurs,
1515+you may be able to fix it automatically.</para>
1616+1717+<para>If the corruption is in a path in the closure of the NixOS
1818+system configuration, you can fix it by doing
1919+2020+<screen>
2121+$ nixos-rebuild switch --repair
2222+</screen>
2323+2424+This will cause Nix to check every path in the closure, and if its
2525+cryptographic hash differs from the hash recorded in Nix’s database,
2626+the path is rebuilt or redownloaded.</para>
2727+2828+<para>You can also scan the entire Nix store for corrupt paths:
2929+3030+<screen>
3131+$ nix-store --verify --check-contents --repair
3232+</screen>
3333+3434+Any corrupt paths will be redownloaded if they’re available in a
3535+binary cache; otherwise, they cannot be repaired.</para>
3636+3737+</section>
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-troubleshooting">
66+77+<title>Troubleshooting</title>
88+99+<para>This chapter describes solutions to common problems you might
1010+encounter when you manage your NixOS system.</para>
1111+1212+<xi:include href="boot-problems.xml" />
1313+<xi:include href="maintenance-mode.xml" />
1414+<xi:include href="rollback.xml" />
1515+<xi:include href="store-corruption.xml" />
1616+<xi:include href="network-problems.xml" />
1717+1818+</chapter>
+53
nixos/doc/manual/administration/user-sessions.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-user-sessions">
66+77+<title>User Sessions</title>
88+99+<para>Systemd keeps track of all users who are logged into the system
1010+(e.g. on a virtual console or remotely via SSH). The command
1111+<command>loginctl</command> allows querying and manipulating user
1212+sessions. For instance, to list all user sessions:
1313+1414+<screen>
1515+$ loginctl
1616+ SESSION UID USER SEAT
1717+ c1 500 eelco seat0
1818+ c3 0 root seat0
1919+ c4 500 alice
2020+</screen>
2121+2222+This shows that two users are logged in locally, while another is
2323+logged in remotely. (“Seats” are essentially the combinations of
2424+displays and input devices attached to the system; usually, there is
2525+only one seat.) To get information about a session:
2626+2727+<screen>
2828+$ loginctl session-status c3
2929+c3 - root (0)
3030+ Since: Tue, 2013-01-08 01:17:56 CET; 4min 42s ago
3131+ Leader: 2536 (login)
3232+ Seat: seat0; vc3
3333+ TTY: /dev/tty3
3434+ Service: login; type tty; class user
3535+ State: online
3636+ CGroup: name=systemd:/user/root/c3
3737+ ├─ 2536 /nix/store/10mn4xip9n7y9bxqwnsx7xwx2v2g34xn-shadow-4.1.5.1/bin/login --
3838+ ├─10339 -bash
3939+ └─10355 w3m nixos.org
4040+</screen>
4141+4242+This shows that the user is logged in on virtual console 3. It also
4343+lists the processes belonging to this session. Since systemd keeps
4444+track of this, you can terminate a session in a way that ensures that
4545+all the session’s processes are gone:
4646+4747+<screen>
4848+$ loginctl terminate-session c3
4949+</screen>
5050+5151+</para>
5252+5353+</chapter>
-1563
nixos/doc/manual/configuration.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-configuration">
44-55-<title>Configuring NixOS</title>
66-77-<para>This chapter describes how to configure various aspects of a
88-NixOS machine through the configuration file
99-<filename>/etc/nixos/configuration.nix</filename>. As described in
1010-<xref linkend="sec-changing-config" />, changes to this file only take
1111-effect after you run <command>nixos-rebuild</command>.</para>
1212-1313-1414-<!--===============================================================-->
1515-1616-<section xml:id="sec-configuration-syntax"><title>Configuration syntax</title>
1717-1818-<section><title>The basics</title>
1919-2020-<para>The NixOS configuration file
2121-<filename>/etc/nixos/configuration.nix</filename> is actually a
2222-<emphasis>Nix expression</emphasis>, which is the Nix package
2323-manager’s purely functional language for describing how to build
2424-packages and configurations. This means you have all the expressive
2525-power of that language at your disposal, including the ability to
2626-abstract over common patterns, which is very useful when managing
2727-complex systems. The syntax and semantics of the Nix language are
2828-fully described in the <link
2929-xlink:href="http://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix
3030-manual</link>, but here we give a short overview of the most important
3131-constructs useful in NixOS configuration files.</para>
3232-3333-<para>The NixOS configuration file generally looks like this:
3434-3535-<programlisting>
3636-{ config, pkgs, ... }:
3737-3838-{ <replaceable>option definitions</replaceable>
3939-}
4040-</programlisting>
4141-4242-The first line (<literal>{ config, pkgs, ... }:</literal>) denotes
4343-that this is actually a function that takes at least the two arguments
4444- <varname>config</varname> and <varname>pkgs</varname>. (These are
4545-explained later.) The function returns a <emphasis>set</emphasis> of
4646-option definitions (<literal>{ <replaceable>...</replaceable> }</literal>). These definitions have the
4747-form <literal><replaceable>name</replaceable> =
4848-<replaceable>value</replaceable></literal>, where
4949-<replaceable>name</replaceable> is the name of an option and
5050-<replaceable>value</replaceable> is its value. For example,
5151-5252-<programlisting>
5353-{ config, pkgs, ... }:
5454-5555-{ services.httpd.enable = true;
5656- services.httpd.adminAddr = "alice@example.org";
5757- services.httpd.documentRoot = "/webroot";
5858-}
5959-</programlisting>
6060-6161-defines a configuration with three option definitions that together
6262-enable the Apache HTTP Server with <filename>/webroot</filename> as
6363-the document root.</para>
6464-6565-<para>Sets can be nested, and in fact dots in option names are
6666-shorthand for defining a set containing another set. For instance,
6767-<option>services.httpd.enable</option> defines a set named
6868-<varname>services</varname> that contains a set named
6969-<varname>httpd</varname>, which in turn contains an option definition
7070-named <varname>enable</varname> with value <literal>true</literal>.
7171-This means that the example above can also be written as:
7272-7373-<programlisting>
7474-{ config, pkgs, ... }:
7575-7676-{ services = {
7777- httpd = {
7878- enable = true;
7979- adminAddr = "alice@example.org";
8080- documentRoot = "/webroot";
8181- };
8282- };
8383-}
8484-</programlisting>
8585-8686-which may be more convenient if you have lots of option definitions
8787-that share the same prefix (such as
8888-<literal>services.httpd</literal>).</para>
8989-9090-<para>NixOS checks your option definitions for correctness. For
9191-instance, if you try to define an option that doesn’t exist (that is,
9292-doesn’t have a corresponding <emphasis>option declaration</emphasis>),
9393-<command>nixos-rebuild</command> will give an error like:
9494-<screen>
9595-The option `services.httpd.enabl' defined in `/etc/nixos/configuration.nix' does not exist.
9696-</screen>
9797-Likewise, values in option definitions must have a correct type. For
9898-instance, <option>services.httpd.enable</option> must be a Boolean
9999-(<literal>true</literal> or <literal>false</literal>). Trying to give
100100-it a value of another type, such as a string, will cause an error:
101101-<screen>
102102-The option value `services.httpd.enable' in `/etc/nixos/configuration.nix' is not a boolean.
103103-</screen>
104104-105105-</para>
106106-107107-<para>Options have various types of values. The most important are:
108108-109109-<variablelist>
110110- <varlistentry>
111111- <term>Strings</term>
112112- <listitem>
113113- <para>Strings are enclosed in double quotes, e.g.
114114-115115-<programlisting>
116116-networking.hostName = "dexter";
117117-</programlisting>
118118-119119- Special characters can be escaped by prefixing them with a
120120- backslash (e.g. <literal>\"</literal>).</para>
121121-122122- <para>Multi-line strings can be enclosed in <emphasis>double
123123- single quotes</emphasis>, e.g.
124124-125125-<programlisting>
126126-networking.extraHosts =
127127- ''
128128- 127.0.0.2 other-localhost
129129- 10.0.0.1 server
130130- '';
131131-</programlisting>
132132-133133- The main difference is that preceding whitespace is
134134- automatically stripped from each line, and that characters like
135135- <literal>"</literal> and <literal>\</literal> are not special
136136- (making it more convenient for including things like shell
137137- code).</para>
138138- </listitem>
139139- </varlistentry>
140140-141141- <varlistentry>
142142- <term>Booleans</term>
143143- <listitem>
144144- <para>These can be <literal>true</literal> or
145145- <literal>false</literal>, e.g.
146146-147147-<programlisting>
148148-networking.firewall.enable = true;
149149-networking.firewall.allowPing = false;
150150-</programlisting>
151151- </para>
152152- </listitem>
153153- </varlistentry>
154154-155155- <varlistentry>
156156- <term>Integers</term>
157157- <listitem>
158158- <para>For example,
159159-160160-<programlisting>
161161-boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 60;
162162-</programlisting>
163163-164164- (Note that here the attribute name
165165- <literal>net.ipv4.tcp_keepalive_time</literal> is enclosed in
166166- quotes to prevent it from being interpreted as a set named
167167- <literal>net</literal> containing a set named
168168- <literal>ipv4</literal>, and so on. This is because it’s not a
169169- NixOS option but the literal name of a Linux kernel
170170- setting.)</para>
171171- </listitem>
172172- </varlistentry>
173173-174174- <varlistentry>
175175- <term>Sets</term>
176176- <listitem>
177177- <para>Sets were introduced above. They are name/value pairs
178178- enclosed in braces, as in the option definition
179179-180180-<programlisting>
181181-fileSystems."/boot" =
182182- { device = "/dev/sda1";
183183- fsType = "ext4";
184184- options = "rw,data=ordered,relatime";
185185- };
186186-</programlisting>
187187- </para>
188188- </listitem>
189189- </varlistentry>
190190-191191- <varlistentry>
192192- <term>Lists</term>
193193- <listitem>
194194- <para>The important thing to note about lists is that list
195195- elements are separated by whitespace, like this:
196196-197197-<programlisting>
198198-boot.kernelModules = [ "fuse" "kvm-intel" "coretemp" ];
199199-</programlisting>
200200-201201- List elements can be any other type, e.g. sets:
202202-203203-<programlisting>
204204-swapDevices = [ { device = "/dev/disk/by-label/swap"; } ];
205205-</programlisting>
206206- </para>
207207- </listitem>
208208- </varlistentry>
209209-210210- <varlistentry>
211211- <term>Packages</term>
212212- <listitem>
213213- <para>Usually, the packages you need are already part of the Nix
214214- Packages collection, which is a set that can be accessed through
215215- the function argument <varname>pkgs</varname>. Typical uses:
216216-217217-<programlisting>
218218-environment.systemPackages =
219219- [ pkgs.thunderbird
220220- pkgs.emacs
221221- ];
222222-223223-postgresql.package = pkgs.postgresql90;
224224-</programlisting>
225225-226226- The latter option definition changes the default PostgreSQL
227227- package used by NixOS’s PostgreSQL service to 9.0. For more
228228- information on packages, including how to add new ones, see
229229- <xref linkend="sec-custom-packages"/>.</para>
230230- </listitem>
231231- </varlistentry>
232232-233233-</variablelist>
234234-235235-</para>
236236-237237-</section>
238238-239239-240240-<section xml:id="sec-module-abstractions"><title>Abstractions</title>
241241-242242-<para>If you find yourself repeating yourself over and over, it’s time
243243-to abstract. Take, for instance, this Apache HTTP Server configuration:
244244-245245-<programlisting>
246246-{
247247- services.httpd.virtualHosts =
248248- [ { hostName = "example.org";
249249- documentRoot = "/webroot";
250250- adminAddr = "alice@example.org";
251251- enableUserDir = true;
252252- }
253253- { hostName = "example.org";
254254- documentRoot = "/webroot";
255255- adminAddr = "alice@example.org";
256256- enableUserDir = true;
257257- enableSSL = true;
258258- sslServerCert = "/root/ssl-example-org.crt";
259259- sslServerKey = "/root/ssl-example-org.key";
260260- }
261261- ];
262262-}
263263-</programlisting>
264264-265265-It defines two virtual hosts with nearly identical configuration; the
266266-only difference is that the second one has SSL enabled. To prevent
267267-this duplication, we can use a <literal>let</literal>:
268268-269269-<programlisting>
270270-let
271271- exampleOrgCommon =
272272- { hostName = "example.org";
273273- documentRoot = "/webroot";
274274- adminAddr = "alice@example.org";
275275- enableUserDir = true;
276276- };
277277-in
278278-{
279279- services.httpd.virtualHosts =
280280- [ exampleOrgCommon
281281- (exampleOrgCommon // {
282282- enableSSL = true;
283283- sslServerCert = "/root/ssl-example-org.crt";
284284- sslServerKey = "/root/ssl-example-org.key";
285285- })
286286- ];
287287-}
288288-</programlisting>
289289-290290-The <literal>let exampleOrgCommon =
291291-<replaceable>...</replaceable></literal> defines a variable named
292292-<literal>exampleOrgCommon</literal>. The <literal>//</literal>
293293-operator merges two attribute sets, so the configuration of the second
294294-virtual host is the set <literal>exampleOrgCommon</literal> extended
295295-with the SSL options.</para>
296296-297297-<para>You can write a <literal>let</literal> wherever an expression is
298298-allowed. Thus, you also could have written:
299299-300300-<programlisting>
301301-{
302302- services.httpd.virtualHosts =
303303- let exampleOrgCommon = <replaceable>...</replaceable>; in
304304- [ exampleOrgCommon
305305- (exampleOrgCommon // { <replaceable>...</replaceable> })
306306- ];
307307-}
308308-</programlisting>
309309-310310-but not <literal>{ let exampleOrgCommon =
311311-<replaceable>...</replaceable>; in <replaceable>...</replaceable>;
312312-}</literal> since attributes (as opposed to attribute values) are not
313313-expressions.</para>
314314-315315-<para><emphasis>Functions</emphasis> provide another method of
316316-abstraction. For instance, suppose that we want to generate lots of
317317-different virtual hosts, all with identical configuration except for
318318-the host name. This can be done as follows:
319319-320320-<programlisting>
321321-{
322322- services.httpd.virtualHosts =
323323- let
324324- makeVirtualHost = name:
325325- { hostName = name;
326326- documentRoot = "/webroot";
327327- adminAddr = "alice@example.org";
328328- };
329329- in
330330- [ (makeVirtualHost "example.org")
331331- (makeVirtualHost "example.com")
332332- (makeVirtualHost "example.gov")
333333- (makeVirtualHost "example.nl")
334334- ];
335335-}
336336-</programlisting>
337337-338338-Here, <varname>makeVirtualHost</varname> is a function that takes a
339339-single argument <literal>name</literal> and returns the configuration
340340-for a virtual host. That function is then called for several names to
341341-produce the list of virtual host configurations.</para>
342342-343343-<para>We can further improve on this by using the function
344344-<varname>map</varname>, which applies another function to every
345345-element in a list:
346346-347347-<programlisting>
348348-{
349349- services.httpd.virtualHosts =
350350- let
351351- makeVirtualHost = <replaceable>...</replaceable>;
352352- in map makeVirtualHost
353353- [ "example.org" "example.com" "example.gov" "example.nl" ];
354354-}
355355-</programlisting>
356356-357357-(The function <literal>map</literal> is called a
358358-<emphasis>higher-order function</emphasis> because it takes another
359359-function as an argument.)</para>
360360-361361-<para>What if you need more than one argument, for instance, if we
362362-want to use a different <literal>documentRoot</literal> for each
363363-virtual host? Then we can make <varname>makeVirtualHost</varname> a
364364-function that takes a <emphasis>set</emphasis> as its argument, like this:
365365-366366-<programlisting>
367367-{
368368- services.httpd.virtualHosts =
369369- let
370370- makeVirtualHost = { name, root }:
371371- { hostName = name;
372372- documentRoot = root;
373373- adminAddr = "alice@example.org";
374374- };
375375- in map makeVirtualHost
376376- [ { name = "example.org"; root = "/sites/example.org"; }
377377- { name = "example.com"; root = "/sites/example.com"; }
378378- { name = "example.gov"; root = "/sites/example.gov"; }
379379- { name = "example.nl"; root = "/sites/example.nl"; }
380380- ];
381381-}
382382-</programlisting>
383383-384384-But in this case (where every root is a subdirectory of
385385-<filename>/sites</filename> named after the virtual host), it would
386386-have been shorter to define <varname>makeVirtualHost</varname> as
387387-<programlisting>
388388-makeVirtualHost = name:
389389- { hostName = name;
390390- documentRoot = "/sites/${name}";
391391- adminAddr = "alice@example.org";
392392- };
393393-</programlisting>
394394-395395-Here, the construct
396396-<literal>${<replaceable>...</replaceable>}</literal> allows the result
397397-of an expression to be spliced into a string.</para>
398398-399399-</section>
400400-401401-402402-<section xml:id="sec-modularity"><title>Modularity</title>
403403-404404-<para>The NixOS configuration mechanism is modular. If your
405405-<filename>configuration.nix</filename> becomes too big, you can split
406406-it into multiple files. Likewise, if you have multiple NixOS
407407-configurations (e.g. for different computers) with some commonality,
408408-you can move the common configuration into a shared file.</para>
409409-410410-<para>Modules have exactly the same syntax as
411411-<filename>configuration.nix</filename>. In fact,
412412-<filename>configuration.nix</filename> is itself a module. You can
413413-use other modules by including them from
414414-<filename>configuration.nix</filename>, e.g.:
415415-416416-<programlisting>
417417-{ config, pkgs, ... }:
418418-419419-{ imports = [ ./vpn.nix ./kde.nix ];
420420- services.httpd.enable = true;
421421- environment.systemPackages = [ pkgs.emacs ];
422422- <replaceable>...</replaceable>
423423-}
424424-</programlisting>
425425-426426-Here, we include two modules from the same directory,
427427-<filename>vpn.nix</filename> and <filename>kde.nix</filename>. The
428428-latter might look like this:
429429-430430-<programlisting>
431431-{ config, pkgs, ... }:
432432-433433-{ services.xserver.enable = true;
434434- services.xserver.displayManager.kdm.enable = true;
435435- services.xserver.desktopManager.kde4.enable = true;
436436- environment.systemPackages = [ pkgs.kde4.kscreensaver ];
437437-}
438438-</programlisting>
439439-440440-Note that both <filename>configuration.nix</filename> and
441441-<filename>kde.nix</filename> define the option
442442-<option>environment.systemPackages</option>. When multiple modules
443443-define an option, NixOS will try to <emphasis>merge</emphasis> the
444444-definitions. In the case of
445445-<option>environment.systemPackages</option>, that’s easy: the lists of
446446-packages can simply be concatenated. The value in
447447-<filename>configuration.nix</filename> is merged last, so for
448448-list-type options, it will appear at the end of the merged list. If
449449-you want it to appear first, you can use <varname>mkBefore</varname>:
450450-451451-<programlisting>
452452-boot.kernelModules = mkBefore [ "kvm-intel" ];
453453-</programlisting>
454454-455455-This causes the <literal>kvm-intel</literal> kernel module to be
456456-loaded before any other kernel modules.</para>
457457-458458-<para>For other types of options, a merge may not be possible. For
459459-instance, if two modules define
460460-<option>services.httpd.adminAddr</option>,
461461-<command>nixos-rebuild</command> will give an error:
462462-463463-<screen>
464464-The unique option `services.httpd.adminAddr' is defined multiple times, in `/etc/nixos/httpd.nix' and `/etc/nixos/configuration.nix'.
465465-</screen>
466466-467467-When that happens, it’s possible to force one definition take
468468-precedence over the others:
469469-470470-<programlisting>
471471-services.httpd.adminAddr = pkgs.lib.mkForce "bob@example.org";
472472-</programlisting>
473473-474474-</para>
475475-476476-<para>When using multiple modules, you may need to access
477477-configuration values defined in other modules. This is what the
478478-<varname>config</varname> function argument is for: it contains the
479479-complete, merged system configuration. That is,
480480-<varname>config</varname> is the result of combining the
481481-configurations returned by every module<footnote><para>If you’re
482482-wondering how it’s possible that the (indirect)
483483-<emphasis>result</emphasis> of a function is passed as an
484484-<emphasis>input</emphasis> to that same function: that’s because Nix
485485-is a “lazy” language — it only computes values when they are needed.
486486-This works as long as no individual configuration value depends on
487487-itself.</para></footnote>. For example, here is a module that adds
488488-some packages to <option>environment.systemPackages</option> only if
489489-<option>services.xserver.enable</option> is set to
490490-<literal>true</literal> somewhere else:
491491-492492-<programlisting>
493493-{ config, pkgs, ... }:
494494-495495-{ environment.systemPackages =
496496- if config.services.xserver.enable then
497497- [ pkgs.firefox
498498- pkgs.thunderbird
499499- ]
500500- else
501501- [ ];
502502-}
503503-</programlisting>
504504-505505-</para>
506506-507507-<para>With multiple modules, it may not be obvious what the final
508508-value of a configuration option is. The command
509509-<option>nixos-option</option> allows you to find out:
510510-511511-<screen>
512512-$ nixos-option services.xserver.enable
513513-true
514514-515515-$ nixos-option boot.kernelModules
516516-[ "tun" "ipv6" "loop" <replaceable>...</replaceable> ]
517517-</screen>
518518-519519-Interactive exploration of the configuration is possible using
520520-<command
521521-xlink:href="https://github.com/edolstra/nix-repl">nix-repl</command>,
522522-a read-eval-print loop for Nix expressions. It’s not installed by
523523-default; run <literal>nix-env -i nix-repl</literal> to get it. A
524524-typical use:
525525-526526-<screen>
527527-$ nix-repl '<nixos>'
528528-529529-nix-repl> config.networking.hostName
530530-"mandark"
531531-532532-nix-repl> map (x: x.hostName) config.services.httpd.virtualHosts
533533-[ "example.org" "example.gov" ]
534534-</screen>
535535-536536-</para>
537537-538538-</section>
539539-540540-541541-<section xml:id="sec-nix-syntax-summary"><title>Syntax summary</title>
542542-543543-<para>Below is a summary of the most important syntactic constructs in
544544-the Nix expression language. It’s not complete. In particular, there
545545-are many other built-in functions. See the <link
546546-xlink:href="http://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix
547547-manual</link> for the rest.</para>
548548-549549-<informaltable frame='none'>
550550- <tgroup cols='2'>
551551- <colspec colname='c1' rowsep='1' colsep='1' />
552552- <colspec colname='c2' rowsep='1' />
553553- <thead>
554554- <row>
555555- <entry>Example</entry>
556556- <entry>Description</entry>
557557- </row>
558558- </thead>
559559- <tbody>
560560-561561- <row>
562562- <entry namest="c1" nameend="c2"><emphasis>Basic values</emphasis></entry>
563563- </row>
564564- <row>
565565- <entry><literal>"Hello world"</literal></entry>
566566- <entry>A string</entry>
567567- </row>
568568- <row>
569569- <entry><literal>"${pkgs.bash}/bin/sh"</literal></entry>
570570- <entry>A string containing an expression (expands to <literal>"/nix/store/<replaceable>hash</replaceable>-bash-<replaceable>version</replaceable>/bin/sh"</literal>)</entry>
571571- </row>
572572- <row>
573573- <entry><literal>true</literal>, <literal>false</literal></entry>
574574- <entry>Booleans</entry>
575575- </row>
576576- <row>
577577- <entry><literal>123</literal></entry>
578578- <entry>An integer</entry>
579579- </row>
580580- <row>
581581- <entry><literal>./foo.png</literal></entry>
582582- <entry>A path (relative to the containing Nix expression)</entry>
583583- </row>
584584-585585- <row>
586586- <entry namest="c1" nameend="c2"><emphasis>Compound values</emphasis></entry>
587587- </row>
588588- <row>
589589- <entry><literal>{ x = 1; y = 2; }</literal></entry>
590590- <entry>An set with attributes names <literal>x</literal> and <literal>y</literal></entry>
591591- </row>
592592- <row>
593593- <entry><literal>{ foo.bar = 1; }</literal></entry>
594594- <entry>A nested set, equivalent to <literal>{ foo = { bar = 1; }; }</literal></entry>
595595- </row>
596596- <row>
597597- <entry><literal>rec { x = "bla"; y = x + "bar"; }</literal></entry>
598598- <entry>A recursive set, equivalent to <literal>{ x = "foo"; y = "foobar"; }</literal></entry>
599599- </row>
600600- <row>
601601- <entry><literal>[ "foo" "bar" ]</literal></entry>
602602- <entry>A list with two elements</entry>
603603- </row>
604604-605605- <row>
606606- <entry namest="c1" nameend="c2"><emphasis>Operators</emphasis></entry>
607607- </row>
608608- <row>
609609- <entry><literal>"foo" + "bar"</literal></entry>
610610- <entry>String concatenation</entry>
611611- </row>
612612- <row>
613613- <entry><literal>1 + 2</literal></entry>
614614- <entry>Integer addition</entry>
615615- </row>
616616- <row>
617617- <entry><literal>"foo" == "f" + "oo"</literal></entry>
618618- <entry>Equality test (evaluates to <literal>true</literal>)</entry>
619619- </row>
620620- <row>
621621- <entry><literal>"foo" != "bar"</literal></entry>
622622- <entry>Inequality test (evaluates to <literal>true</literal>)</entry>
623623- </row>
624624- <row>
625625- <entry><literal>!true</literal></entry>
626626- <entry>Boolean negation</entry>
627627- </row>
628628- <row>
629629- <entry><literal>{ x = 1; y = 2; }.x</literal></entry>
630630- <entry>Attribute selection (evaluates to <literal>1</literal>)</entry>
631631- </row>
632632- <row>
633633- <entry><literal>{ x = 1; y = 2; }.z or 3</literal></entry>
634634- <entry>Attribute selection with default (evaluates to <literal>3</literal>)</entry>
635635- </row>
636636- <row>
637637- <entry><literal>{ x = 1; y = 2; } // { z = 3; }</literal></entry>
638638- <entry>Merge two sets (attributes in the right-hand set taking precedence)</entry>
639639- </row>
640640-641641- <row>
642642- <entry namest="c1" nameend="c2"><emphasis>Control structures</emphasis></entry>
643643- </row>
644644- <row>
645645- <entry><literal>if 1 + 1 == 2 then "yes!" else "no!"</literal></entry>
646646- <entry>Conditional expression</entry>
647647- </row>
648648- <row>
649649- <entry><literal>assert 1 + 1 == 2; "yes!"</literal></entry>
650650- <entry>Assertion check (evaluates to <literal>"yes!"</literal>)</entry>
651651- </row>
652652- <row>
653653- <entry><literal>let x = "foo"; y = "bar"; in x + y</literal></entry>
654654- <entry>Variable definition</entry>
655655- </row>
656656- <row>
657657- <entry><literal>with pkgs.lib; head [ 1 2 3 ]</literal></entry>
658658- <entry>Add all attributes from the given set to the scope
659659- (evaluates to <literal>1</literal>)</entry>
660660- </row>
661661-662662- <row>
663663- <entry namest="c1" nameend="c2"><emphasis>Functions (lambdas)</emphasis></entry>
664664- </row>
665665- <row>
666666- <entry><literal>x: x + 1</literal></entry>
667667- <entry>A function that expects an integer and returns it increased by 1</entry>
668668- </row>
669669- <row>
670670- <entry><literal>(x: x + 1) 100</literal></entry>
671671- <entry>A function call (evaluates to 101)</entry>
672672- </row>
673673- <row>
674674- <entry><literal>let inc = x: x + 1; in inc (inc (inc 100))</literal></entry>
675675- <entry>A function bound to a variable and subsequently called by name (evaluates to 103)</entry>
676676- </row>
677677- <row>
678678- <entry><literal>{ x, y }: x + y</literal></entry>
679679- <entry>A function that expects a set with required attributes
680680- <literal>x</literal> and <literal>y</literal> and concatenates
681681- them</entry>
682682- </row>
683683- <row>
684684- <entry><literal>{ x, y ? "bar" }: x + y</literal></entry>
685685- <entry>A function that expects a set with required attribute
686686- <literal>x</literal> and optional <literal>y</literal>, using
687687- <literal>"bar"</literal> as default value for
688688- <literal>y</literal></entry>
689689- </row>
690690- <row>
691691- <entry><literal>{ x, y, ... }: x + y</literal></entry>
692692- <entry>A function that expects a set with required attributes
693693- <literal>x</literal> and <literal>y</literal> and ignores any
694694- other attributes</entry>
695695- </row>
696696- <row>
697697- <entry><literal>{ x, y } @ args: x + y</literal></entry>
698698- <entry>A function that expects a set with required attributes
699699- <literal>x</literal> and <literal>y</literal>, and binds the
700700- whole set to <literal>args</literal></entry>
701701- </row>
702702-703703- <row>
704704- <entry namest="c1" nameend="c2"><emphasis>Built-in functions</emphasis></entry>
705705- </row>
706706- <row>
707707- <entry><literal>import ./foo.nix</literal></entry>
708708- <entry>Load and return Nix expression in given file</entry>
709709- </row>
710710- <row>
711711- <entry><literal>map (x: x + x) [ 1 2 3 ]</literal></entry>
712712- <entry>Apply a function to every element of a list (evaluates to <literal>[ 2 4 6 ]</literal>)</entry>
713713- </row>
714714- <!--
715715- <row>
716716- <entry><literal>throw "Urgh"</literal></entry>
717717- <entry>Raise an error condition</entry>
718718- </row>
719719- -->
720720-721721- </tbody>
722722- </tgroup>
723723-</informaltable>
724724-725725-</section>
726726-727727-728728-</section>
729729-730730-731731-<!--===============================================================-->
732732-733733-<section xml:id="sec-package-management"><title>Package management</title>
734734-735735-<para>This section describes how to add additional packages to your
736736-system. NixOS has two distinct styles of package management:
737737-738738-<itemizedlist>
739739-740740- <listitem><para><emphasis>Declarative</emphasis>, where you declare
741741- what packages you want in your
742742- <filename>configuration.nix</filename>. Every time you run
743743- <command>nixos-rebuild</command>, NixOS will ensure that you get a
744744- consistent set of binaries corresponding to your
745745- specification.</para></listitem>
746746-747747- <listitem><para><emphasis>Ad hoc</emphasis>, where you install,
748748- upgrade and uninstall packages via the <command>nix-env</command>
749749- command. This style allows mixing packages from different Nixpkgs
750750- versions. It’s the only choice for non-root
751751- users.</para></listitem>
752752-753753-</itemizedlist>
754754-755755-</para>
756756-757757-<para>The next two sections describe these two styles.</para>
758758-759759-760760-<section><title>Declarative package management</title>
761761-762762-<para>With declarative package management, you specify which packages
763763-you want on your system by setting the option
764764-<option>environment.systemPackages</option>. For instance, adding the
765765-following line to <filename>configuration.nix</filename> enables the
766766-Mozilla Thunderbird email application:
767767-768768-<programlisting>
769769-environment.systemPackages = [ pkgs.thunderbird ];
770770-</programlisting>
771771-772772-The effect of this specification is that the Thunderbird package from
773773-Nixpkgs will be built or downloaded as part of the system when you run
774774-<command>nixos-rebuild switch</command>.</para>
775775-776776-<para>You can get a list of the available packages as follows:
777777-<screen>
778778-$ nix-env -qaP '*' --description
779779-nixos.pkgs.firefox firefox-23.0 Mozilla Firefox - the browser, reloaded
780780-<replaceable>...</replaceable>
781781-</screen>
782782-783783-The first column in the output is the <emphasis>attribute
784784-name</emphasis>, such as
785785-<literal>nixos.pkgs.thunderbird</literal>. (The
786786-<literal>nixos</literal> prefix allows distinguishing between
787787-different channels that you might have.)</para>
788788-789789-<para>To “uninstall” a package, simply remove it from
790790-<option>environment.systemPackages</option> and run
791791-<command>nixos-rebuild switch</command>.</para>
792792-793793-794794-<section xml:id="sec-customising-packages"><title>Customising packages</title>
795795-796796-<para>Some packages in Nixpkgs have options to enable or disable
797797-optional functionality or change other aspects of the package. For
798798-instance, the Firefox wrapper package (which provides Firefox with a
799799-set of plugins such as the Adobe Flash player) has an option to enable
800800-the Google Talk plugin. It can be set in
801801-<filename>configuration.nix</filename> as follows:
802802-803803-<filename>
804804-nixpkgs.config.firefox.enableGoogleTalkPlugin = true;
805805-</filename>
806806-</para>
807807-808808-<warning><para>Unfortunately, Nixpkgs currently lacks a way to query
809809-available configuration options.</para></warning>
810810-811811-<para>Apart from high-level options, it’s possible to tweak a package
812812-in almost arbitrary ways, such as changing or disabling dependencies
813813-of a package. For instance, the Emacs package in Nixpkgs by default
814814-has a dependency on GTK+ 2. If you want to build it against GTK+ 3,
815815-you can specify that as follows:
816816-817817-<programlisting>
818818-environment.systemPackages = [ (pkgs.emacs.override { gtk = pkgs.gtk3; }) ];
819819-</programlisting>
820820-821821-The function <varname>override</varname> performs the call to the Nix
822822-function that produces Emacs, with the original arguments amended by
823823-the set of arguments specified by you. So here the function argument
824824-<varname>gtk</varname> gets the value <literal>pkgs.gtk3</literal>,
825825-causing Emacs to depend on GTK+ 3. (The parentheses are necessary
826826-because in Nix, function application binds more weakly than list
827827-construction, so without them,
828828-<literal>environment.systemPackages</literal> would be a list with two
829829-elements.)</para>
830830-831831-<para>Even greater customisation is possible using the function
832832-<varname>overrideDerivation</varname>. While the
833833-<varname>override</varname> mechanism above overrides the arguments of
834834-a package function, <varname>overrideDerivation</varname> allows
835835-changing the <emphasis>result</emphasis> of the function. This
836836-permits changing any aspect of the package, such as the source code.
837837-For instance, if you want to override the source code of Emacs, you
838838-can say:
839839-840840-<programlisting>
841841-environment.systemPackages =
842842- [ (pkgs.lib.overrideDerivation pkgs.emacs (attrs: {
843843- name = "emacs-25.0-pre";
844844- src = /path/to/my/emacs/tree;
845845- }))
846846- ];
847847-</programlisting>
848848-849849-Here, <varname>overrideDerivation</varname> takes the Nix derivation
850850-specified by <varname>pkgs.emacs</varname> and produces a new
851851-derivation in which the original’s <literal>name</literal> and
852852-<literal>src</literal> attribute have been replaced by the given
853853-values. The original attributes are accessible via
854854-<varname>attrs</varname>.</para>
855855-856856-<para>The overrides shown above are not global. They do not affect
857857-the original package; other packages in Nixpkgs continue to depend on
858858-the original rather than the customised package. This means that if
859859-another package in your system depends on the original package, you
860860-end up with two instances of the package. If you want to have
861861-everything depend on your customised instance, you can apply a
862862-<emphasis>global</emphasis> override as follows:
863863-864864-<screen>
865865-nixpkgs.config.packageOverrides = pkgs:
866866- { emacs = pkgs.emacs.override { gtk = pkgs.gtk3; };
867867- };
868868-</screen>
869869-870870-The effect of this definition is essentially equivalent to modifying
871871-the <literal>emacs</literal> attribute in the Nixpkgs source tree.
872872-Any package in Nixpkgs that depends on <literal>emacs</literal> will
873873-be passed your customised instance. (However, the value
874874-<literal>pkgs.emacs</literal> in
875875-<varname>nixpkgs.config.packageOverrides</varname> refers to the
876876-original rather than overridden instance, to prevent an infinite
877877-recursion.)</para>
878878-879879-</section>
880880-881881-<section xml:id="sec-custom-packages"><title>Adding custom packages</title>
882882-883883-<para>It’s possible that a package you need is not available in NixOS.
884884-In that case, you can do two things. First, you can clone the Nixpkgs
885885-repository, add the package to your clone, and (optionally) submit a
886886-patch or pull request to have it accepted into the main Nixpkgs
887887-repository. This is described in detail in the <link
888888-xlink:href="http://nixos.org/nixpkgs/manual">Nixpkgs manual</link>.
889889-In short, you clone Nixpkgs:
890890-891891-<screen>
892892-$ git clone git://github.com/NixOS/nixpkgs.git
893893-$ cd nixpkgs
894894-</screen>
895895-896896-Then you write and test the package as described in the Nixpkgs
897897-manual. Finally, you add it to
898898-<literal>environment.systemPackages</literal>, e.g.
899899-900900-<programlisting>
901901-environment.systemPackages = [ pkgs.my-package ];
902902-</programlisting>
903903-904904-and you run <command>nixos-rebuild</command>, specifying your own
905905-Nixpkgs tree:
906906-907907-<screen>
908908-$ nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs</screen>
909909-910910-</para>
911911-912912-<para>The second possibility is to add the package outside of the
913913-Nixpkgs tree. For instance, here is how you specify a build of the
914914-<link xlink:href="http://www.gnu.org/software/hello/">GNU Hello</link>
915915-package directly in <filename>configuration.nix</filename>:
916916-917917-<programlisting>
918918-environment.systemPackages =
919919- let
920920- my-hello = with pkgs; stdenv.mkDerivation rec {
921921- name = "hello-2.8";
922922- src = fetchurl {
923923- url = "mirror://gnu/hello/${name}.tar.gz";
924924- sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
925925- };
926926- };
927927- in
928928- [ my-hello ];
929929-</programlisting>
930930-931931-Of course, you can also move the definition of
932932-<literal>my-hello</literal> into a separate Nix expression, e.g.
933933-<programlisting>
934934-environment.systemPackages = [ (import ./my-hello.nix) ];
935935-</programlisting>
936936-where <filename>my-hello.nix</filename> contains:
937937-<programlisting>
938938-with import <nixpkgs> {}; # bring all of Nixpkgs into scope
939939-940940-stdenv.mkDerivation rec {
941941- name = "hello-2.8";
942942- src = fetchurl {
943943- url = "mirror://gnu/hello/${name}.tar.gz";
944944- sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
945945- };
946946-}
947947-</programlisting>
948948-949949-This allows testing the package easily:
950950-<screen>
951951-$ nix-build my-hello.nix
952952-$ ./result/bin/hello
953953-Hello, world!
954954-</screen>
955955-956956-</para>
957957-958958-</section>
959959-960960-</section>
961961-962962-963963-<section><title>Ad hoc package management</title>
964964-965965-<para>With the command <command>nix-env</command>, you can install and
966966-uninstall packages from the command line. For instance, to install
967967-Mozilla Thunderbird:
968968-969969-<screen>
970970-$ nix-env -iA nixos.pkgs.thunderbird</screen>
971971-972972-If you invoke this as root, the package is installed in the Nix
973973-profile <filename>/nix/var/nix/profiles/default</filename> and visible
974974-to all users of the system; otherwise, the package ends up in
975975-<filename>/nix/var/nix/profiles/per-user/<replaceable>username</replaceable>/profile</filename>
976976-and is not visible to other users. The <option>-A</option> flag
977977-specifies the package by its attribute name; without it, the package
978978-is installed by matching against its package name
979979-(e.g. <literal>thunderbird</literal>). The latter is slower because
980980-it requires matching against all available Nix packages, and is
981981-ambiguous if there are multiple matching packages.</para>
982982-983983-<para>Packages come from the NixOS channel. You typically upgrade a
984984-package by updating to the latest version of the NixOS channel:
985985-<screen>
986986-$ nix-channel --update nixos
987987-</screen>
988988-and then running <literal>nix-env -i</literal> again. Other packages
989989-in the profile are <emphasis>not</emphasis> affected; this is the
990990-crucial difference with the declarative style of package management,
991991-where running <command>nixos-rebuild switch</command> causes all
992992-packages to be updated to their current versions in the NixOS channel.
993993-You can however upgrade all packages for which there is a newer
994994-version by doing:
995995-<screen>
996996-$ nix-env -u '*'
997997-</screen>
998998-</para>
999999-10001000-<para>A package can be uninstalled using the <option>-e</option>
10011001-flag:
10021002-<screen>
10031003-$ nix-env -e thunderbird
10041004-</screen>
10051005-</para>
10061006-10071007-<para>Finally, you can roll back an undesirable
10081008-<command>nix-env</command> action:
10091009-<screen>
10101010-$ nix-env --rollback
10111011-</screen>
10121012-</para>
10131013-10141014-<para><command>nix-env</command> has many more flags. For details,
10151015-see the
10161016-<citerefentry><refentrytitle>nix-env</refentrytitle><manvolnum>1</manvolnum></citerefentry>
10171017-manpage or the Nix manual.</para>
10181018-10191019-</section>
10201020-10211021-10221022-</section>
10231023-10241024-10251025-<!--===============================================================-->
10261026-10271027-<section xml:id="sec-user-management"><title>User management</title>
10281028-10291029-<para>NixOS supports both declarative and imperative styles of user
10301030-management. In the declarative style, users are specified in
10311031-<filename>configuration.nix</filename>. For instance, the following
10321032-states that a user account named <literal>alice</literal> shall exist:
10331033-10341034-<programlisting>
10351035-users.extraUsers.alice =
10361036- { createHome = true;
10371037- home = "/home/alice";
10381038- description = "Alice Foobar";
10391039- extraGroups = [ "wheel" "networkmanager" ];
10401040- useDefaultShell = true;
10411041- openssh.authorizedKeys.keys = [ "ssh-dss AAAAB3Nza... alice@foobar" ];
10421042- };
10431043-</programlisting>
10441044-10451045-Note that <literal>alice</literal> is a member of the
10461046-<literal>wheel</literal> and <literal>networkmanager</literal> groups,
10471047-which allows her to use <command>sudo</command> to execute commands as
10481048-<literal>root</literal> and to configure the network, respectively.
10491049-Also note the SSH public key that allows remote logins with the
10501050-corresponding private key. Users created in this way do not have a
10511051-password by default, so they cannot log in via mechanisms that require
10521052-a password. However, you can use the <command>passwd</command> program
10531053-to set a password, which is retained across invocations of
10541054-<command>nixos-rebuild</command>.</para>
10551055-10561056-<para>If you set users.mutableUsers to false, then the contents of /etc/passwd
10571057-and /etc/group will be congruent to your NixOS configuration. For instance,
10581058-if you remove a user from users.extraUsers and run nixos-rebuild, the user
10591059-account will cease to exist. Also, imperative commands for managing users
10601060-and groups, such as useradd, are no longer available.</para>
10611061-10621062-<para>A user ID (uid) is assigned automatically. You can also specify
10631063-a uid manually by adding
10641064-10651065-<programlisting>
10661066- uid = 1000;
10671067-</programlisting>
10681068-10691069-to the user specification.</para>
10701070-10711071-<para>Groups can be specified similarly. The following states that a
10721072-group named <literal>students</literal> shall exist:
10731073-10741074-<programlisting>
10751075-users.extraGroups.students.gid = 1000;
10761076-</programlisting>
10771077-10781078-As with users, the group ID (gid) is optional and will be assigned
10791079-automatically if it’s missing.</para>
10801080-10811081-<warning><para>Currently declarative user management is not perfect:
10821082-<command>nixos-rebuild</command> does not know how to realise certain
10831083-configuration changes. This includes removing a user or group, and
10841084-removing group membership from a user.</para></warning>
10851085-10861086-<para>In the imperative style, users and groups are managed by
10871087-commands such as <command>useradd</command>,
10881088-<command>groupmod</command> and so on. For instance, to create a user
10891089-account named <literal>alice</literal>:
10901090-10911091-<screen>
10921092-$ useradd -m alice</screen>
10931093-10941094-The flag <option>-m</option> causes the creation of a home directory
10951095-for the new user, which is generally what you want. The user does not
10961096-have an initial password and therefore cannot log in. A password can
10971097-be set using the <command>passwd</command> utility:
10981098-10991099-<screen>
11001100-$ passwd alice
11011101-Enter new UNIX password: ***
11021102-Retype new UNIX password: ***
11031103-</screen>
11041104-11051105-A user can be deleted using <command>userdel</command>:
11061106-11071107-<screen>
11081108-$ userdel -r alice</screen>
11091109-11101110-The flag <option>-r</option> deletes the user’s home directory.
11111111-Accounts can be modified using <command>usermod</command>. Unix
11121112-groups can be managed using <command>groupadd</command>,
11131113-<command>groupmod</command> and <command>groupdel</command>.</para>
11141114-11151115-</section>
11161116-11171117-11181118-<!--===============================================================-->
11191119-11201120-<section><title>File systems</title>
11211121-11221122-<para>You can define file systems using the
11231123-<option>fileSystems</option> configuration option. For instance, the
11241124-following definition causes NixOS to mount the Ext4 file system on
11251125-device <filename>/dev/disk/by-label/data</filename> onto the mount
11261126-point <filename>/data</filename>:
11271127-11281128-<programlisting>
11291129-fileSystems."/data" =
11301130- { device = "/dev/disk/by-label/data";
11311131- fsType = "ext4";
11321132- };
11331133-</programlisting>
11341134-11351135-Mount points are created automatically if they don’t already exist.
11361136-For <option>device</option>, it’s best to use the topology-independent
11371137-device aliases in <filename>/dev/disk/by-label</filename> and
11381138-<filename>/dev/disk/by-uuid</filename>, as these don’t change if the
11391139-topology changes (e.g. if a disk is moved to another IDE
11401140-controller).</para>
11411141-11421142-<para>You can usually omit the file system type
11431143-(<option>fsType</option>), since <command>mount</command> can usually
11441144-detect the type and load the necessary kernel module automatically.
11451145-However, if the file system is needed at early boot (in the initial
11461146-ramdisk) and is not <literal>ext2</literal>, <literal>ext3</literal>
11471147-or <literal>ext4</literal>, then it’s best to specify
11481148-<option>fsType</option> to ensure that the kernel module is
11491149-available.</para>
11501150-11511151-<section><title>LUKS-encrypted file systems</title>
11521152-11531153-<para>NixOS supports file systems that are encrypted using
11541154-<emphasis>LUKS</emphasis> (Linux Unified Key Setup). For example,
11551155-here is how you create an encrypted Ext4 file system on the device
11561156-<filename>/dev/sda2</filename>:
11571157-11581158-<screen>
11591159-$ cryptsetup luksFormat /dev/sda2
11601160-11611161-WARNING!
11621162-========
11631163-This will overwrite data on /dev/sda2 irrevocably.
11641164-11651165-Are you sure? (Type uppercase yes): YES
11661166-Enter LUKS passphrase: ***
11671167-Verify passphrase: ***
11681168-11691169-$ cryptsetup luksOpen /dev/sda2 crypted
11701170-Enter passphrase for /dev/sda2: ***
11711171-11721172-$ mkfs.ext4 /dev/mapper/crypted
11731173-</screen>
11741174-11751175-To ensure that this file system is automatically mounted at boot time
11761176-as <filename>/</filename>, add the following to
11771177-<filename>configuration.nix</filename>:
11781178-11791179-<programlisting>
11801180-boot.initrd.luks.devices = [ { device = "/dev/sda2"; name = "crypted"; } ];
11811181-fileSystems."/".device = "/dev/mapper/crypted";
11821182-</programlisting>
11831183-11841184-</para>
11851185-11861186-</section>
11871187-11881188-</section>
11891189-11901190-11911191-<!--===============================================================-->
11921192-11931193-<section xml:id="sec-x11"><title>X Window System</title>
11941194-11951195-<para>The X Window System (X11) provides the basis of NixOS’ graphical
11961196-user interface. It can be enabled as follows:
11971197-<programlisting>
11981198-services.xserver.enable = true;
11991199-</programlisting>
12001200-The X server will automatically detect and use the appropriate video
12011201-driver from a set of X.org drivers (such as <literal>vesa</literal>
12021202-and <literal>intel</literal>). You can also specify a driver
12031203-manually, e.g.
12041204-<programlisting>
12051205-services.xserver.videoDrivers = [ "r128" ];
12061206-</programlisting>
12071207-to enable X.org’s <literal>xf86-video-r128</literal> driver.</para>
12081208-12091209-<para>You also need to enable at least one desktop or window manager.
12101210-Otherwise, you can only log into a plain undecorated
12111211-<command>xterm</command> window. Thus you should pick one or more of
12121212-the following lines:
12131213-<programlisting>
12141214-services.xserver.desktopManager.kde4.enable = true;
12151215-services.xserver.desktopManager.xfce.enable = true;
12161216-services.xserver.windowManager.xmonad.enable = true;
12171217-services.xserver.windowManager.twm.enable = true;
12181218-services.xserver.windowManager.icewm.enable = true;
12191219-</programlisting>
12201220-</para>
12211221-12221222-<para>NixOS’s default <emphasis>display manager</emphasis> (the
12231223-program that provides a graphical login prompt and manages the X
12241224-server) is SLiM. You can select KDE’s <command>kdm</command> instead:
12251225-<programlisting>
12261226-services.xserver.displayManager.kdm.enable = true;
12271227-</programlisting>
12281228-</para>
12291229-12301230-<para>The X server is started automatically at boot time. If you
12311231-don’t want this to happen, you can set:
12321232-<programlisting>
12331233-services.xserver.autorun = false;
12341234-</programlisting>
12351235-The X server can then be started manually:
12361236-<screen>
12371237-$ systemctl start display-manager.service
12381238-</screen>
12391239-</para>
12401240-12411241-12421242-<section><title>NVIDIA graphics cards</title>
12431243-12441244-<para>NVIDIA provides a proprietary driver for its graphics cards that
12451245-has better 3D performance than the X.org drivers. It is not enabled
12461246-by default because it’s not free software. You can enable it as follows:
12471247-<programlisting>
12481248-services.xserver.videoDrivers = [ "nvidia" ];
12491249-</programlisting>
12501250-You may need to reboot after enabling this driver to prevent a clash
12511251-with other kernel modules.</para>
12521252-12531253-<para>On 64-bit systems, if you want full acceleration for 32-bit
12541254-programs such as Wine, you should also set the following:
12551255-<programlisting>
12561256-services.xserver.driSupport32Bit = true;
12571257-</programlisting>
12581258-</para>
12591259-12601260-</section>
12611261-12621262-12631263-<section><title>Touchpads</title>
12641264-12651265-<para>Support for Synaptics touchpads (found in many laptops such as
12661266-the Dell Latitude series) can be enabled as follows:
12671267-<programlisting>
12681268-services.xserver.synaptics.enable = true;
12691269-</programlisting>
12701270-The driver has many options (see <xref linkend="ch-options"/>). For
12711271-instance, the following enables two-finger scrolling:
12721272-<programlisting>
12731273-services.xserver.synaptics.twoFingerScroll = true;
12741274-</programlisting>
12751275-</para>
12761276-12771277-</section>
12781278-12791279-12801280-</section>
12811281-12821282-12831283-<!--===============================================================-->
12841284-12851285-<section xml:id="sec-networking"><title>Networking</title>
12861286-12871287-<section xml:id="sec-networkmanager"><title>NetworkManager</title>
12881288-12891289-<para>To facilitate network configuration, some desktop environments
12901290-use NetworkManager. You can enable NetworkManager by setting:
12911291-12921292-<programlisting>
12931293-services.networkmanager.enable = true;
12941294-</programlisting>
12951295-12961296-Some desktop managers (e.g., GNOME) enable NetworkManager
12971297-automatically for you.</para>
12981298-12991299-<para>All users that should have permission to change network settings
13001300-must belong to the <code>networkmanager</code> group.</para>
13011301-13021302-<note><para><code>services.networkmanager</code> and
13031303-<code>services.wireless</code> can not be enabled at the same time:
13041304-you can still connect to the wireless networks using
13051305-NetworkManager.</para></note>
13061306-13071307-</section>
13081308-13091309-<section xml:id="sec-ssh"><title>Secure shell access</title>
13101310-13111311-<para>Secure shell (SSH) access to your machine can be enabled by
13121312-setting:
13131313-13141314-<programlisting>
13151315-services.openssh.enable = true;
13161316-</programlisting>
13171317-13181318-By default, root logins using a password are disallowed. They can be
13191319-disabled entirely by setting
13201320-<literal>services.openssh.permitRootLogin</literal> to
13211321-<literal>"no"</literal>.</para>
13221322-13231323-<para>You can declaratively specify authorised RSA/DSA public keys for
13241324-a user as follows:
13251325-13261326-<!-- FIXME: this might not work if the user is unmanaged. -->
13271327-<programlisting>
13281328-users.extraUsers.alice.openssh.authorizedKeys.keys =
13291329- [ "ssh-dss AAAAB3NzaC1kc3MAAACBAPIkGWVEt4..." ];
13301330-</programlisting>
13311331-13321332-</para>
13331333-13341334-</section>
13351335-13361336-13371337-<section xml:id="sec-ipv4"><title>IPv4 configuration</title>
13381338-13391339-<para>By default, NixOS uses DHCP (specifically,
13401340-<command>dhcpcd</command>) to automatically configure network
13411341-interfaces. However, you can configure an interface manually as
13421342-follows:
13431343-13441344-<programlisting>
13451345-networking.interfaces.eth0 = { ipAddress = "192.168.1.2"; prefixLength = 24; };
13461346-</programlisting>
13471347-13481348-(The network prefix can also be specified using the option
13491349-<literal>subnetMask</literal>,
13501350-e.g. <literal>"255.255.255.0"</literal>, but this is deprecated.)
13511351-Typically you’ll also want to set a default gateway and set of name
13521352-servers:
13531353-13541354-<programlisting>
13551355-networking.defaultGateway = "192.168.1.1";
13561356-networking.nameservers = [ "8.8.8.8" ];
13571357-</programlisting>
13581358-13591359-</para>
13601360-13611361-<note><para>Statically configured interfaces are set up by the systemd
13621362-service
13631363-<replaceable>interface-name</replaceable><literal>-cfg.service</literal>.
13641364-The default gateway and name server configuration is performed by
13651365-<literal>network-setup.service</literal>.</para></note>
13661366-13671367-<para>The host name is set using <option>networking.hostName</option>:
13681368-13691369-<programlisting>
13701370-networking.hostName = "cartman";
13711371-</programlisting>
13721372-13731373-The default host name is <literal>nixos</literal>. Set it to the
13741374-empty string (<literal>""</literal>) to allow the DHCP server to
13751375-provide the host name.</para>
13761376-13771377-</section>
13781378-13791379-13801380-<section xml:id="sec-ipv6"><title>IPv6 configuration</title>
13811381-13821382-<para>IPv6 is enabled by default. Stateless address autoconfiguration
13831383-is used to automatically assign IPv6 addresses to all interfaces. You
13841384-can disable IPv6 support globally by setting:
13851385-13861386-<programlisting>
13871387-networking.enableIPv6 = false;
13881388-</programlisting>
13891389-13901390-</para>
13911391-13921392-</section>
13931393-13941394-13951395-<section xml:id="sec-firewall"><title>Firewall</title>
13961396-13971397-<para>NixOS has a simple stateful firewall that blocks incoming
13981398-connections and other unexpected packets. The firewall applies to
13991399-both IPv4 and IPv6 traffic. It is enabled by default. It can be
14001400-disabled as follows:
14011401-14021402-<programlisting>
14031403-networking.firewall.enable = false;
14041404-</programlisting>
14051405-14061406-If the firewall is enabled, you can open specific TCP ports to the
14071407-outside world:
14081408-14091409-<programlisting>
14101410-networking.firewall.allowedTCPPorts = [ 80 443 ];
14111411-</programlisting>
14121412-14131413-Note that TCP port 22 (ssh) is opened automatically if the SSH daemon
14141414-is enabled (<option>services.openssh.enable = true</option>). UDP
14151415-ports can be opened through
14161416-<option>networking.firewall.allowedUDPPorts</option>. Also of
14171417-interest is
14181418-14191419-<programlisting>
14201420-networking.firewall.allowPing = true;
14211421-</programlisting>
14221422-14231423-to allow the machine to respond to ping requests. (ICMPv6 pings are
14241424-always allowed.)</para>
14251425-14261426-</section>
14271427-14281428-14291429-<section xml:id="sec-wireless"><title>Wireless networks</title>
14301430-14311431-<para>For a desktop installation using NetworkManager (e.g., GNOME),
14321432-you just have to make sure the user is in the
14331433-<code>networkmanager</code> group and you can skip the rest of this
14341434-section on wireless networks.</para>
14351435-14361436-<para>
14371437-NixOS will start wpa_supplicant for you if you enable this setting:
14381438-14391439-<programlisting>
14401440-networking.wireless.enable = true;
14411441-</programlisting>
14421442-14431443-NixOS currently does not generate wpa_supplicant's
14441444-configuration file, <literal>/etc/wpa_supplicant.conf</literal>. You should edit this file
14451445-yourself to define wireless networks, WPA keys and so on (see
14461446-wpa_supplicant.conf(5)).
14471447-</para>
14481448-14491449-<para>
14501450-If you are using WPA2 the <command>wpa_passphrase</command> tool might be useful
14511451-to generate the <literal>wpa_supplicant.conf</literal>.
14521452-14531453-<screen>
14541454-$ wpa_passphrase ESSID PSK > /etc/wpa_supplicant.conf</screen>
14551455-14561456-After you have edited the <literal>wpa_supplicant.conf</literal>,
14571457-you need to restart the wpa_supplicant service.
14581458-14591459-<screen>
14601460-$ systemctl restart wpa_supplicant.service</screen>
14611461-</para>
14621462-14631463-14641464-</section>
14651465-14661466-14671467-<section><title>Ad-hoc configuration</title>
14681468-14691469-<para>You can use <option>networking.localCommands</option> to specify
14701470-shell commands to be run at the end of
14711471-<literal>network-setup.service</literal>. This is useful for doing
14721472-network configuration not covered by the existing NixOS modules. For
14731473-instance, to statically configure an IPv6 address:
14741474-14751475-<programlisting>
14761476-networking.localCommands =
14771477- ''
14781478- ip -6 addr add 2001:610:685:1::1/64 dev eth0
14791479- '';
14801480-</programlisting>
14811481-14821482-</para>
14831483-14841484-</section>
14851485-14861486-14871487-<!-- TODO: OpenVPN, NAT -->
14881488-14891489-14901490-</section>
14911491-14921492-14931493-<!--===============================================================-->
14941494-14951495-<section xml:id="sec-kernel-config"><title>Linux kernel</title>
14961496-14971497-<para>You can override the Linux kernel and associated packages using
14981498-the option <option>boot.kernelPackages</option>. For instance, this
14991499-selects the Linux 3.10 kernel:
15001500-<programlisting>
15011501-boot.kernelPackages = pkgs.linuxPackages_3_10;
15021502-</programlisting>
15031503-Note that this not only replaces the kernel, but also packages that
15041504-are specific to the kernel version, such as the NVIDIA video drivers.
15051505-This ensures that driver packages are consistent with the
15061506-kernel.</para>
15071507-15081508-<para>The default Linux kernel configuration should be fine for most users. You can see the configuration of your current kernel with the following command:
15091509-<programlisting>
15101510-cat /proc/config.gz | gunzip
15111511-</programlisting>
15121512-If you want to change the kernel configuration, you can use the
15131513-<option>packageOverrides</option> feature (see <xref
15141514-linkend="sec-customising-packages" />). For instance, to enable
15151515-support for the kernel debugger KGDB:
15161516-15171517-<programlisting>
15181518-nixpkgs.config.packageOverrides = pkgs:
15191519- { linux_3_4 = pkgs.linux_3_4.override {
15201520- extraConfig =
15211521- ''
15221522- KGDB y
15231523- '';
15241524- };
15251525- };
15261526-</programlisting>
15271527-15281528-<varname>extraConfig</varname> takes a list of Linux kernel
15291529-configuration options, one per line. The name of the option should
15301530-not include the prefix <literal>CONFIG_</literal>. The option value
15311531-is typically <literal>y</literal>, <literal>n</literal> or
15321532-<literal>m</literal> (to build something as a kernel module).</para>
15331533-15341534-<para>Kernel modules for hardware devices are generally loaded
15351535-automatically by <command>udev</command>. You can force a module to
15361536-be loaded via <option>boot.kernelModules</option>, e.g.
15371537-<programlisting>
15381538-boot.kernelModules = [ "fuse" "kvm-intel" "coretemp" ];
15391539-</programlisting>
15401540-If the module is required early during the boot (e.g. to mount the
15411541-root file system), you can use
15421542-<option>boot.initrd.extraKernelModules</option>:
15431543-<programlisting>
15441544-boot.initrd.extraKernelModules = [ "cifs" ];
15451545-</programlisting>
15461546-This causes the specified modules and their dependencies to be added
15471547-to the initial ramdark.</para>
15481548-15491549-<para>Kernel runtime parameters can be set through
15501550-<option>boot.kernel.sysctl</option>, e.g.
15511551-<programlisting>
15521552-boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 120;
15531553-</programlisting>
15541554-sets the kernel’s TCP keepalive time to 120 seconds. To see the
15551555-available parameters, run <command>sysctl -a</command>.</para>
15561556-15571557-</section>
15581558-15591559-15601560-<!-- Apache; libvirtd virtualisation -->
15611561-15621562-15631563-</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-luks-file-systems">
66+77+<title>LUKS-Encrypted File Systems</title>
88+99+<para>NixOS supports file systems that are encrypted using
1010+<emphasis>LUKS</emphasis> (Linux Unified Key Setup). For example,
1111+here is how you create an encrypted Ext4 file system on the device
1212+<filename>/dev/sda2</filename>:
1313+1414+<screen>
1515+$ cryptsetup luksFormat /dev/sda2
1616+1717+WARNING!
1818+========
1919+This will overwrite data on /dev/sda2 irrevocably.
2020+2121+Are you sure? (Type uppercase yes): YES
2222+Enter LUKS passphrase: ***
2323+Verify passphrase: ***
2424+2525+$ cryptsetup luksOpen /dev/sda2 crypted
2626+Enter passphrase for /dev/sda2: ***
2727+2828+$ mkfs.ext4 /dev/mapper/crypted
2929+</screen>
3030+3131+To ensure that this file system is automatically mounted at boot time
3232+as <filename>/</filename>, add the following to
3333+<filename>configuration.nix</filename>:
3434+3535+<programlisting>
3636+boot.initrd.luks.devices = [ { device = "/dev/sda2"; name = "crypted"; } ];
3737+fileSystems."/".device = "/dev/mapper/crypted";
3838+</programlisting>
3939+4040+</para>
4141+4242+</section>
+166
nixos/doc/manual/configuration/abstractions.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-module-abstractions">
66+77+<title>Abstractions</title>
88+99+<para>If you find yourself repeating yourself over and over, it’s time
1010+to abstract. Take, for instance, this Apache HTTP Server configuration:
1111+1212+<programlisting>
1313+{
1414+ services.httpd.virtualHosts =
1515+ [ { hostName = "example.org";
1616+ documentRoot = "/webroot";
1717+ adminAddr = "alice@example.org";
1818+ enableUserDir = true;
1919+ }
2020+ { hostName = "example.org";
2121+ documentRoot = "/webroot";
2222+ adminAddr = "alice@example.org";
2323+ enableUserDir = true;
2424+ enableSSL = true;
2525+ sslServerCert = "/root/ssl-example-org.crt";
2626+ sslServerKey = "/root/ssl-example-org.key";
2727+ }
2828+ ];
2929+}
3030+</programlisting>
3131+3232+It defines two virtual hosts with nearly identical configuration; the
3333+only difference is that the second one has SSL enabled. To prevent
3434+this duplication, we can use a <literal>let</literal>:
3535+3636+<programlisting>
3737+let
3838+ exampleOrgCommon =
3939+ { hostName = "example.org";
4040+ documentRoot = "/webroot";
4141+ adminAddr = "alice@example.org";
4242+ enableUserDir = true;
4343+ };
4444+in
4545+{
4646+ services.httpd.virtualHosts =
4747+ [ exampleOrgCommon
4848+ (exampleOrgCommon // {
4949+ enableSSL = true;
5050+ sslServerCert = "/root/ssl-example-org.crt";
5151+ sslServerKey = "/root/ssl-example-org.key";
5252+ })
5353+ ];
5454+}
5555+</programlisting>
5656+5757+The <literal>let exampleOrgCommon =
5858+<replaceable>...</replaceable></literal> defines a variable named
5959+<literal>exampleOrgCommon</literal>. The <literal>//</literal>
6060+operator merges two attribute sets, so the configuration of the second
6161+virtual host is the set <literal>exampleOrgCommon</literal> extended
6262+with the SSL options.</para>
6363+6464+<para>You can write a <literal>let</literal> wherever an expression is
6565+allowed. Thus, you also could have written:
6666+6767+<programlisting>
6868+{
6969+ services.httpd.virtualHosts =
7070+ let exampleOrgCommon = <replaceable>...</replaceable>; in
7171+ [ exampleOrgCommon
7272+ (exampleOrgCommon // { <replaceable>...</replaceable> })
7373+ ];
7474+}
7575+</programlisting>
7676+7777+but not <literal>{ let exampleOrgCommon =
7878+<replaceable>...</replaceable>; in <replaceable>...</replaceable>;
7979+}</literal> since attributes (as opposed to attribute values) are not
8080+expressions.</para>
8181+8282+<para><emphasis>Functions</emphasis> provide another method of
8383+abstraction. For instance, suppose that we want to generate lots of
8484+different virtual hosts, all with identical configuration except for
8585+the host name. This can be done as follows:
8686+8787+<programlisting>
8888+{
8989+ services.httpd.virtualHosts =
9090+ let
9191+ makeVirtualHost = name:
9292+ { hostName = name;
9393+ documentRoot = "/webroot";
9494+ adminAddr = "alice@example.org";
9595+ };
9696+ in
9797+ [ (makeVirtualHost "example.org")
9898+ (makeVirtualHost "example.com")
9999+ (makeVirtualHost "example.gov")
100100+ (makeVirtualHost "example.nl")
101101+ ];
102102+}
103103+</programlisting>
104104+105105+Here, <varname>makeVirtualHost</varname> is a function that takes a
106106+single argument <literal>name</literal> and returns the configuration
107107+for a virtual host. That function is then called for several names to
108108+produce the list of virtual host configurations.</para>
109109+110110+<para>We can further improve on this by using the function
111111+<varname>map</varname>, which applies another function to every
112112+element in a list:
113113+114114+<programlisting>
115115+{
116116+ services.httpd.virtualHosts =
117117+ let
118118+ makeVirtualHost = <replaceable>...</replaceable>;
119119+ in map makeVirtualHost
120120+ [ "example.org" "example.com" "example.gov" "example.nl" ];
121121+}
122122+</programlisting>
123123+124124+(The function <literal>map</literal> is called a
125125+<emphasis>higher-order function</emphasis> because it takes another
126126+function as an argument.)</para>
127127+128128+<para>What if you need more than one argument, for instance, if we
129129+want to use a different <literal>documentRoot</literal> for each
130130+virtual host? Then we can make <varname>makeVirtualHost</varname> a
131131+function that takes a <emphasis>set</emphasis> as its argument, like this:
132132+133133+<programlisting>
134134+{
135135+ services.httpd.virtualHosts =
136136+ let
137137+ makeVirtualHost = { name, root }:
138138+ { hostName = name;
139139+ documentRoot = root;
140140+ adminAddr = "alice@example.org";
141141+ };
142142+ in map makeVirtualHost
143143+ [ { name = "example.org"; root = "/sites/example.org"; }
144144+ { name = "example.com"; root = "/sites/example.com"; }
145145+ { name = "example.gov"; root = "/sites/example.gov"; }
146146+ { name = "example.nl"; root = "/sites/example.nl"; }
147147+ ];
148148+}
149149+</programlisting>
150150+151151+But in this case (where every root is a subdirectory of
152152+<filename>/sites</filename> named after the virtual host), it would
153153+have been shorter to define <varname>makeVirtualHost</varname> as
154154+<programlisting>
155155+makeVirtualHost = name:
156156+ { hostName = name;
157157+ documentRoot = "/sites/${name}";
158158+ adminAddr = "alice@example.org";
159159+ };
160160+</programlisting>
161161+162162+Here, the construct
163163+<literal>${<replaceable>...</replaceable>}</literal> allows the result
164164+of an expression to be spliced into a string.</para>
165165+166166+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ad-hoc-network-config">
66+77+<title>Ad-Hoc Configuration</title>
88+99+<para>You can use <option>networking.localCommands</option> to specify
1010+shell commands to be run at the end of
1111+<literal>network-setup.service</literal>. This is useful for doing
1212+network configuration not covered by the existing NixOS modules. For
1313+instance, to statically configure an IPv6 address:
1414+1515+<programlisting>
1616+networking.localCommands =
1717+ ''
1818+ ip -6 addr add 2001:610:685:1::1/64 dev eth0
1919+ '';
2020+</programlisting>
2121+2222+</para>
2323+2424+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-ad-hoc-packages">
66+77+<title>Ad-Hoc Package Management</title>
88+99+<para>With the command <command>nix-env</command>, you can install and
1010+uninstall packages from the command line. For instance, to install
1111+Mozilla Thunderbird:
1212+1313+<screen>
1414+$ nix-env -iA nixos.pkgs.thunderbird</screen>
1515+1616+If you invoke this as root, the package is installed in the Nix
1717+profile <filename>/nix/var/nix/profiles/default</filename> and visible
1818+to all users of the system; otherwise, the package ends up in
1919+<filename>/nix/var/nix/profiles/per-user/<replaceable>username</replaceable>/profile</filename>
2020+and is not visible to other users. The <option>-A</option> flag
2121+specifies the package by its attribute name; without it, the package
2222+is installed by matching against its package name
2323+(e.g. <literal>thunderbird</literal>). The latter is slower because
2424+it requires matching against all available Nix packages, and is
2525+ambiguous if there are multiple matching packages.</para>
2626+2727+<para>Packages come from the NixOS channel. You typically upgrade a
2828+package by updating to the latest version of the NixOS channel:
2929+<screen>
3030+$ nix-channel --update nixos
3131+</screen>
3232+and then running <literal>nix-env -i</literal> again. Other packages
3333+in the profile are <emphasis>not</emphasis> affected; this is the
3434+crucial difference with the declarative style of package management,
3535+where running <command>nixos-rebuild switch</command> causes all
3636+packages to be updated to their current versions in the NixOS channel.
3737+You can however upgrade all packages for which there is a newer
3838+version by doing:
3939+<screen>
4040+$ nix-env -u '*'
4141+</screen>
4242+</para>
4343+4444+<para>A package can be uninstalled using the <option>-e</option>
4545+flag:
4646+<screen>
4747+$ nix-env -e thunderbird
4848+</screen>
4949+</para>
5050+5151+<para>Finally, you can roll back an undesirable
5252+<command>nix-env</command> action:
5353+<screen>
5454+$ nix-env --rollback
5555+</screen>
5656+</para>
5757+5858+<para><command>nix-env</command> has many more flags. For details,
5959+see the
6060+<citerefentry><refentrytitle>nix-env</refentrytitle><manvolnum>1</manvolnum></citerefentry>
6161+manpage or the Nix manual.</para>
6262+6363+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-custom-packages">
66+77+<title>Adding Custom Packages</title>
88+99+<para>It’s possible that a package you need is not available in NixOS.
1010+In that case, you can do two things. First, you can clone the Nixpkgs
1111+repository, add the package to your clone, and (optionally) submit a
1212+patch or pull request to have it accepted into the main Nixpkgs
1313+repository. This is described in detail in the <link
1414+xlink:href="http://nixos.org/nixpkgs/manual">Nixpkgs manual</link>.
1515+In short, you clone Nixpkgs:
1616+1717+<screen>
1818+$ git clone git://github.com/NixOS/nixpkgs.git
1919+$ cd nixpkgs
2020+</screen>
2121+2222+Then you write and test the package as described in the Nixpkgs
2323+manual. Finally, you add it to
2424+<literal>environment.systemPackages</literal>, e.g.
2525+2626+<programlisting>
2727+environment.systemPackages = [ pkgs.my-package ];
2828+</programlisting>
2929+3030+and you run <command>nixos-rebuild</command>, specifying your own
3131+Nixpkgs tree:
3232+3333+<screen>
3434+$ nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs</screen>
3535+3636+</para>
3737+3838+<para>The second possibility is to add the package outside of the
3939+Nixpkgs tree. For instance, here is how you specify a build of the
4040+<link xlink:href="http://www.gnu.org/software/hello/">GNU Hello</link>
4141+package directly in <filename>configuration.nix</filename>:
4242+4343+<programlisting>
4444+environment.systemPackages =
4545+ let
4646+ my-hello = with pkgs; stdenv.mkDerivation rec {
4747+ name = "hello-2.8";
4848+ src = fetchurl {
4949+ url = "mirror://gnu/hello/${name}.tar.gz";
5050+ sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
5151+ };
5252+ };
5353+ in
5454+ [ my-hello ];
5555+</programlisting>
5656+5757+Of course, you can also move the definition of
5858+<literal>my-hello</literal> into a separate Nix expression, e.g.
5959+<programlisting>
6060+environment.systemPackages = [ (import ./my-hello.nix) ];
6161+</programlisting>
6262+where <filename>my-hello.nix</filename> contains:
6363+<programlisting>
6464+with import <nixpkgs> {}; # bring all of Nixpkgs into scope
6565+6666+stdenv.mkDerivation rec {
6767+ name = "hello-2.8";
6868+ src = fetchurl {
6969+ url = "mirror://gnu/hello/${name}.tar.gz";
7070+ sha256 = "0wqd8sjmxfskrflaxywc7gqw7sfawrfvdxd9skxawzfgyy0pzdz6";
7171+ };
7272+}
7373+</programlisting>
7474+7575+This allows testing the package easily:
7676+<screen>
7777+$ nix-build my-hello.nix
7878+$ ./result/bin/hello
7979+Hello, world!
8080+</screen>
8181+8282+</para>
8383+8484+</section>
+213
nixos/doc/manual/configuration/config-file.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-configuration-file">
66+77+<title>NixOS Configuration File</title>
88+99+<para>The NixOS configuration file generally looks like this:
1010+1111+<programlisting>
1212+{ config, pkgs, ... }:
1313+1414+{ <replaceable>option definitions</replaceable>
1515+}
1616+</programlisting>
1717+1818+The first line (<literal>{ config, pkgs, ... }:</literal>) denotes
1919+that this is actually a function that takes at least the two arguments
2020+ <varname>config</varname> and <varname>pkgs</varname>. (These are
2121+explained later.) The function returns a <emphasis>set</emphasis> of
2222+option definitions (<literal>{ <replaceable>...</replaceable> }</literal>). These definitions have the
2323+form <literal><replaceable>name</replaceable> =
2424+<replaceable>value</replaceable></literal>, where
2525+<replaceable>name</replaceable> is the name of an option and
2626+<replaceable>value</replaceable> is its value. For example,
2727+2828+<programlisting>
2929+{ config, pkgs, ... }:
3030+3131+{ services.httpd.enable = true;
3232+ services.httpd.adminAddr = "alice@example.org";
3333+ services.httpd.documentRoot = "/webroot";
3434+}
3535+</programlisting>
3636+3737+defines a configuration with three option definitions that together
3838+enable the Apache HTTP Server with <filename>/webroot</filename> as
3939+the document root.</para>
4040+4141+<para>Sets can be nested, and in fact dots in option names are
4242+shorthand for defining a set containing another set. For instance,
4343+<option>services.httpd.enable</option> defines a set named
4444+<varname>services</varname> that contains a set named
4545+<varname>httpd</varname>, which in turn contains an option definition
4646+named <varname>enable</varname> with value <literal>true</literal>.
4747+This means that the example above can also be written as:
4848+4949+<programlisting>
5050+{ config, pkgs, ... }:
5151+5252+{ services = {
5353+ httpd = {
5454+ enable = true;
5555+ adminAddr = "alice@example.org";
5656+ documentRoot = "/webroot";
5757+ };
5858+ };
5959+}
6060+</programlisting>
6161+6262+which may be more convenient if you have lots of option definitions
6363+that share the same prefix (such as
6464+<literal>services.httpd</literal>).</para>
6565+6666+<para>NixOS checks your option definitions for correctness. For
6767+instance, if you try to define an option that doesn’t exist (that is,
6868+doesn’t have a corresponding <emphasis>option declaration</emphasis>),
6969+<command>nixos-rebuild</command> will give an error like:
7070+<screen>
7171+The option `services.httpd.enabl' defined in `/etc/nixos/configuration.nix' does not exist.
7272+</screen>
7373+Likewise, values in option definitions must have a correct type. For
7474+instance, <option>services.httpd.enable</option> must be a Boolean
7575+(<literal>true</literal> or <literal>false</literal>). Trying to give
7676+it a value of another type, such as a string, will cause an error:
7777+<screen>
7878+The option value `services.httpd.enable' in `/etc/nixos/configuration.nix' is not a boolean.
7979+</screen>
8080+8181+</para>
8282+8383+<para>Options have various types of values. The most important are:
8484+8585+<variablelist>
8686+ <varlistentry>
8787+ <term>Strings</term>
8888+ <listitem>
8989+ <para>Strings are enclosed in double quotes, e.g.
9090+9191+<programlisting>
9292+networking.hostName = "dexter";
9393+</programlisting>
9494+9595+ Special characters can be escaped by prefixing them with a
9696+ backslash (e.g. <literal>\"</literal>).</para>
9797+9898+ <para>Multi-line strings can be enclosed in <emphasis>double
9999+ single quotes</emphasis>, e.g.
100100+101101+<programlisting>
102102+networking.extraHosts =
103103+ ''
104104+ 127.0.0.2 other-localhost
105105+ 10.0.0.1 server
106106+ '';
107107+</programlisting>
108108+109109+ The main difference is that preceding whitespace is
110110+ automatically stripped from each line, and that characters like
111111+ <literal>"</literal> and <literal>\</literal> are not special
112112+ (making it more convenient for including things like shell
113113+ code).</para>
114114+ </listitem>
115115+ </varlistentry>
116116+117117+ <varlistentry>
118118+ <term>Booleans</term>
119119+ <listitem>
120120+ <para>These can be <literal>true</literal> or
121121+ <literal>false</literal>, e.g.
122122+123123+<programlisting>
124124+networking.firewall.enable = true;
125125+networking.firewall.allowPing = false;
126126+</programlisting>
127127+ </para>
128128+ </listitem>
129129+ </varlistentry>
130130+131131+ <varlistentry>
132132+ <term>Integers</term>
133133+ <listitem>
134134+ <para>For example,
135135+136136+<programlisting>
137137+boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 60;
138138+</programlisting>
139139+140140+ (Note that here the attribute name
141141+ <literal>net.ipv4.tcp_keepalive_time</literal> is enclosed in
142142+ quotes to prevent it from being interpreted as a set named
143143+ <literal>net</literal> containing a set named
144144+ <literal>ipv4</literal>, and so on. This is because it’s not a
145145+ NixOS option but the literal name of a Linux kernel
146146+ setting.)</para>
147147+ </listitem>
148148+ </varlistentry>
149149+150150+ <varlistentry>
151151+ <term>Sets</term>
152152+ <listitem>
153153+ <para>Sets were introduced above. They are name/value pairs
154154+ enclosed in braces, as in the option definition
155155+156156+<programlisting>
157157+fileSystems."/boot" =
158158+ { device = "/dev/sda1";
159159+ fsType = "ext4";
160160+ options = "rw,data=ordered,relatime";
161161+ };
162162+</programlisting>
163163+ </para>
164164+ </listitem>
165165+ </varlistentry>
166166+167167+ <varlistentry>
168168+ <term>Lists</term>
169169+ <listitem>
170170+ <para>The important thing to note about lists is that list
171171+ elements are separated by whitespace, like this:
172172+173173+<programlisting>
174174+boot.kernelModules = [ "fuse" "kvm-intel" "coretemp" ];
175175+</programlisting>
176176+177177+ List elements can be any other type, e.g. sets:
178178+179179+<programlisting>
180180+swapDevices = [ { device = "/dev/disk/by-label/swap"; } ];
181181+</programlisting>
182182+ </para>
183183+ </listitem>
184184+ </varlistentry>
185185+186186+ <varlistentry>
187187+ <term>Packages</term>
188188+ <listitem>
189189+ <para>Usually, the packages you need are already part of the Nix
190190+ Packages collection, which is a set that can be accessed through
191191+ the function argument <varname>pkgs</varname>. Typical uses:
192192+193193+<programlisting>
194194+environment.systemPackages =
195195+ [ pkgs.thunderbird
196196+ pkgs.emacs
197197+ ];
198198+199199+postgresql.package = pkgs.postgresql90;
200200+</programlisting>
201201+202202+ The latter option definition changes the default PostgreSQL
203203+ package used by NixOS’s PostgreSQL service to 9.0. For more
204204+ information on packages, including how to add new ones, see
205205+ <xref linkend="sec-custom-packages"/>.</para>
206206+ </listitem>
207207+ </varlistentry>
208208+209209+</variablelist>
210210+211211+</para>
212212+213213+</section>
+27
nixos/doc/manual/configuration/config-syntax.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-configuration-syntax">
66+77+<title>Configuration Syntax</title>
88+99+<para>The NixOS configuration file
1010+<filename>/etc/nixos/configuration.nix</filename> is actually a
1111+<emphasis>Nix expression</emphasis>, which is the Nix package
1212+manager’s purely functional language for describing how to build
1313+packages and configurations. This means you have all the expressive
1414+power of that language at your disposal, including the ability to
1515+abstract over common patterns, which is very useful when managing
1616+complex systems. The syntax and semantics of the Nix language are
1717+fully described in the <link
1818+xlink:href="http://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix
1919+manual</link>, but here we give a short overview of the most important
2020+constructs useful in NixOS configuration files.</para>
2121+2222+<xi:include href="config-file.xml" />
2323+<xi:include href="abstractions.xml" />
2424+<xi:include href="modularity.xml" />
2525+<xi:include href="summary.xml" />
2626+2727+</chapter>
+29
nixos/doc/manual/configuration/configuration.xml
···11+<part xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-configuration">
66+77+<title>Configuration</title>
88+99+<partintro>
1010+1111+<para>This chapter describes how to configure various aspects of a
1212+NixOS machine through the configuration file
1313+<filename>/etc/nixos/configuration.nix</filename>. As described in
1414+<xref linkend="sec-changing-config" />, changes to this file only take
1515+effect after you run <command>nixos-rebuild</command>.</para>
1616+1717+</partintro>
1818+1919+<xi:include href="config-syntax.xml" />
2020+<xi:include href="package-mgmt.xml" />
2121+<xi:include href="user-mgmt.xml" />
2222+<xi:include href="file-systems.xml" />
2323+<xi:include href="x-windows.xml" />
2424+<xi:include href="networking.xml" />
2525+<xi:include href="linux-kernel.xml" />
2626+2727+<!-- Apache; libvirtd virtualisation -->
2828+2929+</part>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-customising-packages">
66+77+<title>Customising Packages</title>
88+99+<para>Some packages in Nixpkgs have options to enable or disable
1010+optional functionality or change other aspects of the package. For
1111+instance, the Firefox wrapper package (which provides Firefox with a
1212+set of plugins such as the Adobe Flash player) has an option to enable
1313+the Google Talk plugin. It can be set in
1414+<filename>configuration.nix</filename> as follows:
1515+1616+<filename>
1717+nixpkgs.config.firefox.enableGoogleTalkPlugin = true;
1818+</filename>
1919+</para>
2020+2121+<warning><para>Unfortunately, Nixpkgs currently lacks a way to query
2222+available configuration options.</para></warning>
2323+2424+<para>Apart from high-level options, it’s possible to tweak a package
2525+in almost arbitrary ways, such as changing or disabling dependencies
2626+of a package. For instance, the Emacs package in Nixpkgs by default
2727+has a dependency on GTK+ 2. If you want to build it against GTK+ 3,
2828+you can specify that as follows:
2929+3030+<programlisting>
3131+environment.systemPackages = [ (pkgs.emacs.override { gtk = pkgs.gtk3; }) ];
3232+</programlisting>
3333+3434+The function <varname>override</varname> performs the call to the Nix
3535+function that produces Emacs, with the original arguments amended by
3636+the set of arguments specified by you. So here the function argument
3737+<varname>gtk</varname> gets the value <literal>pkgs.gtk3</literal>,
3838+causing Emacs to depend on GTK+ 3. (The parentheses are necessary
3939+because in Nix, function application binds more weakly than list
4040+construction, so without them,
4141+<literal>environment.systemPackages</literal> would be a list with two
4242+elements.)</para>
4343+4444+<para>Even greater customisation is possible using the function
4545+<varname>overrideDerivation</varname>. While the
4646+<varname>override</varname> mechanism above overrides the arguments of
4747+a package function, <varname>overrideDerivation</varname> allows
4848+changing the <emphasis>result</emphasis> of the function. This
4949+permits changing any aspect of the package, such as the source code.
5050+For instance, if you want to override the source code of Emacs, you
5151+can say:
5252+5353+<programlisting>
5454+environment.systemPackages =
5555+ [ (pkgs.lib.overrideDerivation pkgs.emacs (attrs: {
5656+ name = "emacs-25.0-pre";
5757+ src = /path/to/my/emacs/tree;
5858+ }))
5959+ ];
6060+</programlisting>
6161+6262+Here, <varname>overrideDerivation</varname> takes the Nix derivation
6363+specified by <varname>pkgs.emacs</varname> and produces a new
6464+derivation in which the original’s <literal>name</literal> and
6565+<literal>src</literal> attribute have been replaced by the given
6666+values. The original attributes are accessible via
6767+<varname>attrs</varname>.</para>
6868+6969+<para>The overrides shown above are not global. They do not affect
7070+the original package; other packages in Nixpkgs continue to depend on
7171+the original rather than the customised package. This means that if
7272+another package in your system depends on the original package, you
7373+end up with two instances of the package. If you want to have
7474+everything depend on your customised instance, you can apply a
7575+<emphasis>global</emphasis> override as follows:
7676+7777+<screen>
7878+nixpkgs.config.packageOverrides = pkgs:
7979+ { emacs = pkgs.emacs.override { gtk = pkgs.gtk3; };
8080+ };
8181+</screen>
8282+8383+The effect of this definition is essentially equivalent to modifying
8484+the <literal>emacs</literal> attribute in the Nixpkgs source tree.
8585+Any package in Nixpkgs that depends on <literal>emacs</literal> will
8686+be passed your customised instance. (However, the value
8787+<literal>pkgs.emacs</literal> in
8888+<varname>nixpkgs.config.packageOverrides</varname> refers to the
8989+original rather than overridden instance, to prevent an infinite
9090+recursion.)</para>
9191+9292+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-declarative-package-mgmt">
66+77+<title>Declarative Package Management</title>
88+99+<para>With declarative package management, you specify which packages
1010+you want on your system by setting the option
1111+<option>environment.systemPackages</option>. For instance, adding the
1212+following line to <filename>configuration.nix</filename> enables the
1313+Mozilla Thunderbird email application:
1414+1515+<programlisting>
1616+environment.systemPackages = [ pkgs.thunderbird ];
1717+</programlisting>
1818+1919+The effect of this specification is that the Thunderbird package from
2020+Nixpkgs will be built or downloaded as part of the system when you run
2121+<command>nixos-rebuild switch</command>.</para>
2222+2323+<para>You can get a list of the available packages as follows:
2424+<screen>
2525+$ nix-env -qaP '*' --description
2626+nixos.pkgs.firefox firefox-23.0 Mozilla Firefox - the browser, reloaded
2727+<replaceable>...</replaceable>
2828+</screen>
2929+3030+The first column in the output is the <emphasis>attribute
3131+name</emphasis>, such as
3232+<literal>nixos.pkgs.thunderbird</literal>. (The
3333+<literal>nixos</literal> prefix allows distinguishing between
3434+different channels that you might have.)</para>
3535+3636+<para>To “uninstall” a package, simply remove it from
3737+<option>environment.systemPackages</option> and run
3838+<command>nixos-rebuild switch</command>.</para>
3939+4040+<xi:include href="customizing-packages.xml" />
4141+<xi:include href="adding-custom-packages.xml" />
4242+4343+</section>
+40
nixos/doc/manual/configuration/file-systems.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-file-systems">
66+77+<title>File Systems</title>
88+99+<para>You can define file systems using the
1010+<option>fileSystems</option> configuration option. For instance, the
1111+following definition causes NixOS to mount the Ext4 file system on
1212+device <filename>/dev/disk/by-label/data</filename> onto the mount
1313+point <filename>/data</filename>:
1414+1515+<programlisting>
1616+fileSystems."/data" =
1717+ { device = "/dev/disk/by-label/data";
1818+ fsType = "ext4";
1919+ };
2020+</programlisting>
2121+2222+Mount points are created automatically if they don’t already exist.
2323+For <option>device</option>, it’s best to use the topology-independent
2424+device aliases in <filename>/dev/disk/by-label</filename> and
2525+<filename>/dev/disk/by-uuid</filename>, as these don’t change if the
2626+topology changes (e.g. if a disk is moved to another IDE
2727+controller).</para>
2828+2929+<para>You can usually omit the file system type
3030+(<option>fsType</option>), since <command>mount</command> can usually
3131+detect the type and load the necessary kernel module automatically.
3232+However, if the file system is needed at early boot (in the initial
3333+ramdisk) and is not <literal>ext2</literal>, <literal>ext3</literal>
3434+or <literal>ext4</literal>, then it’s best to specify
3535+<option>fsType</option> to ensure that the kernel module is
3636+available.</para>
3737+3838+<xi:include href="LUKS-file-systems.xml" />
3939+4040+</chapter>
+38
nixos/doc/manual/configuration/firewall.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-firewall">
66+77+<title>Firewall</title>
88+99+<para>NixOS has a simple stateful firewall that blocks incoming
1010+connections and other unexpected packets. The firewall applies to
1111+both IPv4 and IPv6 traffic. It is enabled by default. It can be
1212+disabled as follows:
1313+1414+<programlisting>
1515+networking.firewall.enable = false;
1616+</programlisting>
1717+1818+If the firewall is enabled, you can open specific TCP ports to the
1919+outside world:
2020+2121+<programlisting>
2222+networking.firewall.allowedTCPPorts = [ 80 443 ];
2323+</programlisting>
2424+2525+Note that TCP port 22 (ssh) is opened automatically if the SSH daemon
2626+is enabled (<option>services.openssh.enable = true</option>). UDP
2727+ports can be opened through
2828+<option>networking.firewall.allowedUDPPorts</option>. Also of
2929+interest is
3030+3131+<programlisting>
3232+networking.firewall.allowPing = true;
3333+</programlisting>
3434+3535+to allow the machine to respond to ping requests. (ICMPv6 pings are
3636+always allowed.)</para>
3737+3838+</section>
+47
nixos/doc/manual/configuration/ipv4-config.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-ipv4">
66+77+<title>IPv4 Configuration</title>
88+99+<para>By default, NixOS uses DHCP (specifically,
1010+<command>dhcpcd</command>) to automatically configure network
1111+interfaces. However, you can configure an interface manually as
1212+follows:
1313+1414+<programlisting>
1515+networking.interfaces.eth0 = { ipAddress = "192.168.1.2"; prefixLength = 24; };
1616+</programlisting>
1717+1818+(The network prefix can also be specified using the option
1919+<literal>subnetMask</literal>,
2020+e.g. <literal>"255.255.255.0"</literal>, but this is deprecated.)
2121+Typically you’ll also want to set a default gateway and set of name
2222+servers:
2323+2424+<programlisting>
2525+networking.defaultGateway = "192.168.1.1";
2626+networking.nameservers = [ "8.8.8.8" ];
2727+</programlisting>
2828+2929+</para>
3030+3131+<note><para>Statically configured interfaces are set up by the systemd
3232+service
3333+<replaceable>interface-name</replaceable><literal>-cfg.service</literal>.
3434+The default gateway and name server configuration is performed by
3535+<literal>network-setup.service</literal>.</para></note>
3636+3737+<para>The host name is set using <option>networking.hostName</option>:
3838+3939+<programlisting>
4040+networking.hostName = "cartman";
4141+</programlisting>
4242+4343+The default host name is <literal>nixos</literal>. Set it to the
4444+empty string (<literal>""</literal>) to allow the DHCP server to
4545+provide the host name.</para>
4646+4747+</section>
+19
nixos/doc/manual/configuration/ipv6-config.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-ipv6">
66+77+<title>IPv6 Configuration</title>
88+99+<para>IPv6 is enabled by default. Stateless address autoconfiguration
1010+is used to automatically assign IPv6 addresses to all interfaces. You
1111+can disable IPv6 support globally by setting:
1212+1313+<programlisting>
1414+networking.enableIPv6 = false;
1515+</programlisting>
1616+1717+</para>
1818+1919+</section>
+69
nixos/doc/manual/configuration/linux-kernel.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-kernel-config">
66+77+<title>Linux Kernel</title>
88+99+<para>You can override the Linux kernel and associated packages using
1010+the option <option>boot.kernelPackages</option>. For instance, this
1111+selects the Linux 3.10 kernel:
1212+<programlisting>
1313+boot.kernelPackages = pkgs.linuxPackages_3_10;
1414+</programlisting>
1515+Note that this not only replaces the kernel, but also packages that
1616+are specific to the kernel version, such as the NVIDIA video drivers.
1717+This ensures that driver packages are consistent with the
1818+kernel.</para>
1919+2020+<para>The default Linux kernel configuration should be fine for most users. You can see the configuration of your current kernel with the following command:
2121+<programlisting>
2222+cat /proc/config.gz | gunzip
2323+</programlisting>
2424+If you want to change the kernel configuration, you can use the
2525+<option>packageOverrides</option> feature (see <xref
2626+linkend="sec-customising-packages" />). For instance, to enable
2727+support for the kernel debugger KGDB:
2828+2929+<programlisting>
3030+nixpkgs.config.packageOverrides = pkgs:
3131+ { linux_3_4 = pkgs.linux_3_4.override {
3232+ extraConfig =
3333+ ''
3434+ KGDB y
3535+ '';
3636+ };
3737+ };
3838+</programlisting>
3939+4040+<varname>extraConfig</varname> takes a list of Linux kernel
4141+configuration options, one per line. The name of the option should
4242+not include the prefix <literal>CONFIG_</literal>. The option value
4343+is typically <literal>y</literal>, <literal>n</literal> or
4444+<literal>m</literal> (to build something as a kernel module).</para>
4545+4646+<para>Kernel modules for hardware devices are generally loaded
4747+automatically by <command>udev</command>. You can force a module to
4848+be loaded via <option>boot.kernelModules</option>, e.g.
4949+<programlisting>
5050+boot.kernelModules = [ "fuse" "kvm-intel" "coretemp" ];
5151+</programlisting>
5252+If the module is required early during the boot (e.g. to mount the
5353+root file system), you can use
5454+<option>boot.initrd.extraKernelModules</option>:
5555+<programlisting>
5656+boot.initrd.extraKernelModules = [ "cifs" ];
5757+</programlisting>
5858+This causes the specified modules and their dependencies to be added
5959+to the initial ramdark.</para>
6060+6161+<para>Kernel runtime parameters can be set through
6262+<option>boot.kernel.sysctl</option>, e.g.
6363+<programlisting>
6464+boot.kernel.sysctl."net.ipv4.tcp_keepalive_time" = 120;
6565+</programlisting>
6666+sets the kernel’s TCP keepalive time to 120 seconds. To see the
6767+available parameters, run <command>sysctl -a</command>.</para>
6868+6969+</chapter>
+143
nixos/doc/manual/configuration/modularity.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-modularity">
66+77+<title>Modularity</title>
88+99+<para>The NixOS configuration mechanism is modular. If your
1010+<filename>configuration.nix</filename> becomes too big, you can split
1111+it into multiple files. Likewise, if you have multiple NixOS
1212+configurations (e.g. for different computers) with some commonality,
1313+you can move the common configuration into a shared file.</para>
1414+1515+<para>Modules have exactly the same syntax as
1616+<filename>configuration.nix</filename>. In fact,
1717+<filename>configuration.nix</filename> is itself a module. You can
1818+use other modules by including them from
1919+<filename>configuration.nix</filename>, e.g.:
2020+2121+<programlisting>
2222+{ config, pkgs, ... }:
2323+2424+{ imports = [ ./vpn.nix ./kde.nix ];
2525+ services.httpd.enable = true;
2626+ environment.systemPackages = [ pkgs.emacs ];
2727+ <replaceable>...</replaceable>
2828+}
2929+</programlisting>
3030+3131+Here, we include two modules from the same directory,
3232+<filename>vpn.nix</filename> and <filename>kde.nix</filename>. The
3333+latter might look like this:
3434+3535+<programlisting>
3636+{ config, pkgs, ... }:
3737+3838+{ services.xserver.enable = true;
3939+ services.xserver.displayManager.kdm.enable = true;
4040+ services.xserver.desktopManager.kde4.enable = true;
4141+ environment.systemPackages = [ pkgs.kde4.kscreensaver ];
4242+}
4343+</programlisting>
4444+4545+Note that both <filename>configuration.nix</filename> and
4646+<filename>kde.nix</filename> define the option
4747+<option>environment.systemPackages</option>. When multiple modules
4848+define an option, NixOS will try to <emphasis>merge</emphasis> the
4949+definitions. In the case of
5050+<option>environment.systemPackages</option>, that’s easy: the lists of
5151+packages can simply be concatenated. The value in
5252+<filename>configuration.nix</filename> is merged last, so for
5353+list-type options, it will appear at the end of the merged list. If
5454+you want it to appear first, you can use <varname>mkBefore</varname>:
5555+5656+<programlisting>
5757+boot.kernelModules = mkBefore [ "kvm-intel" ];
5858+</programlisting>
5959+6060+This causes the <literal>kvm-intel</literal> kernel module to be
6161+loaded before any other kernel modules.</para>
6262+6363+<para>For other types of options, a merge may not be possible. For
6464+instance, if two modules define
6565+<option>services.httpd.adminAddr</option>,
6666+<command>nixos-rebuild</command> will give an error:
6767+6868+<screen>
6969+The unique option `services.httpd.adminAddr' is defined multiple times, in `/etc/nixos/httpd.nix' and `/etc/nixos/configuration.nix'.
7070+</screen>
7171+7272+When that happens, it’s possible to force one definition take
7373+precedence over the others:
7474+7575+<programlisting>
7676+services.httpd.adminAddr = pkgs.lib.mkForce "bob@example.org";
7777+</programlisting>
7878+7979+</para>
8080+8181+<para>When using multiple modules, you may need to access
8282+configuration values defined in other modules. This is what the
8383+<varname>config</varname> function argument is for: it contains the
8484+complete, merged system configuration. That is,
8585+<varname>config</varname> is the result of combining the
8686+configurations returned by every module<footnote><para>If you’re
8787+wondering how it’s possible that the (indirect)
8888+<emphasis>result</emphasis> of a function is passed as an
8989+<emphasis>input</emphasis> to that same function: that’s because Nix
9090+is a “lazy” language — it only computes values when they are needed.
9191+This works as long as no individual configuration value depends on
9292+itself.</para></footnote>. For example, here is a module that adds
9393+some packages to <option>environment.systemPackages</option> only if
9494+<option>services.xserver.enable</option> is set to
9595+<literal>true</literal> somewhere else:
9696+9797+<programlisting>
9898+{ config, pkgs, ... }:
9999+100100+{ environment.systemPackages =
101101+ if config.services.xserver.enable then
102102+ [ pkgs.firefox
103103+ pkgs.thunderbird
104104+ ]
105105+ else
106106+ [ ];
107107+}
108108+</programlisting>
109109+110110+</para>
111111+112112+<para>With multiple modules, it may not be obvious what the final
113113+value of a configuration option is. The command
114114+<option>nixos-option</option> allows you to find out:
115115+116116+<screen>
117117+$ nixos-option services.xserver.enable
118118+true
119119+120120+$ nixos-option boot.kernelModules
121121+[ "tun" "ipv6" "loop" <replaceable>...</replaceable> ]
122122+</screen>
123123+124124+Interactive exploration of the configuration is possible using
125125+<command
126126+xlink:href="https://github.com/edolstra/nix-repl">nix-repl</command>,
127127+a read-eval-print loop for Nix expressions. It’s not installed by
128128+default; run <literal>nix-env -i nix-repl</literal> to get it. A
129129+typical use:
130130+131131+<screen>
132132+$ nix-repl '<nixos>'
133133+134134+nix-repl> config.networking.hostName
135135+"mandark"
136136+137137+nix-repl> map (x: x.hostName) config.services.httpd.virtualHosts
138138+[ "example.org" "example.gov" ]
139139+</screen>
140140+141141+</para>
142142+143143+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-networkmanager">
66+77+<title>NetworkManager</title>
88+99+<para>To facilitate network configuration, some desktop environments
1010+use NetworkManager. You can enable NetworkManager by setting:
1111+1212+<programlisting>
1313+services.networkmanager.enable = true;
1414+</programlisting>
1515+1616+Some desktop managers (e.g., GNOME) enable NetworkManager
1717+automatically for you.</para>
1818+1919+<para>All users that should have permission to change network settings
2020+must belong to the <code>networkmanager</code> group.</para>
2121+2222+<note><para><code>services.networkmanager</code> and
2323+<code>services.wireless</code> can not be enabled at the same time:
2424+you can still connect to the wireless networks using
2525+NetworkManager.</para></note>
2626+2727+</section>
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-package-management">
66+77+<title>Package Management</title>
88+99+<para>This section describes how to add additional packages to your
1010+system. NixOS has two distinct styles of package management:
1111+1212+<itemizedlist>
1313+1414+ <listitem><para><emphasis>Declarative</emphasis>, where you declare
1515+ what packages you want in your
1616+ <filename>configuration.nix</filename>. Every time you run
1717+ <command>nixos-rebuild</command>, NixOS will ensure that you get a
1818+ consistent set of binaries corresponding to your
1919+ specification.</para></listitem>
2020+2121+ <listitem><para><emphasis>Ad hoc</emphasis>, where you install,
2222+ upgrade and uninstall packages via the <command>nix-env</command>
2323+ command. This style allows mixing packages from different Nixpkgs
2424+ versions. It’s the only choice for non-root
2525+ users.</para></listitem>
2626+2727+</itemizedlist>
2828+2929+</para>
3030+3131+<xi:include href="declarative-packages.xml" />
3232+<xi:include href="ad-hoc-packages.xml" />
3333+3434+</chapter>
+32
nixos/doc/manual/configuration/ssh.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-ssh">
66+77+<title>Secure Shell Access</title>
88+99+<para>Secure shell (SSH) access to your machine can be enabled by
1010+setting:
1111+1212+<programlisting>
1313+services.openssh.enable = true;
1414+</programlisting>
1515+1616+By default, root logins using a password are disallowed. They can be
1717+disabled entirely by setting
1818+<literal>services.openssh.permitRootLogin</literal> to
1919+<literal>"no"</literal>.</para>
2020+2121+<para>You can declaratively specify authorised RSA/DSA public keys for
2222+a user as follows:
2323+2424+<!-- FIXME: this might not work if the user is unmanaged. -->
2525+<programlisting>
2626+users.extraUsers.alice.openssh.authorizedKeys.keys =
2727+ [ "ssh-dss AAAAB3NzaC1kc3MAAACBAPIkGWVEt4..." ];
2828+</programlisting>
2929+3030+</para>
3131+3232+</section>
+191
nixos/doc/manual/configuration/summary.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-nix-syntax-summary">
66+77+<title>Syntax Summary</title>
88+99+<para>Below is a summary of the most important syntactic constructs in
1010+the Nix expression language. It’s not complete. In particular, there
1111+are many other built-in functions. See the <link
1212+xlink:href="http://nixos.org/nix/manual/#chap-writing-nix-expressions">Nix
1313+manual</link> for the rest.</para>
1414+1515+<informaltable frame='none'>
1616+ <tgroup cols='2'>
1717+ <colspec colname='c1' rowsep='1' colsep='1' />
1818+ <colspec colname='c2' rowsep='1' />
1919+ <thead>
2020+ <row>
2121+ <entry>Example</entry>
2222+ <entry>Description</entry>
2323+ </row>
2424+ </thead>
2525+ <tbody>
2626+2727+ <row>
2828+ <entry namest="c1" nameend="c2"><emphasis>Basic values</emphasis></entry>
2929+ </row>
3030+ <row>
3131+ <entry><literal>"Hello world"</literal></entry>
3232+ <entry>A string</entry>
3333+ </row>
3434+ <row>
3535+ <entry><literal>"${pkgs.bash}/bin/sh"</literal></entry>
3636+ <entry>A string containing an expression (expands to <literal>"/nix/store/<replaceable>hash</replaceable>-bash-<replaceable>version</replaceable>/bin/sh"</literal>)</entry>
3737+ </row>
3838+ <row>
3939+ <entry><literal>true</literal>, <literal>false</literal></entry>
4040+ <entry>Booleans</entry>
4141+ </row>
4242+ <row>
4343+ <entry><literal>123</literal></entry>
4444+ <entry>An integer</entry>
4545+ </row>
4646+ <row>
4747+ <entry><literal>./foo.png</literal></entry>
4848+ <entry>A path (relative to the containing Nix expression)</entry>
4949+ </row>
5050+5151+ <row>
5252+ <entry namest="c1" nameend="c2"><emphasis>Compound values</emphasis></entry>
5353+ </row>
5454+ <row>
5555+ <entry><literal>{ x = 1; y = 2; }</literal></entry>
5656+ <entry>An set with attributes names <literal>x</literal> and <literal>y</literal></entry>
5757+ </row>
5858+ <row>
5959+ <entry><literal>{ foo.bar = 1; }</literal></entry>
6060+ <entry>A nested set, equivalent to <literal>{ foo = { bar = 1; }; }</literal></entry>
6161+ </row>
6262+ <row>
6363+ <entry><literal>rec { x = "bla"; y = x + "bar"; }</literal></entry>
6464+ <entry>A recursive set, equivalent to <literal>{ x = "foo"; y = "foobar"; }</literal></entry>
6565+ </row>
6666+ <row>
6767+ <entry><literal>[ "foo" "bar" ]</literal></entry>
6868+ <entry>A list with two elements</entry>
6969+ </row>
7070+7171+ <row>
7272+ <entry namest="c1" nameend="c2"><emphasis>Operators</emphasis></entry>
7373+ </row>
7474+ <row>
7575+ <entry><literal>"foo" + "bar"</literal></entry>
7676+ <entry>String concatenation</entry>
7777+ </row>
7878+ <row>
7979+ <entry><literal>1 + 2</literal></entry>
8080+ <entry>Integer addition</entry>
8181+ </row>
8282+ <row>
8383+ <entry><literal>"foo" == "f" + "oo"</literal></entry>
8484+ <entry>Equality test (evaluates to <literal>true</literal>)</entry>
8585+ </row>
8686+ <row>
8787+ <entry><literal>"foo" != "bar"</literal></entry>
8888+ <entry>Inequality test (evaluates to <literal>true</literal>)</entry>
8989+ </row>
9090+ <row>
9191+ <entry><literal>!true</literal></entry>
9292+ <entry>Boolean negation</entry>
9393+ </row>
9494+ <row>
9595+ <entry><literal>{ x = 1; y = 2; }.x</literal></entry>
9696+ <entry>Attribute selection (evaluates to <literal>1</literal>)</entry>
9797+ </row>
9898+ <row>
9999+ <entry><literal>{ x = 1; y = 2; }.z or 3</literal></entry>
100100+ <entry>Attribute selection with default (evaluates to <literal>3</literal>)</entry>
101101+ </row>
102102+ <row>
103103+ <entry><literal>{ x = 1; y = 2; } // { z = 3; }</literal></entry>
104104+ <entry>Merge two sets (attributes in the right-hand set taking precedence)</entry>
105105+ </row>
106106+107107+ <row>
108108+ <entry namest="c1" nameend="c2"><emphasis>Control structures</emphasis></entry>
109109+ </row>
110110+ <row>
111111+ <entry><literal>if 1 + 1 == 2 then "yes!" else "no!"</literal></entry>
112112+ <entry>Conditional expression</entry>
113113+ </row>
114114+ <row>
115115+ <entry><literal>assert 1 + 1 == 2; "yes!"</literal></entry>
116116+ <entry>Assertion check (evaluates to <literal>"yes!"</literal>)</entry>
117117+ </row>
118118+ <row>
119119+ <entry><literal>let x = "foo"; y = "bar"; in x + y</literal></entry>
120120+ <entry>Variable definition</entry>
121121+ </row>
122122+ <row>
123123+ <entry><literal>with pkgs.lib; head [ 1 2 3 ]</literal></entry>
124124+ <entry>Add all attributes from the given set to the scope
125125+ (evaluates to <literal>1</literal>)</entry>
126126+ </row>
127127+128128+ <row>
129129+ <entry namest="c1" nameend="c2"><emphasis>Functions (lambdas)</emphasis></entry>
130130+ </row>
131131+ <row>
132132+ <entry><literal>x: x + 1</literal></entry>
133133+ <entry>A function that expects an integer and returns it increased by 1</entry>
134134+ </row>
135135+ <row>
136136+ <entry><literal>(x: x + 1) 100</literal></entry>
137137+ <entry>A function call (evaluates to 101)</entry>
138138+ </row>
139139+ <row>
140140+ <entry><literal>let inc = x: x + 1; in inc (inc (inc 100))</literal></entry>
141141+ <entry>A function bound to a variable and subsequently called by name (evaluates to 103)</entry>
142142+ </row>
143143+ <row>
144144+ <entry><literal>{ x, y }: x + y</literal></entry>
145145+ <entry>A function that expects a set with required attributes
146146+ <literal>x</literal> and <literal>y</literal> and concatenates
147147+ them</entry>
148148+ </row>
149149+ <row>
150150+ <entry><literal>{ x, y ? "bar" }: x + y</literal></entry>
151151+ <entry>A function that expects a set with required attribute
152152+ <literal>x</literal> and optional <literal>y</literal>, using
153153+ <literal>"bar"</literal> as default value for
154154+ <literal>y</literal></entry>
155155+ </row>
156156+ <row>
157157+ <entry><literal>{ x, y, ... }: x + y</literal></entry>
158158+ <entry>A function that expects a set with required attributes
159159+ <literal>x</literal> and <literal>y</literal> and ignores any
160160+ other attributes</entry>
161161+ </row>
162162+ <row>
163163+ <entry><literal>{ x, y } @ args: x + y</literal></entry>
164164+ <entry>A function that expects a set with required attributes
165165+ <literal>x</literal> and <literal>y</literal>, and binds the
166166+ whole set to <literal>args</literal></entry>
167167+ </row>
168168+169169+ <row>
170170+ <entry namest="c1" nameend="c2"><emphasis>Built-in functions</emphasis></entry>
171171+ </row>
172172+ <row>
173173+ <entry><literal>import ./foo.nix</literal></entry>
174174+ <entry>Load and return Nix expression in given file</entry>
175175+ </row>
176176+ <row>
177177+ <entry><literal>map (x: x + x) [ 1 2 3 ]</literal></entry>
178178+ <entry>Apply a function to every element of a list (evaluates to <literal>[ 2 4 6 ]</literal>)</entry>
179179+ </row>
180180+ <!--
181181+ <row>
182182+ <entry><literal>throw "Urgh"</literal></entry>
183183+ <entry>Raise an error condition</entry>
184184+ </row>
185185+ -->
186186+187187+ </tbody>
188188+ </tgroup>
189189+</informaltable>
190190+191191+</section>
+95
nixos/doc/manual/configuration/user-mgmt.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-user-management">
66+77+<title>User Management</title>
88+99+<para>NixOS supports both declarative and imperative styles of user
1010+management. In the declarative style, users are specified in
1111+<filename>configuration.nix</filename>. For instance, the following
1212+states that a user account named <literal>alice</literal> shall exist:
1313+1414+<programlisting>
1515+users.extraUsers.alice =
1616+ { createHome = true;
1717+ home = "/home/alice";
1818+ description = "Alice Foobar";
1919+ extraGroups = [ "wheel" "networkmanager" ];
2020+ useDefaultShell = true;
2121+ openssh.authorizedKeys.keys = [ "ssh-dss AAAAB3Nza... alice@foobar" ];
2222+ };
2323+</programlisting>
2424+2525+Note that <literal>alice</literal> is a member of the
2626+<literal>wheel</literal> and <literal>networkmanager</literal> groups,
2727+which allows her to use <command>sudo</command> to execute commands as
2828+<literal>root</literal> and to configure the network, respectively.
2929+Also note the SSH public key that allows remote logins with the
3030+corresponding private key. Users created in this way do not have a
3131+password by default, so they cannot log in via mechanisms that require
3232+a password. However, you can use the <command>passwd</command> program
3333+to set a password, which is retained across invocations of
3434+<command>nixos-rebuild</command>.</para>
3535+3636+<para>If you set users.mutableUsers to false, then the contents of /etc/passwd
3737+and /etc/group will be congruent to your NixOS configuration. For instance,
3838+if you remove a user from users.extraUsers and run nixos-rebuild, the user
3939+account will cease to exist. Also, imperative commands for managing users
4040+and groups, such as useradd, are no longer available.</para>
4141+4242+<para>A user ID (uid) is assigned automatically. You can also specify
4343+a uid manually by adding
4444+4545+<programlisting>
4646+ uid = 1000;
4747+</programlisting>
4848+4949+to the user specification.</para>
5050+5151+<para>Groups can be specified similarly. The following states that a
5252+group named <literal>students</literal> shall exist:
5353+5454+<programlisting>
5555+users.extraGroups.students.gid = 1000;
5656+</programlisting>
5757+5858+As with users, the group ID (gid) is optional and will be assigned
5959+automatically if it’s missing.</para>
6060+6161+<warning><para>Currently declarative user management is not perfect:
6262+<command>nixos-rebuild</command> does not know how to realise certain
6363+configuration changes. This includes removing a user or group, and
6464+removing group membership from a user.</para></warning>
6565+6666+<para>In the imperative style, users and groups are managed by
6767+commands such as <command>useradd</command>,
6868+<command>groupmod</command> and so on. For instance, to create a user
6969+account named <literal>alice</literal>:
7070+7171+<screen>
7272+$ useradd -m alice</screen>
7373+7474+The flag <option>-m</option> causes the creation of a home directory
7575+for the new user, which is generally what you want. The user does not
7676+have an initial password and therefore cannot log in. A password can
7777+be set using the <command>passwd</command> utility:
7878+7979+<screen>
8080+$ passwd alice
8181+Enter new UNIX password: ***
8282+Retype new UNIX password: ***
8383+</screen>
8484+8585+A user can be deleted using <command>userdel</command>:
8686+8787+<screen>
8888+$ userdel -r alice</screen>
8989+9090+The flag <option>-r</option> deletes the user’s home directory.
9191+Accounts can be modified using <command>usermod</command>. Unix
9292+groups can be managed using <command>groupadd</command>,
9393+<command>groupmod</command> and <command>groupdel</command>.</para>
9494+9595+</chapter>
+41
nixos/doc/manual/configuration/wireless.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-wireless">
66+77+<title>Wireless Networks</title>
88+99+<para>For a desktop installation using NetworkManager (e.g., GNOME),
1010+you just have to make sure the user is in the
1111+<code>networkmanager</code> group and you can skip the rest of this
1212+section on wireless networks.</para>
1313+1414+<para>
1515+NixOS will start wpa_supplicant for you if you enable this setting:
1616+1717+<programlisting>
1818+networking.wireless.enable = true;
1919+</programlisting>
2020+2121+NixOS currently does not generate wpa_supplicant's
2222+configuration file, <literal>/etc/wpa_supplicant.conf</literal>. You should edit this file
2323+yourself to define wireless networks, WPA keys and so on (see
2424+wpa_supplicant.conf(5)).
2525+</para>
2626+2727+<para>
2828+If you are using WPA2 the <command>wpa_passphrase</command> tool might be useful
2929+to generate the <literal>wpa_supplicant.conf</literal>.
3030+3131+<screen>
3232+$ wpa_passphrase ESSID PSK > /etc/wpa_supplicant.conf</screen>
3333+3434+After you have edited the <literal>wpa_supplicant.conf</literal>,
3535+you need to restart the wpa_supplicant service.
3636+3737+<screen>
3838+$ systemctl restart wpa_supplicant.service</screen>
3939+</para>
4040+4141+</section>
+94
nixos/doc/manual/configuration/x-windows.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-x11">
66+77+<title>X Window System</title>
88+99+<para>The X Window System (X11) provides the basis of NixOS’ graphical
1010+user interface. It can be enabled as follows:
1111+<programlisting>
1212+services.xserver.enable = true;
1313+</programlisting>
1414+The X server will automatically detect and use the appropriate video
1515+driver from a set of X.org drivers (such as <literal>vesa</literal>
1616+and <literal>intel</literal>). You can also specify a driver
1717+manually, e.g.
1818+<programlisting>
1919+services.xserver.videoDrivers = [ "r128" ];
2020+</programlisting>
2121+to enable X.org’s <literal>xf86-video-r128</literal> driver.</para>
2222+2323+<para>You also need to enable at least one desktop or window manager.
2424+Otherwise, you can only log into a plain undecorated
2525+<command>xterm</command> window. Thus you should pick one or more of
2626+the following lines:
2727+<programlisting>
2828+services.xserver.desktopManager.kde4.enable = true;
2929+services.xserver.desktopManager.xfce.enable = true;
3030+services.xserver.windowManager.xmonad.enable = true;
3131+services.xserver.windowManager.twm.enable = true;
3232+services.xserver.windowManager.icewm.enable = true;
3333+</programlisting>
3434+</para>
3535+3636+<para>NixOS’s default <emphasis>display manager</emphasis> (the
3737+program that provides a graphical login prompt and manages the X
3838+server) is SLiM. You can select KDE’s <command>kdm</command> instead:
3939+<programlisting>
4040+services.xserver.displayManager.kdm.enable = true;
4141+</programlisting>
4242+</para>
4343+4444+<para>The X server is started automatically at boot time. If you
4545+don’t want this to happen, you can set:
4646+<programlisting>
4747+services.xserver.autorun = false;
4848+</programlisting>
4949+The X server can then be started manually:
5050+<screen>
5151+$ systemctl start display-manager.service
5252+</screen>
5353+</para>
5454+5555+5656+<simplesect><title>NVIDIA Graphics Cards</title>
5757+5858+<para>NVIDIA provides a proprietary driver for its graphics cards that
5959+has better 3D performance than the X.org drivers. It is not enabled
6060+by default because it’s not free software. You can enable it as follows:
6161+<programlisting>
6262+services.xserver.videoDrivers = [ "nvidia" ];
6363+</programlisting>
6464+You may need to reboot after enabling this driver to prevent a clash
6565+with other kernel modules.</para>
6666+6767+<para>On 64-bit systems, if you want full acceleration for 32-bit
6868+programs such as Wine, you should also set the following:
6969+<programlisting>
7070+services.xserver.driSupport32Bit = true;
7171+</programlisting>
7272+</para>
7373+7474+</simplesect>
7575+7676+7777+<simplesect><title>Touchpads</title>
7878+7979+<para>Support for Synaptics touchpads (found in many laptops such as
8080+the Dell Latitude series) can be enabled as follows:
8181+<programlisting>
8282+services.xserver.synaptics.enable = true;
8383+</programlisting>
8484+The driver has many options (see <xref linkend="ch-options"/>). For
8585+instance, the following enables two-finger scrolling:
8686+<programlisting>
8787+services.xserver.synaptics.twoFingerScroll = true;
8888+</programlisting>
8989+</para>
9090+9191+</simplesect>
9292+9393+9494+</chapter>
-242
nixos/doc/manual/containers.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-containers">
44-55-<title>Containers</title>
66-77-<para>NixOS allows you to easily run other NixOS instances as
88-<emphasis>containers</emphasis>. Containers are a light-weight
99-approach to virtualisation that runs software in the container at the
1010-same speed as in the host system. NixOS containers share the Nix store
1111-of the host, making container creation very efficient.</para>
1212-1313-<warning><para>Currently, NixOS containers are not perfectly isolated
1414-from the host system. This means that a user with root access to the
1515-container can do things that affect the host. So you should not give
1616-container root access to untrusted users.</para></warning>
1717-1818-<para>NixOS containers can be created in two ways: imperatively, using
1919-the command <command>nixos-container</command>, and declaratively, by
2020-specifying them in your <filename>configuration.nix</filename>. The
2121-declarative approach implies that containers get upgraded along with
2222-your host system when you run <command>nixos-rebuild</command>, which
2323-is often not what you want. By contrast, in the imperative approach,
2424-containers are configured and updated independently from the host
2525-system.</para>
2626-2727-2828-<section><title>Imperative container management</title>
2929-3030-<para>We’ll cover imperative container management using
3131-<command>nixos-container</command> first. You create a container with
3232-identifier <literal>foo</literal> as follows:
3333-3434-<screen>
3535-$ nixos-container create foo
3636-</screen>
3737-3838-This creates the container’s root directory in
3939-<filename>/var/lib/containers/foo</filename> and a small configuration
4040-file in <filename>/etc/containers/foo.conf</filename>. It also builds
4141-the container’s initial system configuration and stores it in
4242-<filename>/nix/var/nix/profiles/per-container/foo/system</filename>. You
4343-can modify the initial configuration of the container on the command
4444-line. For instance, to create a container that has
4545-<command>sshd</command> running, with the given public key for
4646-<literal>root</literal>:
4747-4848-<screen>
4949-$ nixos-container create foo --config 'services.openssh.enable = true; \
5050- users.extraUsers.root.openssh.authorizedKeys.keys = ["ssh-dss AAAAB3N…"];'
5151-</screen>
5252-5353-</para>
5454-5555-<para>Creating a container does not start it. To start the container,
5656-run:
5757-5858-<screen>
5959-$ nixos-container start foo
6060-</screen>
6161-6262-This command will return as soon as the container has booted and has
6363-reached <literal>multi-user.target</literal>. On the host, the
6464-container runs within a systemd unit called
6565-<literal>container@<replaceable>container-name</replaceable>.service</literal>.
6666-Thus, if something went wrong, you can get status info using
6767-<command>systemctl</command>:
6868-6969-<screen>
7070-$ systemctl status container@foo
7171-</screen>
7272-7373-</para>
7474-7575-<para>If the container has started succesfully, you can log in as
7676-root using the <command>root-login</command> operation:
7777-7878-<screen>
7979-$ nixos-container root-login foo
8080-[root@foo:~]#
8181-</screen>
8282-8383-Note that only root on the host can do this (since there is no
8484-authentication). You can also get a regular login prompt using the
8585-<command>login</command> operation, which is available to all users on
8686-the host:
8787-8888-<screen>
8989-$ nixos-container login foo
9090-foo login: alice
9191-Password: ***
9292-</screen>
9393-9494-With <command>nixos-container run</command>, you can execute arbitrary
9595-commands in the container:
9696-9797-<screen>
9898-$ nixos-container run foo -- uname -a
9999-Linux foo 3.4.82 #1-NixOS SMP Thu Mar 20 14:44:05 UTC 2014 x86_64 GNU/Linux
100100-</screen>
101101-102102-</para>
103103-104104-<para>There are several ways to change the configuration of the
105105-container. First, on the host, you can edit
106106-<literal>/var/lib/container/<replaceable>name</replaceable>/etc/nixos/configuration.nix</literal>,
107107-and run
108108-109109-<screen>
110110-$ nixos-container update foo
111111-</screen>
112112-113113-This will build and activate the new configuration. You can also
114114-specify a new configuration on the command line:
115115-116116-<screen>
117117-$ nixos-container update foo --config 'services.httpd.enable = true; \
118118- services.httpd.adminAddr = "foo@example.org";'
119119-120120-$ curl http://$(nixos-container show-ip foo)/
121121-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">…
122122-</screen>
123123-124124-However, note that this will overwrite the container’s
125125-<filename>/etc/nixos/configuration.nix</filename>.</para>
126126-127127-<para>Alternatively, you can change the configuration from within the
128128-container itself by running <command>nixos-rebuild switch</command>
129129-inside the container. Note that the container by default does not have
130130-a copy of the NixOS channel, so you should run <command>nix-channel
131131---update</command> first.</para>
132132-133133-<para>Containers can be stopped and started using
134134-<literal>nixos-container stop</literal> and <literal>nixos-container
135135-start</literal>, respectively, or by using
136136-<command>systemctl</command> on the container’s service unit. To
137137-destroy a container, including its file system, do
138138-139139-<screen>
140140-$ nixos-container destroy foo
141141-</screen>
142142-143143-</para>
144144-145145-</section>
146146-147147-148148-<section><title>Declarative container specification</title>
149149-150150-<para>You can also specify containers and their configuration in the
151151-host’s <filename>configuration.nix</filename>. For example, the
152152-following specifies that there shall be a container named
153153-<literal>database</literal> running PostgreSQL:
154154-155155-<programlisting>
156156-containers.database =
157157- { config =
158158- { config, pkgs, ... }:
159159- { services.postgresql.enable = true;
160160- services.postgresql.package = pkgs.postgresql92;
161161- };
162162- };
163163-</programlisting>
164164-165165-If you run <literal>nixos-rebuild switch</literal>, the container will
166166-be built and started. If the container was already running, it will be
167167-updated in place, without rebooting.</para>
168168-169169-<para>By default, declarative containers share the network namespace
170170-of the host, meaning that they can listen on (privileged)
171171-ports. However, they cannot change the network configuration. You can
172172-give a container its own network as follows:
173173-174174-<programlisting>
175175-containers.database =
176176- { privateNetwork = true;
177177- hostAddress = "192.168.100.10";
178178- localAddress = "192.168.100.11";
179179- };
180180-</programlisting>
181181-182182-This gives the container a private virtual Ethernet interface with IP
183183-address <literal>192.168.100.11</literal>, which is hooked up to a
184184-virtual Ethernet interface on the host with IP address
185185-<literal>192.168.100.10</literal>. (See the next section for details
186186-on container networking.)</para>
187187-188188-<para>To disable the container, just remove it from
189189-<filename>configuration.nix</filename> and run <literal>nixos-rebuild
190190-switch</literal>. Note that this will not delete the root directory of
191191-the container in <literal>/var/lib/containers</literal>.</para>
192192-193193-</section>
194194-195195-196196-<section><title>Networking</title>
197197-198198-<para>When you create a container using <literal>nixos-container
199199-create</literal>, it gets it own private IPv4 address in the range
200200-<literal>10.233.0.0/16</literal>. You can get the container’s IPv4
201201-address as follows:
202202-203203-<screen>
204204-$ nixos-container show-ip foo
205205-10.233.4.2
206206-207207-$ ping -c1 10.233.4.2
208208-64 bytes from 10.233.4.2: icmp_seq=1 ttl=64 time=0.106 ms
209209-</screen>
210210-211211-</para>
212212-213213-<para>Networking is implemented using a pair of virtual Ethernet
214214-devices. The network interface in the container is called
215215-<literal>eth0</literal>, while the matching interface in the host is
216216-called <literal>ve-<replaceable>container-name</replaceable></literal>
217217-(e.g., <literal>ve-foo</literal>). The container has its own network
218218-namespace and the <literal>CAP_NET_ADMIN</literal> capability, so it
219219-can perform arbitrary network configuration such as setting up
220220-firewall rules, without affecting or having access to the host’s
221221-network.</para>
222222-223223-<para>By default, containers cannot talk to the outside network. If
224224-you want that, you should set up Network Address Translation (NAT)
225225-rules on the host to rewrite container traffic to use your external
226226-IP address. This can be accomplished using the following configuration
227227-on the host:
228228-229229-<programlisting>
230230-networking.nat.enable = true;
231231-networking.nat.internalInterfaces = ["ve-+"];
232232-networking.nat.externalInterface = "eth0";
233233-</programlisting>
234234-where <literal>eth0</literal> should be replaced with the desired
235235-external interface. Note that <literal>ve-+</literal> is a wildcard
236236-that matches all container interfaces.</para>
237237-238238-</section>
239239-240240-241241-</chapter>
242242-
-1119
nixos/doc/manual/development.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-development">
44-55-<title>Development</title>
66-77-<para>This chapter describes how you can modify and extend
88-NixOS.</para>
99-1010-1111-<!--===============================================================-->
1212-1313-<section xml:id="sec-getting-sources">
1414-1515-<title>Getting the sources</title>
1616-1717-<para>By default, NixOS’s <command>nixos-rebuild</command> command
1818-uses the NixOS and Nixpkgs sources provided by the
1919-<literal>nixos-unstable</literal> channel (kept in
2020-<filename>/nix/var/nix/profiles/per-user/root/channels/nixos</filename>).
2121-To modify NixOS, however, you should check out the latest sources from
2222-Git. This is done using the following command:
2323-2424-<screen>
2525-$ nixos-checkout <replaceable>/my/sources</replaceable>
2626-</screen>
2727-2828-or
2929-3030-<screen>
3131-$ mkdir -p <replaceable>/my/sources</replaceable>
3232-$ cd <replaceable>/my/sources</replaceable>
3333-$ nix-env -i git
3434-$ git clone git://github.com/NixOS/nixpkgs.git
3535-</screen>
3636-3737-This will check out the latest NixOS sources to
3838-<filename><replaceable>/my/sources</replaceable>/nixpkgs/nixos</filename>
3939-and the Nixpkgs sources to
4040-<filename><replaceable>/my/sources</replaceable>/nixpkgs</filename>.
4141-(The NixOS source tree lives in a subdirectory of the Nixpkgs
4242-repository.)</para>
4343-4444-<para>It’s often inconvenient to develop directly on the master
4545-branch, since if somebody has just committed (say) a change to GCC,
4646-then the binary cache may not have caught up yet and you’ll have to
4747-rebuild everything from source. So you may want to create a local
4848-branch based on your current NixOS version:
4949-5050-<screen>
5151-$ nixos-version
5252-14.04.273.ea1952b (Baboon)
5353-5454-$ git checkout -b local ea1952b
5555-</screen>
5656-5757-Or, to base your local branch on the latest version available in the
5858-NixOS channel:
5959-6060-<screen>
6161-$ curl -sI http://nixos.org/channels/nixos-unstable/ | grep Location
6262-Location: http://releases.nixos.org/nixos/unstable/nixos-14.10pre43986.acaf4a6/
6363-6464-$ git checkout -b local acaf4a6
6565-</screen>
6666-6767-You can then use <command>git rebase</command> to sync your local
6868-branch with the upstream branch, and use <command>git
6969-cherry-pick</command> to copy commits from your local branch to the
7070-upstream branch.</para>
7171-7272-<para>If you want to rebuild your system using your (modified)
7373-sources, you need to tell <command>nixos-rebuild</command> about them
7474-using the <option>-I</option> flag:
7575-7676-<screen>
7777-$ nixos-rebuild switch -I nixpkgs=<replaceable>/my/sources</replaceable>/nixpkgs
7878-</screen>
7979-8080-</para>
8181-8282-<para>If you want <command>nix-env</command> to use the expressions in
8383-<replaceable>/my/sources</replaceable>, use <command>nix-env -f
8484-<replaceable>/my/sources</replaceable>/nixpkgs</command>, or change
8585-the default by adding a symlink in
8686-<filename>~/.nix-defexpr</filename>:
8787-8888-<screen>
8989-$ ln -s <replaceable>/my/sources</replaceable>/nixpkgs ~/.nix-defexpr/nixpkgs
9090-</screen>
9191-9292-You may want to delete the symlink
9393-<filename>~/.nix-defexpr/channels_root</filename> to prevent root’s
9494-NixOS channel from clashing with your own tree.</para>
9595-9696-<!-- FIXME: not sure what this means.
9797-<para>You should not pass the base directory
9898-<filename><replaceable>/my/sources</replaceable></filename>
9999-to <command>nix-env</command>, as it will break after interpreting expressions
100100-in <filename>nixos/</filename> as packages.</para>
101101--->
102102-103103-</section>
104104-105105-106106-<!--===============================================================-->
107107-108108-<section xml:id="sec-writing-modules">
109109-110110-<title>Writing NixOS modules</title>
111111-112112-<para>NixOS has a modular system for declarative configuration. This
113113-system combines multiple <emphasis>modules</emphasis> to produce the
114114-full system configuration. One of the modules that constitute the
115115-configuration is <filename>/etc/nixos/configuration.nix</filename>.
116116-Most of the others live in the <link
117117-xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/modules"><filename>nixos/modules</filename></link>
118118-subdirectory of the Nixpkgs tree.</para>
119119-120120-<para>Each NixOS module is a file that handles one logical aspect of
121121-the configuration, such as a specific kind of hardware, a service, or
122122-network settings. A module configuration does not have to handle
123123-everything from scratch; it can use the functionality provided by
124124-other modules for its implementation. Thus a module can
125125-<emphasis>declare</emphasis> options that can be used by other
126126-modules, and conversely can <emphasis>define</emphasis> options
127127-provided by other modules in its own implementation. For example, the
128128-module <link
129129-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/security/pam.nix"><filename>pam.nix</filename></link>
130130-declares the option <option>security.pam.services</option> that allows
131131-other modules (e.g. <link
132132-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/networking/ssh/sshd.nix"><filename>sshd.nix</filename></link>)
133133-to define PAM services; and it defines the option
134134-<option>environment.etc</option> (declared by <link
135135-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/etc/etc.nix"><filename>etc.nix</filename></link>)
136136-to cause files to be created in
137137-<filename>/etc/pam.d</filename>.</para>
138138-139139-<para xml:id="para-module-syn">In <xref
140140-linkend="sec-configuration-syntax"/>, we saw the following structure
141141-of NixOS modules:
142142-143143-<programlisting>
144144-{ config, pkgs, ... }:
145145-146146-{ <replaceable>option definitions</replaceable>
147147-}
148148-</programlisting>
149149-150150-This is actually an <emphasis>abbreviated</emphasis> form of module
151151-that only defines options, but does not declare any. The structure of
152152-full NixOS modules is shown in <xref linkend='ex-module-syntax' />.</para>
153153-154154-<example xml:id='ex-module-syntax'><title>Structure of NixOS modules</title>
155155-<programlisting>
156156-{ config, pkgs, ... }: <co xml:id='module-syntax-1' />
157157-158158-{
159159- imports =
160160- [ <replaceable>paths of other modules</replaceable> <co xml:id='module-syntax-2' />
161161- ];
162162-163163- options = {
164164- <replaceable>option declarations</replaceable> <co xml:id='module-syntax-3' />
165165- };
166166-167167- config = {
168168- <replaceable>option definitions</replaceable> <co xml:id='module-syntax-4' />
169169- };
170170-}</programlisting>
171171-</example>
172172-173173-<para>The meaning of each part is as follows.
174174-175175-<calloutlist>
176176- <callout arearefs='module-syntax-1'>
177177- <para>This line makes the current Nix expression a function. The
178178- variable <varname>pkgs</varname> contains Nixpkgs, while
179179- <varname>config</varname> contains the full system configuration.
180180- This line can be omitted if there is no reference to
181181- <varname>pkgs</varname> and <varname>config</varname> inside the
182182- module.</para>
183183- </callout>
184184-185185- <callout arearefs='module-syntax-2'>
186186- <para>This list enumerates the paths to other NixOS modules that
187187- should be included in the evaluation of the system configuration.
188188- A default set of modules is defined in the file
189189- <filename>modules/module-list.nix</filename>. These don't need to
190190- be added in the import list.</para>
191191- </callout>
192192-193193- <callout arearefs='module-syntax-3'>
194194- <para>The attribute <varname>options</varname> is a nested set of
195195- <emphasis>option declarations</emphasis> (described below).</para>
196196- </callout>
197197-198198- <callout arearefs='module-syntax-4'>
199199- <para>The attribute <varname>config</varname> is a nested set of
200200- <emphasis>option definitions</emphasis> (also described
201201- below).</para>
202202- </callout>
203203-</calloutlist>
204204-205205-</para>
206206-207207-<para><xref linkend='locate-example' /> shows a module that handles
208208-the regular update of the “locate” database, an index of all files in
209209-the file system. This module declares two options that can be defined
210210-by other modules (typically the user’s
211211-<filename>configuration.nix</filename>):
212212-<option>services.locate.enable</option> (whether the database should
213213-be updated) and <option>services.locate.period</option> (when the
214214-update should be done). It implements its functionality by defining
215215-two options declared by other modules:
216216-<option>systemd.services</option> (the set of all systemd services)
217217-and <option>services.cron.systemCronJobs</option> (the list of
218218-commands to be executed periodically by <command>cron</command>).</para>
219219-220220-<example xml:id='locate-example'><title>NixOS module for the “locate” service</title>
221221-<programlisting>
222222-{ config, lib, pkgs, ... }:
223223-224224-with lib;
225225-226226-let locatedb = "/var/cache/locatedb"; in
227227-228228-{
229229- options = {
230230-231231- services.locate = {
232232-233233- enable = mkOption {
234234- type = types.bool;
235235- default = false;
236236- description = ''
237237- If enabled, NixOS will periodically update the database of
238238- files used by the <command>locate</command> command.
239239- '';
240240- };
241241-242242- period = mkOption {
243243- type = types.str;
244244- default = "15 02 * * *";
245245- description = ''
246246- This option defines (in the format used by cron) when the
247247- locate database is updated. The default is to update at
248248- 02:15 at night every day.
249249- '';
250250- };
251251-252252- };
253253-254254- };
255255-256256- config = {
257257-258258- systemd.services.update-locatedb =
259259- { description = "Update Locate Database";
260260- path = [ pkgs.su ];
261261- script =
262262- ''
263263- mkdir -m 0755 -p $(dirname ${locatedb})
264264- exec updatedb --localuser=nobody --output=${locatedb} --prunepaths='/tmp /var/tmp /media /run'
265265- '';
266266- };
267267-268268- services.cron.systemCronJobs = optional config.services.locate.enable
269269- "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
270270-271271- };
272272-}</programlisting>
273273-</example>
274274-275275-<section><title>Option declarations</title>
276276-277277-<para>An option declaration specifies the name, type and description
278278-of a NixOS configuration option. It is illegal to define an option
279279-that hasn’t been declared in any module. A option declaration
280280-generally looks like this:
281281-282282-<programlisting>
283283-options = {
284284- <replaceable>name</replaceable> = mkOption {
285285- type = <replaceable>type specification</replaceable>;
286286- default = <replaceable>default value</replaceable>;
287287- example = <replaceable>example value</replaceable>;
288288- description = "<replaceable>Description for use in the NixOS manual.</replaceable>";
289289- };
290290-};
291291-</programlisting>
292292-293293-</para>
294294-295295-<para>The function <varname>mkOption</varname> accepts the following arguments.
296296-297297-<variablelist>
298298-299299- <varlistentry>
300300- <term><varname>type</varname></term>
301301- <listitem>
302302- <para>The type of the option (see below). It may be omitted,
303303- but that’s not advisable since it may lead to errors that are
304304- hard to diagnose.</para>
305305- </listitem>
306306- </varlistentry>
307307-308308- <varlistentry>
309309- <term><varname>default</varname></term>
310310- <listitem>
311311- <para>The default value used if no value is defined by any
312312- module. A default is not required; in that case, if the option
313313- value is ever used, an error will be thrown.</para>
314314- </listitem>
315315- </varlistentry>
316316-317317- <varlistentry>
318318- <term><varname>example</varname></term>
319319- <listitem>
320320- <para>An example value that will be shown in the NixOS manual.</para>
321321- </listitem>
322322- </varlistentry>
323323-324324- <varlistentry>
325325- <term><varname>description</varname></term>
326326- <listitem>
327327- <para>A textual description of the option, in DocBook format,
328328- that will be included in the NixOS manual.</para>
329329- </listitem>
330330- </varlistentry>
331331-332332-</variablelist>
333333-334334-</para>
335335-336336-<para>Here is a non-exhaustive list of option types:
337337-338338-<variablelist>
339339-340340- <varlistentry>
341341- <term><varname>types.bool</varname></term>
342342- <listitem>
343343- <para>A Boolean.</para>
344344- </listitem>
345345- </varlistentry>
346346-347347- <varlistentry>
348348- <term><varname>types.int</varname></term>
349349- <listitem>
350350- <para>An integer.</para>
351351- </listitem>
352352- </varlistentry>
353353-354354- <varlistentry>
355355- <term><varname>types.str</varname></term>
356356- <listitem>
357357- <para>A string.</para>
358358- </listitem>
359359- </varlistentry>
360360-361361- <varlistentry>
362362- <term><varname>types.lines</varname></term>
363363- <listitem>
364364- <para>A string. If there are multiple definitions, they are
365365- concatenated, with newline characters in between.</para>
366366- </listitem>
367367- </varlistentry>
368368-369369- <varlistentry>
370370- <term><varname>types.path</varname></term>
371371- <listitem>
372372- <para>A path, defined as anything that, when coerced to a
373373- string, starts with a slash. This includes derivations.</para>
374374- </listitem>
375375- </varlistentry>
376376-377377- <varlistentry>
378378- <term><varname>types.listOf</varname> <replaceable>t</replaceable></term>
379379- <listitem>
380380- <para>A list of elements of type <replaceable>t</replaceable>
381381- (e.g., <literal>types.listOf types.str</literal> is a list of
382382- strings). Multiple definitions are concatenated together.</para>
383383- </listitem>
384384- </varlistentry>
385385-386386- <varlistentry>
387387- <term><varname>types.attrsOf</varname> <replaceable>t</replaceable></term>
388388- <listitem>
389389- <para>A set of elements of type <replaceable>t</replaceable>
390390- (e.g., <literal>types.attrsOf types.int</literal> is a set of
391391- name/value pairs, the values being integers).</para>
392392- </listitem>
393393- </varlistentry>
394394-395395- <varlistentry>
396396- <term><varname>types.nullOr</varname> <replaceable>t</replaceable></term>
397397- <listitem>
398398- <para>Either the value <literal>null</literal> or something of
399399- type <replaceable>t</replaceable>.</para>
400400- </listitem>
401401- </varlistentry>
402402-403403-</variablelist>
404404-405405-You can also create new types using the function
406406-<varname>mkOptionType</varname>. See
407407-<filename>lib/types.nix</filename> in Nixpkgs for details.</para>
408408-409409-</section>
410410-411411-412412-<section><title>Option definitions</title>
413413-414414-<para>Option definitions are generally straight-forward bindings of values to option names, like
415415-416416-<programlisting>
417417-config = {
418418- services.httpd.enable = true;
419419-};
420420-</programlisting>
421421-422422-However, sometimes you need to wrap an option definition or set of
423423-option definitions in a <emphasis>property</emphasis> to achieve
424424-certain effects:</para>
425425-426426-<simplesect><title>Delaying conditionals</title>
427427-428428-<para>If a set of option definitions is conditional on the value of
429429-another option, you may need to use <varname>mkIf</varname>.
430430-Consider, for instance:
431431-432432-<programlisting>
433433-config = if config.services.httpd.enable then {
434434- environment.systemPackages = [ <replaceable>...</replaceable> ];
435435- <replaceable>...</replaceable>
436436-} else {};
437437-</programlisting>
438438-439439-This definition will cause Nix to fail with an “infinite recursion”
440440-error. Why? Because the value of
441441-<option>config.services.httpd.enable</option> depends on the value
442442-being constructed here. After all, you could also write the clearly
443443-circular and contradictory:
444444-<programlisting>
445445-config = if config.services.httpd.enable then {
446446- services.httpd.enable = false;
447447-} else {
448448- services.httpd.enable = true;
449449-};
450450-</programlisting>
451451-452452-The solution is to write:
453453-454454-<programlisting>
455455-config = mkIf config.services.httpd.enable {
456456- environment.systemPackages = [ <replaceable>...</replaceable> ];
457457- <replaceable>...</replaceable>
458458-};
459459-</programlisting>
460460-461461-The special function <varname>mkIf</varname> causes the evaluation of
462462-the conditional to be “pushed down” into the individual definitions,
463463-as if you had written:
464464-465465-<programlisting>
466466-config = {
467467- environment.systemPackages = if config.services.httpd.enable then [ <replaceable>...</replaceable> ] else [];
468468- <replaceable>...</replaceable>
469469-};
470470-</programlisting>
471471-472472-</para>
473473-474474-</simplesect>
475475-476476-<simplesect><title>Setting priorities</title>
477477-478478-<para>A module can override the definitions of an option in other
479479-modules by setting a <emphasis>priority</emphasis>. All option
480480-definitions that do not have the lowest priority value are discarded.
481481-By default, option definitions have priority 1000. You can specify an
482482-explicit priority by using <varname>mkOverride</varname>, e.g.
483483-484484-<programlisting>
485485-services.openssh.enable = mkOverride 10 false;
486486-</programlisting>
487487-488488-This definition causes all other definitions with priorities above 10
489489-to be discarded. The function <varname>mkForce</varname> is
490490-equal to <varname>mkOverride 50</varname>.</para>
491491-492492-</simplesect>
493493-494494-<simplesect><title>Merging configurations</title>
495495-496496-<para>In conjunction with <literal>mkIf</literal>, it is sometimes
497497-useful for a module to return multiple sets of option definitions, to
498498-be merged together as if they were declared in separate modules. This
499499-can be done using <varname>mkMerge</varname>:
500500-501501-<programlisting>
502502-config = mkMerge
503503- [ # Unconditional stuff.
504504- { environment.systemPackages = [ <replaceable>...</replaceable> ];
505505- }
506506- # Conditional stuff.
507507- (mkIf config.services.bla.enable {
508508- environment.systemPackages = [ <replaceable>...</replaceable> ];
509509- })
510510- ];
511511-</programlisting>
512512-513513-</para>
514514-515515-</simplesect>
516516-517517-</section>
518518-519519-520520-<section><title>Important options</title>
521521-522522-<para>NixOS has many options, but some are of particular importance to
523523-module writers.</para>
524524-525525-<variablelist>
526526-527527- <varlistentry>
528528- <term><option>environment.etc</option></term>
529529- <listitem>
530530- <para>This set defines files in <filename>/etc</filename>. A
531531- typical use is:
532532-<programlisting>
533533-environment.etc."os-release".text =
534534- ''
535535- NAME=NixOS
536536- <replaceable>...</replaceable>
537537- '';
538538-</programlisting>
539539- which causes a file named <filename>/etc/os-release</filename>
540540- to be created with the given contents.</para>
541541- </listitem>
542542- </varlistentry>
543543-544544- <varlistentry>
545545- <term><option>system.activationScripts</option></term>
546546- <listitem>
547547- <para>A set of shell script fragments that must be executed
548548- whenever the configuration is activated (i.e., at boot time, or
549549- after running <command>nixos-rebuild switch</command>). For instance,
550550-<programlisting>
551551-system.activationScripts.media =
552552- ''
553553- mkdir -m 0755 -p /media
554554- '';
555555-</programlisting>
556556- causes the directory <filename>/media</filename> to be created.
557557- Activation scripts must be idempotent. They should not start
558558- background processes such as daemons; use
559559- <option>systemd.services</option> for that.</para>
560560- </listitem>
561561- </varlistentry>
562562-563563- <varlistentry>
564564- <term><option>systemd.services</option></term>
565565- <listitem>
566566- <para>This is the set of systemd services. Example:
567567-<programlisting>
568568-systemd.services.dhcpcd =
569569- { description = "DHCP Client";
570570- wantedBy = [ "multi-user.target" ];
571571- after = [ "systemd-udev-settle.service" ];
572572- path = [ dhcpcd pkgs.nettools pkgs.openresolv ];
573573- serviceConfig =
574574- { Type = "forking";
575575- PIDFile = "/run/dhcpcd.pid";
576576- ExecStart = "${dhcpcd}/sbin/dhcpcd --config ${dhcpcdConf}";
577577- Restart = "always";
578578- };
579579- };
580580-</programlisting>
581581- which creates the systemd unit
582582- <literal>dhcpcd.service</literal>. The option
583583- <option>wantedBy</option> determined which other units pull this
584584- one in; <literal>multi-user.target</literal> is the default
585585- target of the system, so <literal>dhcpcd.service</literal> will
586586- always be started. The option
587587- <option>serviceConfig.ExecStart</option> provides the main
588588- command for the service; it’s also possible to provide pre-start
589589- actions, stop scripts, and so on.</para>
590590- </listitem>
591591- </varlistentry>
592592-593593- <varlistentry>
594594- <term><option>users.extraUsers</option></term>
595595- <term><option>users.extraGroups</option></term>
596596- <listitem>
597597- <para>If your service requires special UIDs or GIDs, you can
598598- define them with these options. See <xref
599599- linkend="sec-user-management"/> for details.</para>
600600- </listitem>
601601- </varlistentry>
602602-603603-</variablelist>
604604-605605-</section>
606606-607607-608608-</section>
609609-610610-611611-<!--===============================================================-->
612612-613613-<section xml:id="sec-building-parts">
614614-615615-<title>Building specific parts of NixOS</title>
616616-617617-<para>With the command <command>nix-build</command>, you can build
618618-specific parts of your NixOS configuration. This is done as follows:
619619-620620-<screen>
621621-$ cd <replaceable>/path/to/nixpkgs/nixos</replaceable>
622622-$ nix-build -A config.<replaceable>option</replaceable></screen>
623623-624624-where <replaceable>option</replaceable> is a NixOS option with type
625625-“derivation” (i.e. something that can be built). Attributes of
626626-interest include:
627627-628628-<variablelist>
629629-630630- <varlistentry>
631631- <term><varname>system.build.toplevel</varname></term>
632632- <listitem>
633633- <para>The top-level option that builds the entire NixOS system.
634634- Everything else in your configuration is indirectly pulled in by
635635- this option. This is what <command>nixos-rebuild</command>
636636- builds and what <filename>/run/current-system</filename> points
637637- to afterwards.</para>
638638-639639- <para>A shortcut to build this is:
640640-641641-<screen>
642642-$ nix-build -A system</screen>
643643- </para>
644644- </listitem>
645645- </varlistentry>
646646-647647- <varlistentry>
648648- <term><varname>system.build.manual.manual</varname></term>
649649- <listitem><para>The NixOS manual.</para></listitem>
650650- </varlistentry>
651651-652652- <varlistentry>
653653- <term><varname>system.build.etc</varname></term>
654654- <listitem><para>A tree of symlinks that form the static parts of
655655- <filename>/etc</filename>.</para></listitem>
656656- </varlistentry>
657657-658658- <varlistentry>
659659- <term><varname>system.build.initialRamdisk</varname></term>
660660- <term><varname>system.build.kernel</varname></term>
661661- <listitem>
662662- <para>The initial ramdisk and kernel of the system. This allows
663663- a quick way to test whether the kernel and the initial ramdisk
664664- boot correctly, by using QEMU’s <option>-kernel</option> and
665665- <option>-initrd</option> options:
666666-667667-<screen>
668668-$ nix-build -A config.system.build.initialRamdisk -o initrd
669669-$ nix-build -A config.system.build.kernel -o kernel
670670-$ qemu-system-x86_64 -kernel ./kernel/bzImage -initrd ./initrd/initrd -hda /dev/null
671671-</screen>
672672-673673- </para>
674674- </listitem>
675675- </varlistentry>
676676-677677- <varlistentry>
678678- <term><varname>system.build.nixos-rebuild</varname></term>
679679- <term><varname>system.build.nixos-install</varname></term>
680680- <term><varname>system.build.nixos-generate-config</varname></term>
681681- <listitem>
682682- <para>These build the corresponding NixOS commands.</para>
683683- </listitem>
684684- </varlistentry>
685685-686686- <varlistentry>
687687- <term><varname>systemd.units.<replaceable>unit-name</replaceable>.unit</varname></term>
688688- <listitem>
689689- <para>This builds the unit with the specified name. Note that
690690- since unit names contain dots
691691- (e.g. <literal>httpd.service</literal>), you need to put them
692692- between quotes, like this:
693693-694694-<screen>
695695-$ nix-build -A 'config.systemd.units."httpd.service".unit'
696696-</screen>
697697-698698- You can also test individual units, without rebuilding the whole
699699- system, by putting them in
700700- <filename>/run/systemd/system</filename>:
701701-702702-<screen>
703703-$ cp $(nix-build -A 'config.systemd.units."httpd.service".unit')/httpd.service \
704704- /run/systemd/system/tmp-httpd.service
705705-$ systemctl daemon-reload
706706-$ systemctl start tmp-httpd.service
707707-</screen>
708708-709709- Note that the unit must not have the same name as any unit in
710710- <filename>/etc/systemd/system</filename> since those take
711711- precedence over <filename>/run/systemd/system</filename>.
712712- That’s why the unit is installed as
713713- <filename>tmp-httpd.service</filename> here.</para>
714714- </listitem>
715715- </varlistentry>
716716-717717-</variablelist>
718718-719719-</para>
720720-721721-</section>
722722-723723-724724-<!--===============================================================-->
725725-726726-<section xml:id="sec-building-cd">
727727-728728-<title>Building your own NixOS CD</title>
729729-730730-<para>Building a NixOS CD is as easy as configuring your own computer. The
731731-idea is to use another module which will replace
732732-your <filename>configuration.nix</filename> to configure the system that
733733-would be installed on the CD.</para>
734734-735735-<para>Default CD/DVD configurations are available
736736-inside <filename>nixos/modules/installer/cd-dvd</filename>. To build them
737737-you have to set <envar>NIXOS_CONFIG</envar> before
738738-running <command>nix-build</command> to build the ISO.
739739-740740-<screen>
741741-$ nix-build -A config.system.build.isoImage -I nixos-config=modules/installer/cd-dvd/installation-cd-minimal.nix</screen>
742742-743743-</para>
744744-745745-<para>Before burning your CD/DVD, you can check the content of the image by mounting anywhere like
746746-suggested by the following command:
747747-748748-<screen>
749749-$ mount -o loop -t iso9660 ./result/iso/cd.iso /mnt/iso</screen>
750750-751751-</para>
752752-753753-</section>
754754-755755-756756-<!--===============================================================-->
757757-758758-<section>
759759-760760-<title>Testing the installer</title>
761761-762762-<para>Building, burning, and booting from an installation CD is rather
763763-tedious, so here is a quick way to see if the installer works
764764-properly:
765765-766766-<screen>
767767-$ nix-build -A config.system.build.nixos-install
768768-$ mount -t tmpfs none /mnt
769769-$ ./result/bin/nixos-install</screen>
770770-771771-To start a login shell in the new NixOS installation in
772772-<filename>/mnt</filename>:
773773-774774-<screen>
775775-$ ./result/bin/nixos-install --chroot
776776-</screen>
777777-778778-</para>
779779-780780-</section>
781781-782782-783783-784784-<!--===============================================================-->
785785-786786-<section xml:id="sec-nixos-tests">
787787-788788-<title>NixOS tests</title>
789789-790790-<para>When you add some feature to NixOS, you should write a test for
791791-it. NixOS tests are kept in the directory <filename
792792-xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/tests">nixos/tests</filename>,
793793-and are executed (using Nix) by a testing framework that automatically
794794-starts one or more virtual machines containing the NixOS system(s)
795795-required for the test.</para>
796796-797797-<simplesect><title>Writing tests</title>
798798-799799-<para>A NixOS test is a Nix expression that has the following structure:
800800-801801-<programlisting>
802802-import ./make-test.nix {
803803-804804- # Either the configuration of a single machine:
805805- machine =
806806- { config, pkgs, ... }:
807807- { <replaceable>configuration…</replaceable>
808808- };
809809-810810- # Or a set of machines:
811811- nodes =
812812- { <replaceable>machine1</replaceable> =
813813- { config, pkgs, ... }: { <replaceable>…</replaceable> };
814814- <replaceable>machine2</replaceable> =
815815- { config, pkgs, ... }: { <replaceable>…</replaceable> };
816816- …
817817- };
818818-819819- testScript =
820820- ''
821821- <replaceable>Perl code…</replaceable>
822822- '';
823823-}
824824-</programlisting>
825825-826826-The attribute <literal>testScript</literal> is a bit of Perl code that
827827-executes the test (described below). During the test, it will start
828828-one or more virtual machines, the configuration of which is described
829829-by the attribute <literal>machine</literal> (if you need only one
830830-machine in your test) or by the attribute <literal>nodes</literal> (if
831831-you need multiple machines). For instance, <filename
832832-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix">login.nix</filename>
833833-only needs a single machine to test whether users can log in on the
834834-virtual console, whether device ownership is correctly maintained when
835835-switching between consoles, and so on. On the other hand, <filename
836836-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nfs.nix">nfs.nix</filename>,
837837-which tests NFS client and server functionality in the Linux kernel
838838-(including whether locks are maintained across server crashes),
839839-requires three machines: a server and two clients.</para>
840840-841841-<para>There are a few special NixOS configuration options for test
842842-VMs:
843843-844844-<!-- FIXME: would be nice to generate this automatically. -->
845845-846846-<variablelist>
847847-848848- <varlistentry>
849849- <term><option>virtualisation.memorySize</option></term>
850850- <listitem><para>The memory of the VM in
851851- megabytes.</para></listitem>
852852- </varlistentry>
853853-854854- <varlistentry>
855855- <term><option>virtualisation.vlans</option></term>
856856- <listitem><para>The virtual networks to which the VM is
857857- connected. See <filename
858858- xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nat.nix">nat.nix</filename>
859859- for an example.</para></listitem>
860860- </varlistentry>
861861-862862- <varlistentry>
863863- <term><option>virtualisation.writableStore</option></term>
864864- <listitem><para>By default, the Nix store in the VM is not
865865- writable. If you enable this option, a writable union file system
866866- is mounted on top of the Nix store to make it appear
867867- writable. This is necessary for tests that run Nix operations that
868868- modify the store.</para></listitem>
869869- </varlistentry>
870870-871871-</variablelist>
872872-873873-For more options, see the module <filename
874874-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/qemu-vm.nix">qemu-vm.nix</filename>.</para>
875875-876876-<para>The test script is a sequence of Perl statements that perform
877877-various actions, such as starting VMs, executing commands in the VMs,
878878-and so on. Each virtual machine is represented as an object stored in
879879-the variable <literal>$<replaceable>name</replaceable></literal>,
880880-where <replaceable>name</replaceable> is the identifier of the machine
881881-(which is just <literal>machine</literal> if you didn’t specify
882882-multiple machines using the <literal>nodes</literal> attribute). For
883883-instance, the following starts the machine, waits until it has
884884-finished booting, then executes a command and checks that the output
885885-is more-or-less correct:
886886-887887-<programlisting>
888888-$machine->start;
889889-$machine->waitForUnit("default.target");
890890-$machine->succeed("uname") =~ /Linux/;
891891-</programlisting>
892892-893893-The first line is actually unnecessary; machines are implicitly
894894-started when you first execute an action on them (such as
895895-<literal>waitForUnit</literal> or <literal>succeed</literal>). If you
896896-have multiple machines, you can speed up the test by starting them in
897897-parallel:
898898-899899-<programlisting>
900900-startAll;
901901-</programlisting>
902902-903903-</para>
904904-905905-<para>The following methods are available on machine objects:
906906-907907-<variablelist>
908908-909909- <varlistentry>
910910- <term><methodname>start</methodname></term>
911911- <listitem><para>Start the virtual machine. This method is
912912- asynchronous — it does not wait for the machine to finish
913913- booting.</para></listitem>
914914- </varlistentry>
915915-916916- <varlistentry>
917917- <term><methodname>shutdown</methodname></term>
918918- <listitem><para>Shut down the machine, waiting for the VM to
919919- exit.</para></listitem>
920920- </varlistentry>
921921-922922- <varlistentry>
923923- <term><methodname>crash</methodname></term>
924924- <listitem><para>Simulate a sudden power failure, by telling the VM
925925- to exit immediately.</para></listitem>
926926- </varlistentry>
927927-928928- <varlistentry>
929929- <term><methodname>block</methodname></term>
930930- <listitem><para>Simulate unplugging the Ethernet cable that
931931- connects the machine to the other machines.</para></listitem>
932932- </varlistentry>
933933-934934- <varlistentry>
935935- <term><methodname>unblock</methodname></term>
936936- <listitem><para>Undo the effect of
937937- <methodname>block</methodname>.</para></listitem>
938938- </varlistentry>
939939-940940- <varlistentry>
941941- <term><methodname>screenshot</methodname></term>
942942- <listitem><para>Take a picture of the display of the virtual
943943- machine, in PNG format. The screenshot is linked from the HTML
944944- log.</para></listitem>
945945- </varlistentry>
946946-947947- <varlistentry>
948948- <term><methodname>sendMonitorCommand</methodname></term>
949949- <listitem><para>Send a command to the QEMU monitor. This is rarely
950950- used, but allows doing stuff such as attaching virtual USB disks
951951- to a running machine.</para></listitem>
952952- </varlistentry>
953953-954954- <varlistentry>
955955- <term><methodname>sendKeys</methodname></term>
956956- <listitem><para>Simulate pressing keys on the virtual keyboard,
957957- e.g., <literal>sendKeys("ctrl-alt-delete")</literal>.</para></listitem>
958958- </varlistentry>
959959-960960- <varlistentry>
961961- <term><methodname>sendChars</methodname></term>
962962- <listitem><para>Simulate typing a sequence of characters on the
963963- virtual keyboard, e.g., <literal>sendKeys("foobar\n")</literal>
964964- will type the string <literal>foobar</literal> followed by the
965965- Enter key.</para></listitem>
966966- </varlistentry>
967967-968968- <varlistentry>
969969- <term><methodname>execute</methodname></term>
970970- <listitem><para>Execute a shell command, returning a list
971971- <literal>(<replaceable>status</replaceable>,
972972- <replaceable>stdout</replaceable>)</literal>.</para></listitem>
973973- </varlistentry>
974974-975975- <varlistentry>
976976- <term><methodname>succeed</methodname></term>
977977- <listitem><para>Execute a shell command, raising an exception if
978978- the exit status is not zero, otherwise returning the standard
979979- output.</para></listitem>
980980- </varlistentry>
981981-982982- <varlistentry>
983983- <term><methodname>fail</methodname></term>
984984- <listitem><para>Like <methodname>succeed</methodname>, but raising
985985- an exception if the command returns a zero status.</para></listitem>
986986- </varlistentry>
987987-988988- <varlistentry>
989989- <term><methodname>waitUntilSucceeds</methodname></term>
990990- <listitem><para>Repeat a shell command with 1-second intervals
991991- until it succeeds.</para></listitem>
992992- </varlistentry>
993993-994994- <varlistentry>
995995- <term><methodname>waitUntilFails</methodname></term>
996996- <listitem><para>Repeat a shell command with 1-second intervals
997997- until it fails.</para></listitem>
998998- </varlistentry>
999999-10001000- <varlistentry>
10011001- <term><methodname>waitForUnit</methodname></term>
10021002- <listitem><para>Wait until the specified systemd unit has reached
10031003- the “active” state.</para></listitem>
10041004- </varlistentry>
10051005-10061006- <varlistentry>
10071007- <term><methodname>waitForFile</methodname></term>
10081008- <listitem><para>Wait until the specified file
10091009- exists.</para></listitem>
10101010- </varlistentry>
10111011-10121012- <varlistentry>
10131013- <term><methodname>waitForOpenPort</methodname></term>
10141014- <listitem><para>Wait until a process is listening on the given TCP
10151015- port (on <literal>localhost</literal>, at least).</para></listitem>
10161016- </varlistentry>
10171017-10181018- <varlistentry>
10191019- <term><methodname>waitForClosedPort</methodname></term>
10201020- <listitem><para>Wait until nobody is listening on the given TCP
10211021- port.</para></listitem>
10221022- </varlistentry>
10231023-10241024- <varlistentry>
10251025- <term><methodname>waitForX</methodname></term>
10261026- <listitem><para>Wait until the X11 server is accepting
10271027- connections.</para></listitem>
10281028- </varlistentry>
10291029-10301030- <varlistentry>
10311031- <term><methodname>waitForWindow</methodname></term>
10321032- <listitem><para>Wait until an X11 window has appeared whose name
10331033- matches the given regular expression, e.g.,
10341034- <literal>waitForWindow(qr/Terminal/)</literal>.</para></listitem>
10351035- </varlistentry>
10361036-10371037-</variablelist>
10381038-10391039-</para>
10401040-10411041-</simplesect>
10421042-10431043-10441044-<simplesect><title>Running tests</title>
10451045-10461046-<para>You can run tests using <command>nix-build</command>. For
10471047-example, to run the test <filename
10481048-xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix">login.nix</filename>,
10491049-you just do:
10501050-10511051-<screen>
10521052-$ nix-build '<nixpkgs/nixos/tests/login.nix>'
10531053-</screen>
10541054-10551055-or, if you don’t want to rely on <envar>NIX_PATH</envar>:
10561056-10571057-<screen>
10581058-$ cd /my/nixpkgs/nixos/tests
10591059-$ nix-build login.nix
10601060-…
10611061-running the VM test script
10621062-machine: QEMU running (pid 8841)
10631063-…
10641064-6 out of 6 tests succeeded
10651065-</screen>
10661066-10671067-After building/downloading all required dependencies, this will
10681068-perform a build that starts a QEMU/KVM virtual machine containing a
10691069-NixOS system. The virtual machine mounts the Nix store of the host;
10701070-this makes VM creation very fast, as no disk image needs to be
10711071-created. Afterwards, you can view a pretty-printed log of the test:
10721072-10731073-<screen>
10741074-$ firefox result/log.html
10751075-</screen>
10761076-10771077-</para>
10781078-10791079-<para>It is also possible to run the test environment interactively,
10801080-allowing you to experiment with the VMs. For example:
10811081-10821082-<screen>
10831083-$ nix-build login.nix -A driver
10841084-$ ./result/bin/nixos-run-vms
10851085-</screen>
10861086-10871087-The script <command>nixos-run-vms</command> starts the virtual
10881088-machines defined by test. The root file system of the VMs is created
10891089-on the fly and kept across VM restarts in
10901090-<filename>./</filename><varname>hostname</varname><filename>.qcow2</filename>.</para>
10911091-10921092-<para>Finally, the test itself can be run interactively. This is
10931093-particularly useful when developing or debugging a test:
10941094-10951095-<screen>
10961096-$ nix-build tests/ -A nfs.driver
10971097-$ ./result/bin/nixos-test-driver
10981098-starting VDE switch for network 1
10991099->
11001100-</screen>
11011101-11021102-You can then take any Perl statement, e.g.
11031103-11041104-<screen>
11051105-> startAll
11061106-> $machine->succeed("touch /tmp/foo")
11071107-</screen>
11081108-11091109-The function <command>testScript</command> executes the entire test
11101110-script and drops you back into the test driver command line upon its
11111111-completion. This allows you to inspect the state of the VMs after the
11121112-test (e.g. to debug the test script).</para>
11131113-11141114-</simplesect>
11151115-11161116-</section>
11171117-11181118-11191119-</chapter>
+32
nixos/doc/manual/development/building-nixos.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-building-cd">
66+77+<title>Building Your Own NixOS CD</title>
88+99+<para>Building a NixOS CD is as easy as configuring your own computer. The
1010+idea is to use another module which will replace
1111+your <filename>configuration.nix</filename> to configure the system that
1212+would be installed on the CD.</para>
1313+1414+<para>Default CD/DVD configurations are available
1515+inside <filename>nixos/modules/installer/cd-dvd</filename>. To build them
1616+you have to set <envar>NIXOS_CONFIG</envar> before
1717+running <command>nix-build</command> to build the ISO.
1818+1919+<screen>
2020+$ nix-build -A config.system.build.isoImage -I nixos-config=modules/installer/cd-dvd/installation-cd-minimal.nix</screen>
2121+2222+</para>
2323+2424+<para>Before burning your CD/DVD, you can check the content of the image by mounting anywhere like
2525+suggested by the following command:
2626+2727+<screen>
2828+$ mount -o loop -t iso9660 ./result/iso/cd.iso /mnt/iso</screen>
2929+3030+</para>
3131+3232+</chapter>
+113
nixos/doc/manual/development/building-parts.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-building-parts">
66+77+<title>Building Specific Parts of NixOS</title>
88+99+<para>With the command <command>nix-build</command>, you can build
1010+specific parts of your NixOS configuration. This is done as follows:
1111+1212+<screen>
1313+$ cd <replaceable>/path/to/nixpkgs/nixos</replaceable>
1414+$ nix-build -A config.<replaceable>option</replaceable></screen>
1515+1616+where <replaceable>option</replaceable> is a NixOS option with type
1717+“derivation” (i.e. something that can be built). Attributes of
1818+interest include:
1919+2020+<variablelist>
2121+2222+ <varlistentry>
2323+ <term><varname>system.build.toplevel</varname></term>
2424+ <listitem>
2525+ <para>The top-level option that builds the entire NixOS system.
2626+ Everything else in your configuration is indirectly pulled in by
2727+ this option. This is what <command>nixos-rebuild</command>
2828+ builds and what <filename>/run/current-system</filename> points
2929+ to afterwards.</para>
3030+3131+ <para>A shortcut to build this is:
3232+3333+<screen>
3434+$ nix-build -A system</screen>
3535+ </para>
3636+ </listitem>
3737+ </varlistentry>
3838+3939+ <varlistentry>
4040+ <term><varname>system.build.manual.manual</varname></term>
4141+ <listitem><para>The NixOS manual.</para></listitem>
4242+ </varlistentry>
4343+4444+ <varlistentry>
4545+ <term><varname>system.build.etc</varname></term>
4646+ <listitem><para>A tree of symlinks that form the static parts of
4747+ <filename>/etc</filename>.</para></listitem>
4848+ </varlistentry>
4949+5050+ <varlistentry>
5151+ <term><varname>system.build.initialRamdisk</varname></term>
5252+ <term><varname>system.build.kernel</varname></term>
5353+ <listitem>
5454+ <para>The initial ramdisk and kernel of the system. This allows
5555+ a quick way to test whether the kernel and the initial ramdisk
5656+ boot correctly, by using QEMU’s <option>-kernel</option> and
5757+ <option>-initrd</option> options:
5858+5959+<screen>
6060+$ nix-build -A config.system.build.initialRamdisk -o initrd
6161+$ nix-build -A config.system.build.kernel -o kernel
6262+$ qemu-system-x86_64 -kernel ./kernel/bzImage -initrd ./initrd/initrd -hda /dev/null
6363+</screen>
6464+6565+ </para>
6666+ </listitem>
6767+ </varlistentry>
6868+6969+ <varlistentry>
7070+ <term><varname>system.build.nixos-rebuild</varname></term>
7171+ <term><varname>system.build.nixos-install</varname></term>
7272+ <term><varname>system.build.nixos-generate-config</varname></term>
7373+ <listitem>
7474+ <para>These build the corresponding NixOS commands.</para>
7575+ </listitem>
7676+ </varlistentry>
7777+7878+ <varlistentry>
7979+ <term><varname>systemd.units.<replaceable>unit-name</replaceable>.unit</varname></term>
8080+ <listitem>
8181+ <para>This builds the unit with the specified name. Note that
8282+ since unit names contain dots
8383+ (e.g. <literal>httpd.service</literal>), you need to put them
8484+ between quotes, like this:
8585+8686+<screen>
8787+$ nix-build -A 'config.systemd.units."httpd.service".unit'
8888+</screen>
8989+9090+ You can also test individual units, without rebuilding the whole
9191+ system, by putting them in
9292+ <filename>/run/systemd/system</filename>:
9393+9494+<screen>
9595+$ cp $(nix-build -A 'config.systemd.units."httpd.service".unit')/httpd.service \
9696+ /run/systemd/system/tmp-httpd.service
9797+$ systemctl daemon-reload
9898+$ systemctl start tmp-httpd.service
9999+</screen>
100100+101101+ Note that the unit must not have the same name as any unit in
102102+ <filename>/etc/systemd/system</filename> since those take
103103+ precedence over <filename>/run/systemd/system</filename>.
104104+ That’s why the unit is installed as
105105+ <filename>tmp-httpd.service</filename> here.</para>
106106+ </listitem>
107107+ </varlistentry>
108108+109109+</variablelist>
110110+111111+</para>
112112+113113+</chapter>
+20
nixos/doc/manual/development/development.xml
···11+<part xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-development">
66+77+<title>Development</title>
88+99+<partintro>
1010+<para>This chapter describes how you can modify and extend
1111+NixOS.</para>
1212+</partintro>
1313+1414+<xi:include href="sources.xml" />
1515+<xi:include href="writing-modules.xml" />
1616+<xi:include href="building-parts.xml" />
1717+<xi:include href="building-nixos.xml" />
1818+<xi:include href="testing-installer.xml" />
1919+2020+</part>
+19
nixos/doc/manual/development/nixos-tests.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-nixos-tests">
66+77+<title>NixOS Tests</title>
88+99+<para>When you add some feature to NixOS, you should write a test for
1010+it. NixOS tests are kept in the directory <filename
1111+xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/tests">nixos/tests</filename>,
1212+and are executed (using Nix) by a testing framework that automatically
1313+starts one or more virtual machines containing the NixOS system(s)
1414+required for the test.</para>
1515+1616+<xi:include href="writing-nixos-tests.xml" />
1717+<xi:include href="running-nixos-tests.xml" />
1818+1919+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-option-declarations">
66+77+<title>Option Declarations</title>
88+99+<para>An option declaration specifies the name, type and description
1010+of a NixOS configuration option. It is illegal to define an option
1111+that hasn’t been declared in any module. A option declaration
1212+generally looks like this:
1313+1414+<programlisting>
1515+options = {
1616+ <replaceable>name</replaceable> = mkOption {
1717+ type = <replaceable>type specification</replaceable>;
1818+ default = <replaceable>default value</replaceable>;
1919+ example = <replaceable>example value</replaceable>;
2020+ description = "<replaceable>Description for use in the NixOS manual.</replaceable>";
2121+ };
2222+};
2323+</programlisting>
2424+2525+</para>
2626+2727+<para>The function <varname>mkOption</varname> accepts the following arguments.
2828+2929+<variablelist>
3030+3131+ <varlistentry>
3232+ <term><varname>type</varname></term>
3333+ <listitem>
3434+ <para>The type of the option (see below). It may be omitted,
3535+ but that’s not advisable since it may lead to errors that are
3636+ hard to diagnose.</para>
3737+ </listitem>
3838+ </varlistentry>
3939+4040+ <varlistentry>
4141+ <term><varname>default</varname></term>
4242+ <listitem>
4343+ <para>The default value used if no value is defined by any
4444+ module. A default is not required; in that case, if the option
4545+ value is ever used, an error will be thrown.</para>
4646+ </listitem>
4747+ </varlistentry>
4848+4949+ <varlistentry>
5050+ <term><varname>example</varname></term>
5151+ <listitem>
5252+ <para>An example value that will be shown in the NixOS manual.</para>
5353+ </listitem>
5454+ </varlistentry>
5555+5656+ <varlistentry>
5757+ <term><varname>description</varname></term>
5858+ <listitem>
5959+ <para>A textual description of the option, in DocBook format,
6060+ that will be included in the NixOS manual.</para>
6161+ </listitem>
6262+ </varlistentry>
6363+6464+</variablelist>
6565+6666+</para>
6767+6868+<para>Here is a non-exhaustive list of option types:
6969+7070+<variablelist>
7171+7272+ <varlistentry>
7373+ <term><varname>types.bool</varname></term>
7474+ <listitem>
7575+ <para>A Boolean.</para>
7676+ </listitem>
7777+ </varlistentry>
7878+7979+ <varlistentry>
8080+ <term><varname>types.int</varname></term>
8181+ <listitem>
8282+ <para>An integer.</para>
8383+ </listitem>
8484+ </varlistentry>
8585+8686+ <varlistentry>
8787+ <term><varname>types.str</varname></term>
8888+ <listitem>
8989+ <para>A string.</para>
9090+ </listitem>
9191+ </varlistentry>
9292+9393+ <varlistentry>
9494+ <term><varname>types.lines</varname></term>
9595+ <listitem>
9696+ <para>A string. If there are multiple definitions, they are
9797+ concatenated, with newline characters in between.</para>
9898+ </listitem>
9999+ </varlistentry>
100100+101101+ <varlistentry>
102102+ <term><varname>types.path</varname></term>
103103+ <listitem>
104104+ <para>A path, defined as anything that, when coerced to a
105105+ string, starts with a slash. This includes derivations.</para>
106106+ </listitem>
107107+ </varlistentry>
108108+109109+ <varlistentry>
110110+ <term><varname>types.listOf</varname> <replaceable>t</replaceable></term>
111111+ <listitem>
112112+ <para>A list of elements of type <replaceable>t</replaceable>
113113+ (e.g., <literal>types.listOf types.str</literal> is a list of
114114+ strings). Multiple definitions are concatenated together.</para>
115115+ </listitem>
116116+ </varlistentry>
117117+118118+ <varlistentry>
119119+ <term><varname>types.attrsOf</varname> <replaceable>t</replaceable></term>
120120+ <listitem>
121121+ <para>A set of elements of type <replaceable>t</replaceable>
122122+ (e.g., <literal>types.attrsOf types.int</literal> is a set of
123123+ name/value pairs, the values being integers).</para>
124124+ </listitem>
125125+ </varlistentry>
126126+127127+ <varlistentry>
128128+ <term><varname>types.nullOr</varname> <replaceable>t</replaceable></term>
129129+ <listitem>
130130+ <para>Either the value <literal>null</literal> or something of
131131+ type <replaceable>t</replaceable>.</para>
132132+ </listitem>
133133+ </varlistentry>
134134+135135+</variablelist>
136136+137137+You can also create new types using the function
138138+<varname>mkOptionType</varname>. See
139139+<filename>lib/types.nix</filename> in Nixpkgs for details.</para>
140140+141141+</section>
+112
nixos/doc/manual/development/option-def.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-option-definitions">
66+77+<title>Option Definitions</title>
88+99+<para>Option definitions are generally straight-forward bindings of values to option names, like
1010+1111+<programlisting>
1212+config = {
1313+ services.httpd.enable = true;
1414+};
1515+</programlisting>
1616+1717+However, sometimes you need to wrap an option definition or set of
1818+option definitions in a <emphasis>property</emphasis> to achieve
1919+certain effects:</para>
2020+2121+<simplesect><title>Delaying Conditionals</title>
2222+2323+<para>If a set of option definitions is conditional on the value of
2424+another option, you may need to use <varname>mkIf</varname>.
2525+Consider, for instance:
2626+2727+<programlisting>
2828+config = if config.services.httpd.enable then {
2929+ environment.systemPackages = [ <replaceable>...</replaceable> ];
3030+ <replaceable>...</replaceable>
3131+} else {};
3232+</programlisting>
3333+3434+This definition will cause Nix to fail with an “infinite recursion”
3535+error. Why? Because the value of
3636+<option>config.services.httpd.enable</option> depends on the value
3737+being constructed here. After all, you could also write the clearly
3838+circular and contradictory:
3939+<programlisting>
4040+config = if config.services.httpd.enable then {
4141+ services.httpd.enable = false;
4242+} else {
4343+ services.httpd.enable = true;
4444+};
4545+</programlisting>
4646+4747+The solution is to write:
4848+4949+<programlisting>
5050+config = mkIf config.services.httpd.enable {
5151+ environment.systemPackages = [ <replaceable>...</replaceable> ];
5252+ <replaceable>...</replaceable>
5353+};
5454+</programlisting>
5555+5656+The special function <varname>mkIf</varname> causes the evaluation of
5757+the conditional to be “pushed down” into the individual definitions,
5858+as if you had written:
5959+6060+<programlisting>
6161+config = {
6262+ environment.systemPackages = if config.services.httpd.enable then [ <replaceable>...</replaceable> ] else [];
6363+ <replaceable>...</replaceable>
6464+};
6565+</programlisting>
6666+6767+</para>
6868+6969+</simplesect>
7070+7171+<simplesect><title>Setting Priorities</title>
7272+7373+<para>A module can override the definitions of an option in other
7474+modules by setting a <emphasis>priority</emphasis>. All option
7575+definitions that do not have the lowest priority value are discarded.
7676+By default, option definitions have priority 1000. You can specify an
7777+explicit priority by using <varname>mkOverride</varname>, e.g.
7878+7979+<programlisting>
8080+services.openssh.enable = mkOverride 10 false;
8181+</programlisting>
8282+8383+This definition causes all other definitions with priorities above 10
8484+to be discarded. The function <varname>mkForce</varname> is
8585+equal to <varname>mkOverride 50</varname>.</para>
8686+8787+</simplesect>
8888+8989+<simplesect><title>Merging Configurations</title>
9090+9191+<para>In conjunction with <literal>mkIf</literal>, it is sometimes
9292+useful for a module to return multiple sets of option definitions, to
9393+be merged together as if they were declared in separate modules. This
9494+can be done using <varname>mkMerge</varname>:
9595+9696+<programlisting>
9797+config = mkMerge
9898+ [ # Unconditional stuff.
9999+ { environment.systemPackages = [ <replaceable>...</replaceable> ];
100100+ }
101101+ # Conditional stuff.
102102+ (mkIf config.services.bla.enable {
103103+ environment.systemPackages = [ <replaceable>...</replaceable> ];
104104+ })
105105+ ];
106106+</programlisting>
107107+108108+</para>
109109+110110+</simplesect>
111111+112112+</section>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-running-nixos-tests">
66+77+<title>Running Tests</title>
88+99+<para>You can run tests using <command>nix-build</command>. For
1010+example, to run the test <filename
1111+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix">login.nix</filename>,
1212+you just do:
1313+1414+<screen>
1515+$ nix-build '<nixpkgs/nixos/tests/login.nix>'
1616+</screen>
1717+1818+or, if you don’t want to rely on <envar>NIX_PATH</envar>:
1919+2020+<screen>
2121+$ cd /my/nixpkgs/nixos/tests
2222+$ nix-build login.nix
2323+…
2424+running the VM test script
2525+machine: QEMU running (pid 8841)
2626+…
2727+6 out of 6 tests succeeded
2828+</screen>
2929+3030+After building/downloading all required dependencies, this will
3131+perform a build that starts a QEMU/KVM virtual machine containing a
3232+NixOS system. The virtual machine mounts the Nix store of the host;
3333+this makes VM creation very fast, as no disk image needs to be
3434+created. Afterwards, you can view a pretty-printed log of the test:
3535+3636+<screen>
3737+$ firefox result/log.html
3838+</screen>
3939+4040+</para>
4141+4242+<para>It is also possible to run the test environment interactively,
4343+allowing you to experiment with the VMs. For example:
4444+4545+<screen>
4646+$ nix-build login.nix -A driver
4747+$ ./result/bin/nixos-run-vms
4848+</screen>
4949+5050+The script <command>nixos-run-vms</command> starts the virtual
5151+machines defined by test. The root file system of the VMs is created
5252+on the fly and kept across VM restarts in
5353+<filename>./</filename><varname>hostname</varname><filename>.qcow2</filename>.</para>
5454+5555+<para>Finally, the test itself can be run interactively. This is
5656+particularly useful when developing or debugging a test:
5757+5858+<screen>
5959+$ nix-build tests/ -A nfs.driver
6060+$ ./result/bin/nixos-test-driver
6161+starting VDE switch for network 1
6262+>
6363+</screen>
6464+6565+You can then take any Perl statement, e.g.
6666+6767+<screen>
6868+> startAll
6969+> $machine->succeed("touch /tmp/foo")
7070+</screen>
7171+7272+The function <command>testScript</command> executes the entire test
7373+script and drops you back into the test driver command line upon its
7474+completion. This allows you to inspect the state of the VMs after the
7575+test (e.g. to debug the test script).</para>
7676+7777+</section>
+95
nixos/doc/manual/development/sources.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-getting-sources">
66+77+<title>Getting the Sources</title>
88+99+<para>By default, NixOS’s <command>nixos-rebuild</command> command
1010+uses the NixOS and Nixpkgs sources provided by the
1111+<literal>nixos-unstable</literal> channel (kept in
1212+<filename>/nix/var/nix/profiles/per-user/root/channels/nixos</filename>).
1313+To modify NixOS, however, you should check out the latest sources from
1414+Git. This is done using the following command:
1515+1616+<screen>
1717+$ nixos-checkout <replaceable>/my/sources</replaceable>
1818+</screen>
1919+2020+or
2121+2222+<screen>
2323+$ mkdir -p <replaceable>/my/sources</replaceable>
2424+$ cd <replaceable>/my/sources</replaceable>
2525+$ nix-env -i git
2626+$ git clone git://github.com/NixOS/nixpkgs.git
2727+</screen>
2828+2929+This will check out the latest NixOS sources to
3030+<filename><replaceable>/my/sources</replaceable>/nixpkgs/nixos</filename>
3131+and the Nixpkgs sources to
3232+<filename><replaceable>/my/sources</replaceable>/nixpkgs</filename>.
3333+(The NixOS source tree lives in a subdirectory of the Nixpkgs
3434+repository.)</para>
3535+3636+<para>It’s often inconvenient to develop directly on the master
3737+branch, since if somebody has just committed (say) a change to GCC,
3838+then the binary cache may not have caught up yet and you’ll have to
3939+rebuild everything from source. So you may want to create a local
4040+branch based on your current NixOS version:
4141+4242+<screen>
4343+$ nixos-version
4444+14.04.273.ea1952b (Baboon)
4545+4646+$ git checkout -b local ea1952b
4747+</screen>
4848+4949+Or, to base your local branch on the latest version available in the
5050+NixOS channel:
5151+5252+<screen>
5353+$ curl -sI http://nixos.org/channels/nixos-unstable/ | grep Location
5454+Location: http://releases.nixos.org/nixos/unstable/nixos-14.10pre43986.acaf4a6/
5555+5656+$ git checkout -b local acaf4a6
5757+</screen>
5858+5959+You can then use <command>git rebase</command> to sync your local
6060+branch with the upstream branch, and use <command>git
6161+cherry-pick</command> to copy commits from your local branch to the
6262+upstream branch.</para>
6363+6464+<para>If you want to rebuild your system using your (modified)
6565+sources, you need to tell <command>nixos-rebuild</command> about them
6666+using the <option>-I</option> flag:
6767+6868+<screen>
6969+$ nixos-rebuild switch -I nixpkgs=<replaceable>/my/sources</replaceable>/nixpkgs
7070+</screen>
7171+7272+</para>
7373+7474+<para>If you want <command>nix-env</command> to use the expressions in
7575+<replaceable>/my/sources</replaceable>, use <command>nix-env -f
7676+<replaceable>/my/sources</replaceable>/nixpkgs</command>, or change
7777+the default by adding a symlink in
7878+<filename>~/.nix-defexpr</filename>:
7979+8080+<screen>
8181+$ ln -s <replaceable>/my/sources</replaceable>/nixpkgs ~/.nix-defexpr/nixpkgs
8282+</screen>
8383+8484+You may want to delete the symlink
8585+<filename>~/.nix-defexpr/channels_root</filename> to prevent root’s
8686+NixOS channel from clashing with your own tree.</para>
8787+8888+<!-- FIXME: not sure what this means.
8989+<para>You should not pass the base directory
9090+<filename><replaceable>/my/sources</replaceable></filename>
9191+to <command>nix-env</command>, as it will break after interpreting expressions
9292+in <filename>nixos/</filename> as packages.</para>
9393+-->
9494+9595+</chapter>
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-testing-installer">
66+77+<title>Testing the Installer</title>
88+99+<para>Building, burning, and booting from an installation CD is rather
1010+tedious, so here is a quick way to see if the installer works
1111+properly:
1212+1313+<screen>
1414+$ nix-build -A config.system.build.nixos-install
1515+$ mount -t tmpfs none /mnt
1616+$ ./result/bin/nixos-install</screen>
1717+1818+To start a login shell in the new NixOS installation in
1919+<filename>/mnt</filename>:
2020+2121+<screen>
2222+$ ./result/bin/nixos-install --chroot
2323+</screen>
2424+2525+</para>
2626+2727+</chapter>
+175
nixos/doc/manual/development/writing-modules.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-writing-modules">
66+77+<title>Writing NixOS Modules</title>
88+99+<para>NixOS has a modular system for declarative configuration. This
1010+system combines multiple <emphasis>modules</emphasis> to produce the
1111+full system configuration. One of the modules that constitute the
1212+configuration is <filename>/etc/nixos/configuration.nix</filename>.
1313+Most of the others live in the <link
1414+xlink:href="https://github.com/NixOS/nixpkgs/tree/master/nixos/modules"><filename>nixos/modules</filename></link>
1515+subdirectory of the Nixpkgs tree.</para>
1616+1717+<para>Each NixOS module is a file that handles one logical aspect of
1818+the configuration, such as a specific kind of hardware, a service, or
1919+network settings. A module configuration does not have to handle
2020+everything from scratch; it can use the functionality provided by
2121+other modules for its implementation. Thus a module can
2222+<emphasis>declare</emphasis> options that can be used by other
2323+modules, and conversely can <emphasis>define</emphasis> options
2424+provided by other modules in its own implementation. For example, the
2525+module <link
2626+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/security/pam.nix"><filename>pam.nix</filename></link>
2727+declares the option <option>security.pam.services</option> that allows
2828+other modules (e.g. <link
2929+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/networking/ssh/sshd.nix"><filename>sshd.nix</filename></link>)
3030+to define PAM services; and it defines the option
3131+<option>environment.etc</option> (declared by <link
3232+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/system/etc/etc.nix"><filename>etc.nix</filename></link>)
3333+to cause files to be created in
3434+<filename>/etc/pam.d</filename>.</para>
3535+3636+<para xml:id="para-module-syn">In <xref
3737+linkend="sec-configuration-syntax"/>, we saw the following structure
3838+of NixOS modules:
3939+4040+<programlisting>
4141+{ config, pkgs, ... }:
4242+4343+{ <replaceable>option definitions</replaceable>
4444+}
4545+</programlisting>
4646+4747+This is actually an <emphasis>abbreviated</emphasis> form of module
4848+that only defines options, but does not declare any. The structure of
4949+full NixOS modules is shown in <xref linkend='ex-module-syntax' />.</para>
5050+5151+<example xml:id='ex-module-syntax'><title>Structure of NixOS Modules</title>
5252+<programlisting>
5353+{ config, pkgs, ... }: <co xml:id='module-syntax-1' />
5454+5555+{
5656+ imports =
5757+ [ <replaceable>paths of other modules</replaceable> <co xml:id='module-syntax-2' />
5858+ ];
5959+6060+ options = {
6161+ <replaceable>option declarations</replaceable> <co xml:id='module-syntax-3' />
6262+ };
6363+6464+ config = {
6565+ <replaceable>option definitions</replaceable> <co xml:id='module-syntax-4' />
6666+ };
6767+}</programlisting>
6868+</example>
6969+7070+<para>The meaning of each part is as follows.
7171+7272+<calloutlist>
7373+ <callout arearefs='module-syntax-1'>
7474+ <para>This line makes the current Nix expression a function. The
7575+ variable <varname>pkgs</varname> contains Nixpkgs, while
7676+ <varname>config</varname> contains the full system configuration.
7777+ This line can be omitted if there is no reference to
7878+ <varname>pkgs</varname> and <varname>config</varname> inside the
7979+ module.</para>
8080+ </callout>
8181+8282+ <callout arearefs='module-syntax-2'>
8383+ <para>This list enumerates the paths to other NixOS modules that
8484+ should be included in the evaluation of the system configuration.
8585+ A default set of modules is defined in the file
8686+ <filename>modules/module-list.nix</filename>. These don't need to
8787+ be added in the import list.</para>
8888+ </callout>
8989+9090+ <callout arearefs='module-syntax-3'>
9191+ <para>The attribute <varname>options</varname> is a nested set of
9292+ <emphasis>option declarations</emphasis> (described below).</para>
9393+ </callout>
9494+9595+ <callout arearefs='module-syntax-4'>
9696+ <para>The attribute <varname>config</varname> is a nested set of
9797+ <emphasis>option definitions</emphasis> (also described
9898+ below).</para>
9999+ </callout>
100100+</calloutlist>
101101+102102+</para>
103103+104104+<para><xref linkend='locate-example' /> shows a module that handles
105105+the regular update of the “locate” database, an index of all files in
106106+the file system. This module declares two options that can be defined
107107+by other modules (typically the user’s
108108+<filename>configuration.nix</filename>):
109109+<option>services.locate.enable</option> (whether the database should
110110+be updated) and <option>services.locate.period</option> (when the
111111+update should be done). It implements its functionality by defining
112112+two options declared by other modules:
113113+<option>systemd.services</option> (the set of all systemd services)
114114+and <option>services.cron.systemCronJobs</option> (the list of
115115+commands to be executed periodically by <command>cron</command>).</para>
116116+117117+<example xml:id='locate-example'><title>NixOS Module for the “locate” Service</title>
118118+<programlisting>
119119+{ config, lib, pkgs, ... }:
120120+121121+with lib;
122122+123123+let locatedb = "/var/cache/locatedb"; in
124124+125125+{
126126+ options = {
127127+128128+ services.locate = {
129129+130130+ enable = mkOption {
131131+ type = types.bool;
132132+ default = false;
133133+ description = ''
134134+ If enabled, NixOS will periodically update the database of
135135+ files used by the <command>locate</command> command.
136136+ '';
137137+ };
138138+139139+ period = mkOption {
140140+ type = types.str;
141141+ default = "15 02 * * *";
142142+ description = ''
143143+ This option defines (in the format used by cron) when the
144144+ locate database is updated. The default is to update at
145145+ 02:15 at night every day.
146146+ '';
147147+ };
148148+149149+ };
150150+151151+ };
152152+153153+ config = {
154154+155155+ systemd.services.update-locatedb =
156156+ { description = "Update Locate Database";
157157+ path = [ pkgs.su ];
158158+ script =
159159+ ''
160160+ mkdir -m 0755 -p $(dirname ${locatedb})
161161+ exec updatedb --localuser=nobody --output=${locatedb} --prunepaths='/tmp /var/tmp /media /run'
162162+ '';
163163+ };
164164+165165+ services.cron.systemCronJobs = optional config.services.locate.enable
166166+ "${config.services.locate.period} root ${config.systemd.package}/bin/systemctl start update-locatedb.service";
167167+168168+ };
169169+}</programlisting>
170170+</example>
171171+172172+<xi:include href="option-declarations.xml" />
173173+<xi:include href="option-def.xml" />
174174+175175+</chapter>
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-writing-nixos-tests">
66+77+<title>Writing Tests</title>
88+99+<para>A NixOS test is a Nix expression that has the following structure:
1010+1111+<programlisting>
1212+import ./make-test.nix {
1313+1414+ # Either the configuration of a single machine:
1515+ machine =
1616+ { config, pkgs, ... }:
1717+ { <replaceable>configuration…</replaceable>
1818+ };
1919+2020+ # Or a set of machines:
2121+ nodes =
2222+ { <replaceable>machine1</replaceable> =
2323+ { config, pkgs, ... }: { <replaceable>…</replaceable> };
2424+ <replaceable>machine2</replaceable> =
2525+ { config, pkgs, ... }: { <replaceable>…</replaceable> };
2626+ …
2727+ };
2828+2929+ testScript =
3030+ ''
3131+ <replaceable>Perl code…</replaceable>
3232+ '';
3333+}
3434+</programlisting>
3535+3636+The attribute <literal>testScript</literal> is a bit of Perl code that
3737+executes the test (described below). During the test, it will start
3838+one or more virtual machines, the configuration of which is described
3939+by the attribute <literal>machine</literal> (if you need only one
4040+machine in your test) or by the attribute <literal>nodes</literal> (if
4141+you need multiple machines). For instance, <filename
4242+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/login.nix">login.nix</filename>
4343+only needs a single machine to test whether users can log in on the
4444+virtual console, whether device ownership is correctly maintained when
4545+switching between consoles, and so on. On the other hand, <filename
4646+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nfs.nix">nfs.nix</filename>,
4747+which tests NFS client and server functionality in the Linux kernel
4848+(including whether locks are maintained across server crashes),
4949+requires three machines: a server and two clients.</para>
5050+5151+<para>There are a few special NixOS configuration options for test
5252+VMs:
5353+5454+<!-- FIXME: would be nice to generate this automatically. -->
5555+5656+<variablelist>
5757+5858+ <varlistentry>
5959+ <term><option>virtualisation.memorySize</option></term>
6060+ <listitem><para>The memory of the VM in
6161+ megabytes.</para></listitem>
6262+ </varlistentry>
6363+6464+ <varlistentry>
6565+ <term><option>virtualisation.vlans</option></term>
6666+ <listitem><para>The virtual networks to which the VM is
6767+ connected. See <filename
6868+ xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/nat.nix">nat.nix</filename>
6969+ for an example.</para></listitem>
7070+ </varlistentry>
7171+7272+ <varlistentry>
7373+ <term><option>virtualisation.writableStore</option></term>
7474+ <listitem><para>By default, the Nix store in the VM is not
7575+ writable. If you enable this option, a writable union file system
7676+ is mounted on top of the Nix store to make it appear
7777+ writable. This is necessary for tests that run Nix operations that
7878+ modify the store.</para></listitem>
7979+ </varlistentry>
8080+8181+</variablelist>
8282+8383+For more options, see the module <filename
8484+xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/virtualisation/qemu-vm.nix">qemu-vm.nix</filename>.</para>
8585+8686+<para>The test script is a sequence of Perl statements that perform
8787+various actions, such as starting VMs, executing commands in the VMs,
8888+and so on. Each virtual machine is represented as an object stored in
8989+the variable <literal>$<replaceable>name</replaceable></literal>,
9090+where <replaceable>name</replaceable> is the identifier of the machine
9191+(which is just <literal>machine</literal> if you didn’t specify
9292+multiple machines using the <literal>nodes</literal> attribute). For
9393+instance, the following starts the machine, waits until it has
9494+finished booting, then executes a command and checks that the output
9595+is more-or-less correct:
9696+9797+<programlisting>
9898+$machine->start;
9999+$machine->waitForUnit("default.target");
100100+$machine->succeed("uname") =~ /Linux/;
101101+</programlisting>
102102+103103+The first line is actually unnecessary; machines are implicitly
104104+started when you first execute an action on them (such as
105105+<literal>waitForUnit</literal> or <literal>succeed</literal>). If you
106106+have multiple machines, you can speed up the test by starting them in
107107+parallel:
108108+109109+<programlisting>
110110+startAll;
111111+</programlisting>
112112+113113+</para>
114114+115115+<para>The following methods are available on machine objects:
116116+117117+<variablelist>
118118+119119+ <varlistentry>
120120+ <term><methodname>start</methodname></term>
121121+ <listitem><para>Start the virtual machine. This method is
122122+ asynchronous — it does not wait for the machine to finish
123123+ booting.</para></listitem>
124124+ </varlistentry>
125125+126126+ <varlistentry>
127127+ <term><methodname>shutdown</methodname></term>
128128+ <listitem><para>Shut down the machine, waiting for the VM to
129129+ exit.</para></listitem>
130130+ </varlistentry>
131131+132132+ <varlistentry>
133133+ <term><methodname>crash</methodname></term>
134134+ <listitem><para>Simulate a sudden power failure, by telling the VM
135135+ to exit immediately.</para></listitem>
136136+ </varlistentry>
137137+138138+ <varlistentry>
139139+ <term><methodname>block</methodname></term>
140140+ <listitem><para>Simulate unplugging the Ethernet cable that
141141+ connects the machine to the other machines.</para></listitem>
142142+ </varlistentry>
143143+144144+ <varlistentry>
145145+ <term><methodname>unblock</methodname></term>
146146+ <listitem><para>Undo the effect of
147147+ <methodname>block</methodname>.</para></listitem>
148148+ </varlistentry>
149149+150150+ <varlistentry>
151151+ <term><methodname>screenshot</methodname></term>
152152+ <listitem><para>Take a picture of the display of the virtual
153153+ machine, in PNG format. The screenshot is linked from the HTML
154154+ log.</para></listitem>
155155+ </varlistentry>
156156+157157+ <varlistentry>
158158+ <term><methodname>sendMonitorCommand</methodname></term>
159159+ <listitem><para>Send a command to the QEMU monitor. This is rarely
160160+ used, but allows doing stuff such as attaching virtual USB disks
161161+ to a running machine.</para></listitem>
162162+ </varlistentry>
163163+164164+ <varlistentry>
165165+ <term><methodname>sendKeys</methodname></term>
166166+ <listitem><para>Simulate pressing keys on the virtual keyboard,
167167+ e.g., <literal>sendKeys("ctrl-alt-delete")</literal>.</para></listitem>
168168+ </varlistentry>
169169+170170+ <varlistentry>
171171+ <term><methodname>sendChars</methodname></term>
172172+ <listitem><para>Simulate typing a sequence of characters on the
173173+ virtual keyboard, e.g., <literal>sendKeys("foobar\n")</literal>
174174+ will type the string <literal>foobar</literal> followed by the
175175+ Enter key.</para></listitem>
176176+ </varlistentry>
177177+178178+ <varlistentry>
179179+ <term><methodname>execute</methodname></term>
180180+ <listitem><para>Execute a shell command, returning a list
181181+ <literal>(<replaceable>status</replaceable>,
182182+ <replaceable>stdout</replaceable>)</literal>.</para></listitem>
183183+ </varlistentry>
184184+185185+ <varlistentry>
186186+ <term><methodname>succeed</methodname></term>
187187+ <listitem><para>Execute a shell command, raising an exception if
188188+ the exit status is not zero, otherwise returning the standard
189189+ output.</para></listitem>
190190+ </varlistentry>
191191+192192+ <varlistentry>
193193+ <term><methodname>fail</methodname></term>
194194+ <listitem><para>Like <methodname>succeed</methodname>, but raising
195195+ an exception if the command returns a zero status.</para></listitem>
196196+ </varlistentry>
197197+198198+ <varlistentry>
199199+ <term><methodname>waitUntilSucceeds</methodname></term>
200200+ <listitem><para>Repeat a shell command with 1-second intervals
201201+ until it succeeds.</para></listitem>
202202+ </varlistentry>
203203+204204+ <varlistentry>
205205+ <term><methodname>waitUntilFails</methodname></term>
206206+ <listitem><para>Repeat a shell command with 1-second intervals
207207+ until it fails.</para></listitem>
208208+ </varlistentry>
209209+210210+ <varlistentry>
211211+ <term><methodname>waitForUnit</methodname></term>
212212+ <listitem><para>Wait until the specified systemd unit has reached
213213+ the “active” state.</para></listitem>
214214+ </varlistentry>
215215+216216+ <varlistentry>
217217+ <term><methodname>waitForFile</methodname></term>
218218+ <listitem><para>Wait until the specified file
219219+ exists.</para></listitem>
220220+ </varlistentry>
221221+222222+ <varlistentry>
223223+ <term><methodname>waitForOpenPort</methodname></term>
224224+ <listitem><para>Wait until a process is listening on the given TCP
225225+ port (on <literal>localhost</literal>, at least).</para></listitem>
226226+ </varlistentry>
227227+228228+ <varlistentry>
229229+ <term><methodname>waitForClosedPort</methodname></term>
230230+ <listitem><para>Wait until nobody is listening on the given TCP
231231+ port.</para></listitem>
232232+ </varlistentry>
233233+234234+ <varlistentry>
235235+ <term><methodname>waitForX</methodname></term>
236236+ <listitem><para>Wait until the X11 server is accepting
237237+ connections.</para></listitem>
238238+ </varlistentry>
239239+240240+ <varlistentry>
241241+ <term><methodname>waitForWindow</methodname></term>
242242+ <listitem><para>Wait until an X11 window has appeared whose name
243243+ matches the given regular expression, e.g.,
244244+ <literal>waitForWindow(qr/Terminal/)</literal>.</para></listitem>
245245+ </varlistentry>
246246+247247+</variablelist>
248248+249249+</para>
250250+251251+</section>
-570
nixos/doc/manual/installation.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-installation">
44-55-<title>Installing NixOS</title>
66-77-88-<!--===============================================================-->
99-1010-<section xml:id="sec-obtaining">
1111-1212-<title>Obtaining NixOS</title>
1313-1414-<para>NixOS ISO images can be downloaded from the <link
1515-xlink:href="http://nixos.org/nixos/download.html">NixOS
1616-homepage</link>. These can be burned onto a CD. It is also possible
1717-to copy them onto a USB stick and install NixOS from there. For
1818-details, see the <link
1919-xlink:href="https://nixos.org/wiki/Installing_NixOS_from_a_USB_stick">NixOS
2020-Wiki</link>.</para>
2121-2222-<para>As an alternative to installing NixOS yourself, you can get a
2323-running NixOS system through several other means:
2424-2525-<itemizedlist>
2626- <listitem>
2727- <para>Using virtual appliances in Open Virtualization Format (OVF)
2828- that can be imported into VirtualBox. These are available from
2929- the <link xlink:href="http://nixos.org/nixos/download.html">NixOS
3030- homepage</link>.</para>
3131- </listitem>
3232- <listitem>
3333- <para>Using AMIs for Amazon’s EC2. To find one for your region
3434- and instance type, please refer to the <link
3535- xlink:href="https://github.com/NixOS/nixops/blob/master/nix/ec2-amis.nix">list
3636- of most recent AMIs</link>.</para>
3737- </listitem>
3838- <listitem>
3939- <para>Using NixOps, the NixOS-based cloud deployment tool, which
4040- allows you to provision VirtualBox and EC2 NixOS instances from
4141- declarative specifications. Check out the <link
4242- xlink:href="https://github.com/NixOS/nixops">NixOps
4343- homepage</link> for details.</para>
4444- </listitem>
4545-</itemizedlist>
4646-4747-</para>
4848-4949-</section>
5050-5151-5252-<!--===============================================================-->
5353-5454-<section xml:id="sec-installation">
5555-5656-<title>Installation</title>
5757-5858-<orderedlist>
5959-6060- <listitem><para>Boot from the CD.</para></listitem>
6161-6262- <listitem><para>The CD contains a basic NixOS installation. (It
6363- also contains Memtest86+, useful if you want to test new hardware.)
6464- When it’s finished booting, it should have detected most of your
6565- hardware and brought up networking (check
6666- <command>ifconfig</command>). Networking is necessary for the
6767- installer, since it will download lots of stuff (such as source
6868- tarballs or Nixpkgs channel binaries). It’s best if you have a DHCP
6969- server on your network. Otherwise configure networking manually
7070- using <command>ifconfig</command>.</para></listitem>
7171-7272- <listitem><para>The NixOS manual is available on virtual console 8
7373- (press Alt+F8 to access).</para></listitem>
7474-7575- <listitem><para>Login as <literal>root</literal> and the empty
7676- password.</para></listitem>
7777-7878- <listitem><para>If you downloaded the graphical ISO image, you can
7979- run <command>start display-manager</command> to start KDE.</para></listitem>
8080-8181- <listitem><para>The NixOS installer doesn’t do any partitioning or
8282- formatting yet, so you need to that yourself. Use the following
8383- commands:
8484-8585- <itemizedlist>
8686-8787- <listitem><para>For partitioning:
8888- <command>fdisk</command>.</para></listitem>
8989-9090- <listitem><para>For initialising Ext4 partitions:
9191- <command>mkfs.ext4</command>. It is recommended that you assign a
9292- unique symbolic label to the file system using the option
9393- <option>-L <replaceable>label</replaceable></option>, since this
9494- makes the file system configuration independent from device
9595- changes. For example:
9696-9797-<screen>
9898-$ mkfs.ext4 -L nixos /dev/sda1</screen>
9999-100100- </para></listitem>
101101-102102- <listitem><para>For creating swap partitions:
103103- <command>mkswap</command>. Again it’s recommended to assign a
104104- label to the swap partition: <option>-L
105105- <replaceable>label</replaceable></option>.</para></listitem>
106106-107107- <listitem><para>For creating LVM volumes, the LVM commands, e.g.,
108108-109109-<screen>
110110-$ pvcreate /dev/sda1 /dev/sdb1
111111-$ vgcreate MyVolGroup /dev/sda1 /dev/sdb1
112112-$ lvcreate --size 2G --name bigdisk MyVolGroup
113113-$ lvcreate --size 1G --name smalldisk MyVolGroup</screen>
114114-115115- </para></listitem>
116116-117117- <listitem><para>For creating software RAID devices, use
118118- <command>mdadm</command>.</para></listitem>
119119-120120- </itemizedlist>
121121-122122- </para></listitem>
123123-124124- <listitem><para>Mount the target file system on which NixOS should
125125- be installed on <filename>/mnt</filename>, e.g.
126126-127127-<screen>
128128-$ mount /dev/disk/by-label/nixos /mnt
129129-</screen>
130130-131131- </para></listitem>
132132-133133- <listitem><para>If your machine has a limited amount of memory, you
134134- may want to activate swap devices now (<command>swapon
135135- <replaceable>device</replaceable></command>). The installer (or
136136- rather, the build actions that it may spawn) may need quite a bit of
137137- RAM, depending on your configuration.</para></listitem>
138138-139139- <listitem>
140140-141141- <para>You now need to create a file
142142- <filename>/mnt/etc/nixos/configuration.nix</filename> that
143143- specifies the intended configuration of the system. This is
144144- because NixOS has a <emphasis>declarative</emphasis> configuration
145145- model: you create or edit a description of the desired
146146- configuration of your system, and then NixOS takes care of making
147147- it happen. The syntax of the NixOS configuration file is
148148- described in <xref linkend="sec-configuration-syntax"/>, while a
149149- list of available configuration options appears in <xref
150150- linkend="ch-options"/>. A minimal example is shown in <xref
151151- linkend="ex-config"/>.</para>
152152-153153- <para>The command <command>nixos-generate-config</command> can
154154- generate an initial configuration file for you:
155155-156156-<screen>
157157-$ nixos-generate-config --root /mnt</screen>
158158-159159- You should then edit
160160- <filename>/mnt/etc/nixos/configuration.nix</filename> to suit your
161161- needs:
162162-163163-<screen>
164164-$ nano /mnt/etc/nixos/configuration.nix
165165-</screen>
166166-167167- The <command>vim</command> text editor is also available.</para>
168168-169169- <para>You <emphasis>must</emphasis> set the option
170170- <option>boot.loader.grub.device</option> to specify on which disk
171171- the GRUB boot loader is to be installed. Without it, NixOS cannot
172172- boot.</para>
173173-174174- <para>Another critical option is <option>fileSystems</option>,
175175- specifying the file systems that need to be mounted by NixOS.
176176- However, you typically don’t need to set it yourself, because
177177- <command>nixos-generate-config</command> sets it automatically in
178178- <filename>/mnt/etc/nixos/hardware-configuration.nix</filename>
179179- from your currently mounted file systems. (The configuration file
180180- <filename>hardware-configuration.nix</filename> is included from
181181- <filename>configuration.nix</filename> and will be overwritten by
182182- future invocations of <command>nixos-generate-config</command>;
183183- thus, you generally should not modify it.)</para>
184184-185185- <note><para>Depending on your hardware configuration or type of
186186- file system, you may need to set the option
187187- <option>boot.initrd.kernelModules</option> to include the kernel
188188- modules that are necessary for mounting the root file system,
189189- otherwise the installed system will not be able to boot. (If this
190190- happens, boot from the CD again, mount the target file system on
191191- <filename>/mnt</filename>, fix
192192- <filename>/mnt/etc/nixos/configuration.nix</filename> and rerun
193193- <filename>nixos-install</filename>.) In most cases,
194194- <command>nixos-generate-config</command> will figure out the
195195- required modules.</para></note>
196196-197197- <para>Examples of real-world NixOS configuration files can be
198198- found at <link
199199- xlink:href="https://nixos.org/repos/nix/configurations/trunk/"/>.</para>
200200-201201- </listitem>
202202-203203- <listitem><para>Do the installation:
204204-205205-<screen>
206206-$ nixos-install</screen>
207207-208208- Cross fingers. If this fails due to a temporary problem (such as
209209- a network issue while downloading binaries from the NixOS binary
210210- cache), you can just re-run <command>nixos-install</command>.
211211- Otherwise, fix your <filename>configuration.nix</filename> and
212212- then re-run <command>nixos-install</command>.</para>
213213-214214- <para>As the last step, <command>nixos-install</command> will ask
215215- you to set the password for the <literal>root</literal> user, e.g.
216216-217217-<screen>
218218-setting root password...
219219-Enter new UNIX password: ***
220220-Retype new UNIX password: ***
221221-</screen>
222222-223223- </para>
224224-225225- </listitem>
226226-227227- <listitem><para>If everything went well:
228228-229229-<screen>
230230-$ reboot</screen>
231231-232232- </para></listitem>
233233-234234- <listitem>
235235-236236- <para>You should now be able to boot into the installed NixOS.
237237- The GRUB boot menu shows a list of <emphasis>available
238238- configurations</emphasis> (initially just one). Every time you
239239- change the NixOS configuration (see <xref
240240- linkend="sec-changing-config" />), a new item appears in the menu.
241241- This allows you to easily roll back to another configuration if
242242- something goes wrong.</para>
243243-244244- <para>You should log in and change the <literal>root</literal>
245245- password with <command>passwd</command>.</para>
246246-247247- <para>You’ll probably want to create some user accounts as well,
248248- which can be done with <command>useradd</command>:
249249-250250-<screen>
251251-$ useradd -c 'Eelco Dolstra' -m eelco
252252-$ passwd eelco</screen>
253253-254254- </para>
255255-256256- <para>You may also want to install some software. For instance,
257257-258258-<screen>
259259-$ nix-env -qa \*</screen>
260260-261261- shows what packages are available, and
262262-263263-<screen>
264264-$ nix-env -i w3m</screen>
265265-266266- install the <literal>w3m</literal> browser.</para>
267267-268268- </listitem>
269269-270270-</orderedlist>
271271-272272-<para>To summarise, <xref linkend="ex-install-sequence" /> shows a
273273-typical sequence of commands for installing NixOS on an empty hard
274274-drive (here <filename>/dev/sda</filename>). <xref linkend="ex-config"
275275-/> shows a corresponding configuration Nix expression.</para>
276276-277277-<example xml:id='ex-install-sequence'><title>Commands for installing NixOS on <filename>/dev/sda</filename></title>
278278-<screen>
279279-$ fdisk /dev/sda # <lineannotation>(or whatever device you want to install on)</lineannotation>
280280-$ mkfs.ext4 -L nixos /dev/sda1
281281-$ mkswap -L swap /dev/sda2
282282-$ swapon /dev/sda2
283283-$ mount /dev/disk/by-label/nixos /mnt
284284-$ nixos-generate-config --root /mnt
285285-$ nano /mnt/etc/nixos/configuration.nix
286286-$ nixos-install
287287-$ reboot</screen>
288288-</example>
289289-290290-<example xml:id='ex-config'><title>NixOS configuration</title>
291291-<screen>
292292-{ config, pkgs, ... }:
293293-294294-{
295295- imports =
296296- [ # Include the results of the hardware scan.
297297- ./hardware-configuration.nix
298298- ];
299299-300300- boot.loader.grub.device = "/dev/sda";
301301-302302- # Note: setting fileSystems is generally not
303303- # necessary, since nixos-generate-config figures them out
304304- # automatically in hardware-configuration.nix.
305305- #fileSystems."/".device = "/dev/disk/by-label/nixos";
306306-307307- # Enable the OpenSSH server.
308308- services.sshd.enable = true;
309309-}</screen>
310310-</example>
311311-312312-<section xml:id="sec-uefi-installation">
313313-314314-<title>UEFI Installation</title>
315315-316316-<para>NixOS can also be installed on UEFI systems. The procedure
317317-is by and large the same as a BIOS installation, with the following
318318-changes:
319319-320320-<itemizedlist>
321321- <listitem>
322322- <para>You should boot the live CD in UEFI mode (consult your
323323- specific hardware's documentation for instructions). You may find
324324- the <link
325325- xlink:href="http://www.rodsbooks.com/refind">rEFInd
326326- boot manager</link> useful.</para>
327327- </listitem>
328328- <listitem>
329329- <para>Instead of <command>fdisk</command>, you should use
330330- <command>gdisk</command> to partition your disks. You will need to
331331- have a separate partition for <filename>/boot</filename> with
332332- partition code EF00, and it should be formatted as a
333333- <literal>vfat</literal> filesystem.</para>
334334- </listitem>
335335- <listitem>
336336- <para>You must set <option>boot.loader.gummiboot.enable</option> to
337337- <literal>true</literal>. <command>nixos-generate-config</command>
338338- should do this automatically for new configurations when booted in
339339- UEFI mode.</para>
340340- </listitem>
341341- <listitem>
342342- <para>After having mounted your installation partition to
343343- <code>/mnt</code>, you must mount the <code>boot</code> partition
344344- to <code>/mnt/boot</code>.</para>
345345- </listitem>
346346- <listitem>
347347- <para>You may want to look at the options starting with
348348- <option>boot.loader.efi</option> and <option>boot.loader.gummiboot</option>
349349- as well.</para>
350350- </listitem>
351351- <listitem>
352352- <para>To see console messages during early boot, add <literal>"fbcon"</literal>
353353- to your <option>boot.initrd.kernelModules</option>.</para>
354354- </listitem>
355355-</itemizedlist>
356356-</para>
357357-358358-</section>
359359-360360-<section>
361361-362362-<title xml:id="sec-booting-from-usb">Booting from a USB stick</title>
363363-364364-<para>For systems without CD drive, the NixOS livecd can be booted from
365365-a usb stick. For non-UEFI installations,
366366-<link xlink:href="http://unetbootin.sourceforge.net/">unetbootin</link>
367367-will work. For UEFI installations, you should mount the ISO, copy its contents
368368-verbatim to your drive, then either:
369369-370370-<itemizedlist>
371371- <listitem>
372372- <para>Change the label of the disk partition to the label of the ISO
373373- (visible with the blkid command), or</para>
374374- </listitem>
375375- <listitem>
376376- <para>Edit <filename>loader/entries/nixos-livecd.conf</filename> on the drive
377377- and change the <literal>root=</literal> field in the <literal>options</literal>
378378- line to point to your drive (see the documentation on <literal>root=</literal>
379379- in <link xlink:href="https://www.kernel.org/doc/Documentation/kernel-parameters.txt">
380380- the kernel documentation</link> for more details).</para>
381381- </listitem>
382382-</itemizedlist>
383383-</para>
384384-</section>
385385-386386-</section>
387387-388388-389389-<!--===============================================================-->
390390-391391-<section xml:id="sec-changing-config">
392392-393393-<title>Changing the configuration</title>
394394-395395-<para>The file <filename>/etc/nixos/configuration.nix</filename>
396396-contains the current configuration of your machine. Whenever you’ve
397397-changed something to that file, you should do
398398-399399-<screen>
400400-$ nixos-rebuild switch</screen>
401401-402402-to build the new configuration, make it the default configuration for
403403-booting, and try to realise the configuration in the running system
404404-(e.g., by restarting system services).</para>
405405-406406-<warning><para>These commands must be executed as root, so you should
407407-either run them from a root shell or by prefixing them with
408408-<literal>sudo -i</literal>.</para></warning>
409409-410410-<para>You can also do
411411-412412-<screen>
413413-$ nixos-rebuild test</screen>
414414-415415-to build the configuration and switch the running system to it, but
416416-without making it the boot default. So if (say) the configuration
417417-locks up your machine, you can just reboot to get back to a working
418418-configuration.</para>
419419-420420-<para>There is also
421421-422422-<screen>
423423-$ nixos-rebuild boot</screen>
424424-425425-to build the configuration and make it the boot default, but not
426426-switch to it now (so it will only take effect after the next
427427-reboot).</para>
428428-429429-<para>You can make your configuration show up in a different submenu
430430-of the GRUB 2 boot screen by giving it a different <emphasis>profile
431431-name</emphasis>, e.g.
432432-433433-<screen>
434434-$ nixos-rebuild switch -p test </screen>
435435-436436-which causes the new configuration (and previous ones created using
437437-<literal>-p test</literal>) to show up in the GRUB submenu “NixOS -
438438-Profile 'test'”. This can be useful to separate test configurations
439439-from “stable” configurations.</para>
440440-441441-<para>Finally, you can do
442442-443443-<screen>
444444-$ nixos-rebuild build</screen>
445445-446446-to build the configuration but nothing more. This is useful to see
447447-whether everything compiles cleanly.</para>
448448-449449-<para>If you have a machine that supports hardware virtualisation, you
450450-can also test the new configuration in a sandbox by building and
451451-running a QEMU <emphasis>virtual machine</emphasis> that contains the
452452-desired configuration. Just do
453453-454454-<screen>
455455-$ nixos-rebuild build-vm
456456-$ ./result/bin/run-*-vm
457457-</screen>
458458-459459-The VM does not have any data from your host system, so your existing
460460-user accounts and home directories will not be available. You can
461461-forward ports on the host to the guest. For instance, the following
462462-will forward host port 2222 to guest port 22 (SSH):
463463-464464-<screen>
465465-$ QEMU_NET_OPTS="hostfwd=tcp::2222-:22" ./result/bin/run-*-vm
466466-</screen>
467467-468468-allowing you to log in via SSH (assuming you have set the appropriate
469469-passwords or SSH authorized keys):
470470-471471-<screen>
472472-$ ssh -p 2222 localhost
473473-</screen>
474474-475475-</para>
476476-477477-</section>
478478-479479-480480-<!--===============================================================-->
481481-482482-<section xml:id="sec-upgrading">
483483-484484-<title>Upgrading NixOS</title>
485485-486486-<para>The best way to keep your NixOS installation up to date is to
487487-use one of the NixOS <emphasis>channels</emphasis>. A channel is a
488488-Nix mechanism for distributing Nix expressions and associated
489489-binaries. The NixOS channels are updated automatically from NixOS’s
490490-Git repository after certain tests have passed and all packages have
491491-been built. These channels are:
492492-493493-<itemizedlist>
494494- <listitem>
495495- <para>Stable channels, such as <literal
496496- xlink:href="http://nixos.org/channels/nixos-14.04">nixos-14.04</literal>.
497497- These only get conservative bug fixes and package upgrades. For
498498- instance, a channel update may cause the Linux kernel on your
499499- system to be upgraded from 3.4.66 to 3.4.67 (a minor bug fix), but
500500- not from 3.4.<replaceable>x</replaceable> to
501501- 3.11.<replaceable>x</replaceable> (a major change that has the
502502- potential to break things). Stable channels are generally
503503- maintained until the next stable branch is created.</para>
504504- </listitem>
505505- <listitem>
506506- <para>The unstable channel, <literal
507507- xlink:href="http://nixos.org/channels/nixos-unstable">nixos-unstable</literal>.
508508- This corresponds to NixOS’s main development branch, and may thus
509509- see radical changes between channel updates. It’s not recommended
510510- for production systems.</para>
511511- </listitem>
512512-</itemizedlist>
513513-514514-To see what channels are available, go to <link
515515-xlink:href="http://nixos.org/channels"/>. (Note that the URIs of the
516516-various channels redirect to a directory that contains the channel’s
517517-latest version and includes ISO images and VirtualBox
518518-appliances.)</para>
519519-520520-<para>When you first install NixOS, you’re automatically subscribed to
521521-the NixOS channel that corresponds to your installation source. For
522522-instance, if you installed from a 14.04 ISO, you will be subscribed to
523523-the <literal>nixos-14.04</literal> channel. To see which NixOS
524524-channel you’re subscribed to, run the following as root:
525525-526526-<screen>
527527-$ nix-channel --list | grep nixos
528528-nixos https://nixos.org/channels/nixos-unstable
529529-</screen>
530530-531531-To switch to a different NixOS channel, do
532532-533533-<screen>
534534-$ nix-channel --add http://nixos.org/channels/<replaceable>channel-name</replaceable> nixos
535535-</screen>
536536-537537-(Be sure to include the <literal>nixos</literal> parameter at the
538538-end.) For instance, to use the NixOS 14.04 stable channel:
539539-540540-<screen>
541541-$ nix-channel --add http://nixos.org/channels/nixos-14.04 nixos
542542-</screen>
543543-544544-But it you want to live on the bleeding edge:
545545-546546-<screen>
547547-$ nix-channel --add http://nixos.org/channels/nixos-unstable nixos
548548-</screen>
549549-550550-</para>
551551-552552-<para>You can then upgrade NixOS to the latest version in your chosen
553553-channel by running
554554-555555-<screen>
556556-$ nixos-rebuild switch --upgrade
557557-</screen>
558558-559559-which is equivalent to the more verbose <literal>nix-channel --update
560560-nixos; nixos-rebuild switch</literal>.</para>
561561-562562-<warning><para>It is generally safe to switch back and forth between
563563-channels. The only exception is that a newer NixOS may also have a
564564-newer Nix version, which may involve an upgrade of Nix’s database
565565-schema. This cannot be undone easily, so in that case you will not be
566566-able to go back to your original channel.</para></warning>
567567-568568-</section>
569569-570570-</chapter>
+90
nixos/doc/manual/installation/changing-config.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ version="5.0"
44+ xml:id="sec-changing-config">
55+66+<title>Changing the Configuration</title>
77+88+<para>The file <filename>/etc/nixos/configuration.nix</filename>
99+contains the current configuration of your machine. Whenever you’ve
1010+changed something to that file, you should do
1111+1212+<screen>
1313+$ nixos-rebuild switch</screen>
1414+1515+to build the new configuration, make it the default configuration for
1616+booting, and try to realise the configuration in the running system
1717+(e.g., by restarting system services).</para>
1818+1919+<warning><para>These commands must be executed as root, so you should
2020+either run them from a root shell or by prefixing them with
2121+<literal>sudo -i</literal>.</para></warning>
2222+2323+<para>You can also do
2424+2525+<screen>
2626+$ nixos-rebuild test</screen>
2727+2828+to build the configuration and switch the running system to it, but
2929+without making it the boot default. So if (say) the configuration
3030+locks up your machine, you can just reboot to get back to a working
3131+configuration.</para>
3232+3333+<para>There is also
3434+3535+<screen>
3636+$ nixos-rebuild boot</screen>
3737+3838+to build the configuration and make it the boot default, but not
3939+switch to it now (so it will only take effect after the next
4040+reboot).</para>
4141+4242+<para>You can make your configuration show up in a different submenu
4343+of the GRUB 2 boot screen by giving it a different <emphasis>profile
4444+name</emphasis>, e.g.
4545+4646+<screen>
4747+$ nixos-rebuild switch -p test </screen>
4848+4949+which causes the new configuration (and previous ones created using
5050+<literal>-p test</literal>) to show up in the GRUB submenu “NixOS -
5151+Profile 'test'”. This can be useful to separate test configurations
5252+from “stable” configurations.</para>
5353+5454+<para>Finally, you can do
5555+5656+<screen>
5757+$ nixos-rebuild build</screen>
5858+5959+to build the configuration but nothing more. This is useful to see
6060+whether everything compiles cleanly.</para>
6161+6262+<para>If you have a machine that supports hardware virtualisation, you
6363+can also test the new configuration in a sandbox by building and
6464+running a QEMU <emphasis>virtual machine</emphasis> that contains the
6565+desired configuration. Just do
6666+6767+<screen>
6868+$ nixos-rebuild build-vm
6969+$ ./result/bin/run-*-vm
7070+</screen>
7171+7272+The VM does not have any data from your host system, so your existing
7373+user accounts and home directories will not be available. You can
7474+forward ports on the host to the guest. For instance, the following
7575+will forward host port 2222 to guest port 22 (SSH):
7676+7777+<screen>
7878+$ QEMU_NET_OPTS="hostfwd=tcp::2222-:22" ./result/bin/run-*-vm
7979+</screen>
8080+8181+allowing you to log in via SSH (assuming you have set the appropriate
8282+passwords or SSH authorized keys):
8383+8484+<screen>
8585+$ ssh -p 2222 localhost
8686+</screen>
8787+8888+</para>
8989+9090+</chapter>
+21
nixos/doc/manual/installation/installation.xml
···11+<part xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-installation">
66+77+<title>Installation</title>
88+99+<partintro>
1010+1111+<para>This section describes how to obtain, install, and configure
1212+NixOS for first-time use.</para>
1313+1414+</partintro>
1515+1616+<xi:include href="obtaining.xml" />
1717+<xi:include href="installing.xml" />
1818+<xi:include href="changing-config.xml" />
1919+<xi:include href="upgrading.xml" />
2020+2121+</part>
+51
nixos/doc/manual/installation/installing-UEFI.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-uefi-installation">
66+77+<title>UEFI Installation</title>
88+99+<para>NixOS can also be installed on UEFI systems. The procedure
1010+is by and large the same as a BIOS installation, with the following
1111+changes:
1212+1313+<itemizedlist>
1414+ <listitem>
1515+ <para>You should boot the live CD in UEFI mode (consult your
1616+ specific hardware's documentation for instructions). You may find
1717+ the <link
1818+ xlink:href="http://www.rodsbooks.com/refind">rEFInd
1919+ boot manager</link> useful.</para>
2020+ </listitem>
2121+ <listitem>
2222+ <para>Instead of <command>fdisk</command>, you should use
2323+ <command>gdisk</command> to partition your disks. You will need to
2424+ have a separate partition for <filename>/boot</filename> with
2525+ partition code EF00, and it should be formatted as a
2626+ <literal>vfat</literal> filesystem.</para>
2727+ </listitem>
2828+ <listitem>
2929+ <para>You must set <option>boot.loader.gummiboot.enable</option> to
3030+ <literal>true</literal>. <command>nixos-generate-config</command>
3131+ should do this automatically for new configurations when booted in
3232+ UEFI mode.</para>
3333+ </listitem>
3434+ <listitem>
3535+ <para>After having mounted your installation partition to
3636+ <code>/mnt</code>, you must mount the <code>boot</code> partition
3737+ to <code>/mnt/boot</code>.</para>
3838+ </listitem>
3939+ <listitem>
4040+ <para>You may want to look at the options starting with
4141+ <option>boot.loader.efi</option> and <option>boot.loader.gummiboot</option>
4242+ as well.</para>
4343+ </listitem>
4444+ <listitem>
4545+ <para>To see console messages during early boot, add <literal>"fbcon"</literal>
4646+ to your <option>boot.initrd.kernelModules</option>.</para>
4747+ </listitem>
4848+</itemizedlist>
4949+</para>
5050+5151+</section>
+30
nixos/doc/manual/installation/installing-USB.xml
···11+<section xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-booting-from-usb">
66+77+<title>Booting from a USB Drive</title>
88+99+<para>For systems without CD drive, the NixOS livecd can be booted from
1010+a usb stick. For non-UEFI installations,
1111+<link xlink:href="http://unetbootin.sourceforge.net/">unetbootin</link>
1212+will work. For UEFI installations, you should mount the ISO, copy its contents
1313+verbatim to your drive, then either:
1414+1515+<itemizedlist>
1616+ <listitem>
1717+ <para>Change the label of the disk partition to the label of the ISO
1818+ (visible with the blkid command), or</para>
1919+ </listitem>
2020+ <listitem>
2121+ <para>Edit <filename>loader/entries/nixos-livecd.conf</filename> on the drive
2222+ and change the <literal>root=</literal> field in the <literal>options</literal>
2323+ line to point to your drive (see the documentation on <literal>root=</literal>
2424+ in <link xlink:href="https://www.kernel.org/doc/Documentation/kernel-parameters.txt">
2525+ the kernel documentation</link> for more details).</para>
2626+ </listitem>
2727+</itemizedlist>
2828+</para>
2929+3030+</section>
+264
nixos/doc/manual/installation/installing.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-installation">
66+77+<title>Installing NixOS</title>
88+99+<orderedlist>
1010+1111+ <listitem><para>Boot from the CD.</para></listitem>
1212+1313+ <listitem><para>The CD contains a basic NixOS installation. (It
1414+ also contains Memtest86+, useful if you want to test new hardware.)
1515+ When it’s finished booting, it should have detected most of your
1616+ hardware and brought up networking (check
1717+ <command>ifconfig</command>). Networking is necessary for the
1818+ installer, since it will download lots of stuff (such as source
1919+ tarballs or Nixpkgs channel binaries). It’s best if you have a DHCP
2020+ server on your network. Otherwise configure networking manually
2121+ using <command>ifconfig</command>.</para></listitem>
2222+2323+ <listitem><para>The NixOS manual is available on virtual console 8
2424+ (press Alt+F8 to access).</para></listitem>
2525+2626+ <listitem><para>Login as <literal>root</literal> and the empty
2727+ password.</para></listitem>
2828+2929+ <listitem><para>If you downloaded the graphical ISO image, you can
3030+ run <command>start display-manager</command> to start KDE.</para></listitem>
3131+3232+ <listitem><para>The NixOS installer doesn’t do any partitioning or
3333+ formatting yet, so you need to that yourself. Use the following
3434+ commands:
3535+3636+ <itemizedlist>
3737+3838+ <listitem><para>For partitioning:
3939+ <command>fdisk</command>.</para></listitem>
4040+4141+ <listitem><para>For initialising Ext4 partitions:
4242+ <command>mkfs.ext4</command>. It is recommended that you assign a
4343+ unique symbolic label to the file system using the option
4444+ <option>-L <replaceable>label</replaceable></option>, since this
4545+ makes the file system configuration independent from device
4646+ changes. For example:
4747+4848+<screen>
4949+$ mkfs.ext4 -L nixos /dev/sda1</screen>
5050+5151+ </para></listitem>
5252+5353+ <listitem><para>For creating swap partitions:
5454+ <command>mkswap</command>. Again it’s recommended to assign a
5555+ label to the swap partition: <option>-L
5656+ <replaceable>label</replaceable></option>.</para></listitem>
5757+5858+ <listitem><para>For creating LVM volumes, the LVM commands, e.g.,
5959+6060+<screen>
6161+$ pvcreate /dev/sda1 /dev/sdb1
6262+$ vgcreate MyVolGroup /dev/sda1 /dev/sdb1
6363+$ lvcreate --size 2G --name bigdisk MyVolGroup
6464+$ lvcreate --size 1G --name smalldisk MyVolGroup</screen>
6565+6666+ </para></listitem>
6767+6868+ <listitem><para>For creating software RAID devices, use
6969+ <command>mdadm</command>.</para></listitem>
7070+7171+ </itemizedlist>
7272+7373+ </para></listitem>
7474+7575+ <listitem><para>Mount the target file system on which NixOS should
7676+ be installed on <filename>/mnt</filename>, e.g.
7777+7878+<screen>
7979+$ mount /dev/disk/by-label/nixos /mnt
8080+</screen>
8181+8282+ </para></listitem>
8383+8484+ <listitem><para>If your machine has a limited amount of memory, you
8585+ may want to activate swap devices now (<command>swapon
8686+ <replaceable>device</replaceable></command>). The installer (or
8787+ rather, the build actions that it may spawn) may need quite a bit of
8888+ RAM, depending on your configuration.</para></listitem>
8989+9090+ <listitem>
9191+9292+ <para>You now need to create a file
9393+ <filename>/mnt/etc/nixos/configuration.nix</filename> that
9494+ specifies the intended configuration of the system. This is
9595+ because NixOS has a <emphasis>declarative</emphasis> configuration
9696+ model: you create or edit a description of the desired
9797+ configuration of your system, and then NixOS takes care of making
9898+ it happen. The syntax of the NixOS configuration file is
9999+ described in <xref linkend="sec-configuration-syntax"/>, while a
100100+ list of available configuration options appears in <xref
101101+ linkend="ch-options"/>. A minimal example is shown in <xref
102102+ linkend="ex-config"/>.</para>
103103+104104+ <para>The command <command>nixos-generate-config</command> can
105105+ generate an initial configuration file for you:
106106+107107+<screen>
108108+$ nixos-generate-config --root /mnt</screen>
109109+110110+ You should then edit
111111+ <filename>/mnt/etc/nixos/configuration.nix</filename> to suit your
112112+ needs:
113113+114114+<screen>
115115+$ nano /mnt/etc/nixos/configuration.nix
116116+</screen>
117117+118118+ The <command>vim</command> text editor is also available.</para>
119119+120120+ <para>You <emphasis>must</emphasis> set the option
121121+ <option>boot.loader.grub.device</option> to specify on which disk
122122+ the GRUB boot loader is to be installed. Without it, NixOS cannot
123123+ boot.</para>
124124+125125+ <para>Another critical option is <option>fileSystems</option>,
126126+ specifying the file systems that need to be mounted by NixOS.
127127+ However, you typically don’t need to set it yourself, because
128128+ <command>nixos-generate-config</command> sets it automatically in
129129+ <filename>/mnt/etc/nixos/hardware-configuration.nix</filename>
130130+ from your currently mounted file systems. (The configuration file
131131+ <filename>hardware-configuration.nix</filename> is included from
132132+ <filename>configuration.nix</filename> and will be overwritten by
133133+ future invocations of <command>nixos-generate-config</command>;
134134+ thus, you generally should not modify it.)</para>
135135+136136+ <note><para>Depending on your hardware configuration or type of
137137+ file system, you may need to set the option
138138+ <option>boot.initrd.kernelModules</option> to include the kernel
139139+ modules that are necessary for mounting the root file system,
140140+ otherwise the installed system will not be able to boot. (If this
141141+ happens, boot from the CD again, mount the target file system on
142142+ <filename>/mnt</filename>, fix
143143+ <filename>/mnt/etc/nixos/configuration.nix</filename> and rerun
144144+ <filename>nixos-install</filename>.) In most cases,
145145+ <command>nixos-generate-config</command> will figure out the
146146+ required modules.</para></note>
147147+148148+ <para>Examples of real-world NixOS configuration files can be
149149+ found at <link
150150+ xlink:href="https://nixos.org/repos/nix/configurations/trunk/"/>.</para>
151151+152152+ </listitem>
153153+154154+ <listitem><para>Do the installation:
155155+156156+<screen>
157157+$ nixos-install</screen>
158158+159159+ Cross fingers. If this fails due to a temporary problem (such as
160160+ a network issue while downloading binaries from the NixOS binary
161161+ cache), you can just re-run <command>nixos-install</command>.
162162+ Otherwise, fix your <filename>configuration.nix</filename> and
163163+ then re-run <command>nixos-install</command>.</para>
164164+165165+ <para>As the last step, <command>nixos-install</command> will ask
166166+ you to set the password for the <literal>root</literal> user, e.g.
167167+168168+<screen>
169169+setting root password...
170170+Enter new UNIX password: ***
171171+Retype new UNIX password: ***
172172+</screen>
173173+174174+ </para>
175175+176176+ </listitem>
177177+178178+ <listitem><para>If everything went well:
179179+180180+<screen>
181181+$ reboot</screen>
182182+183183+ </para></listitem>
184184+185185+ <listitem>
186186+187187+ <para>You should now be able to boot into the installed NixOS. The GRUB boot menu shows a list
188188+ of <emphasis>available configurations</emphasis> (initially just one). Every time
189189+ you change the NixOS configuration (see<link linkend="sec-changing-config">Changing
190190+ Configuration</link> ), a new item appears in the menu. This allows you to
191191+ easily roll back to another configuration if something goes wrong.</para>
192192+193193+ <para>You should log in and change the <literal>root</literal>
194194+ password with <command>passwd</command>.</para>
195195+196196+ <para>You’ll probably want to create some user accounts as well,
197197+ which can be done with <command>useradd</command>:
198198+199199+<screen>
200200+$ useradd -c 'Eelco Dolstra' -m eelco
201201+$ passwd eelco</screen>
202202+203203+ </para>
204204+205205+ <para>You may also want to install some software. For instance,
206206+207207+<screen>
208208+$ nix-env -qa \*</screen>
209209+210210+ shows what packages are available, and
211211+212212+<screen>
213213+$ nix-env -i w3m</screen>
214214+215215+ install the <literal>w3m</literal> browser.</para>
216216+217217+ </listitem>
218218+219219+</orderedlist>
220220+221221+<para>To summarise, <xref linkend="ex-install-sequence" /> shows a
222222+typical sequence of commands for installing NixOS on an empty hard
223223+drive (here <filename>/dev/sda</filename>). <xref linkend="ex-config"
224224+/> shows a corresponding configuration Nix expression.</para>
225225+226226+<example xml:id='ex-install-sequence'><title>Commands for Installing NixOS on <filename>/dev/sda</filename></title>
227227+<screen>
228228+$ fdisk /dev/sda # <lineannotation>(or whatever device you want to install on)</lineannotation>
229229+$ mkfs.ext4 -L nixos /dev/sda1
230230+$ mkswap -L swap /dev/sda2
231231+$ swapon /dev/sda2
232232+$ mount /dev/disk/by-label/nixos /mnt
233233+$ nixos-generate-config --root /mnt
234234+$ nano /mnt/etc/nixos/configuration.nix
235235+$ nixos-install
236236+$ reboot</screen>
237237+</example>
238238+239239+<example xml:id='ex-config'><title>NixOS Configuration</title>
240240+<screen>
241241+{ config, pkgs, ... }:
242242+243243+{
244244+ imports =
245245+ [ # Include the results of the hardware scan.
246246+ ./hardware-configuration.nix
247247+ ];
248248+249249+ boot.loader.grub.device = "/dev/sda";
250250+251251+ # Note: setting fileSystems is generally not
252252+ # necessary, since nixos-generate-config figures them out
253253+ # automatically in hardware-configuration.nix.
254254+ #fileSystems."/".device = "/dev/disk/by-label/nixos";
255255+256256+ # Enable the OpenSSH server.
257257+ services.sshd.enable = true;
258258+}</screen>
259259+</example>
260260+261261+<xi:include href="installing-UEFI.xml" />
262262+<xi:include href="installing-USB.xml" />
263263+264264+</chapter>
+44
nixos/doc/manual/installation/obtaining.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-obtaining">
66+77+<title>Obtaining NixOS</title>
88+99+<para>NixOS ISO images can be downloaded from the <link
1010+xlink:href="http://nixos.org/nixos/download.html">NixOS
1111+homepage</link>. These can be burned onto a CD. It is also possible
1212+to copy them onto a USB stick and install NixOS from there. For
1313+details, see the <link
1414+xlink:href="https://nixos.org/wiki/Installing_NixOS_from_a_USB_stick">NixOS
1515+Wiki</link>.</para>
1616+1717+<para>As an alternative to installing NixOS yourself, you can get a
1818+running NixOS system through several other means:
1919+2020+<itemizedlist>
2121+ <listitem>
2222+ <para>Using virtual appliances in Open Virtualization Format (OVF)
2323+ that can be imported into VirtualBox. These are available from
2424+ the <link xlink:href="http://nixos.org/nixos/download.html">NixOS
2525+ homepage</link>.</para>
2626+ </listitem>
2727+ <listitem>
2828+ <para>Using AMIs for Amazon’s EC2. To find one for your region
2929+ and instance type, please refer to the <link
3030+ xlink:href="https://github.com/NixOS/nixops/blob/master/nix/ec2-amis.nix">list
3131+ of most recent AMIs</link>.</para>
3232+ </listitem>
3333+ <listitem>
3434+ <para>Using NixOps, the NixOS-based cloud deployment tool, which
3535+ allows you to provision VirtualBox and EC2 NixOS instances from
3636+ declarative specifications. Check out the <link
3737+ xlink:href="https://github.com/NixOS/nixops">NixOps
3838+ homepage</link> for details.</para>
3939+ </listitem>
4040+</itemizedlist>
4141+4242+</para>
4343+4444+</chapter>
+90
nixos/doc/manual/installation/upgrading.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ version="5.0"
44+ xml:id="sec-upgrading">
55+66+<title>Upgrading NixOS</title>
77+88+<para>The best way to keep your NixOS installation up to date is to
99+use one of the NixOS <emphasis>channels</emphasis>. A channel is a
1010+Nix mechanism for distributing Nix expressions and associated
1111+binaries. The NixOS channels are updated automatically from NixOS’s
1212+Git repository after certain tests have passed and all packages have
1313+been built. These channels are:
1414+1515+<itemizedlist>
1616+ <listitem>
1717+ <para>Stable channels, such as <literal
1818+ xlink:href="http://nixos.org/channels/nixos-14.04">nixos-14.04</literal>.
1919+ These only get conservative bug fixes and package upgrades. For
2020+ instance, a channel update may cause the Linux kernel on your
2121+ system to be upgraded from 3.4.66 to 3.4.67 (a minor bug fix), but
2222+ not from 3.4.<replaceable>x</replaceable> to
2323+ 3.11.<replaceable>x</replaceable> (a major change that has the
2424+ potential to break things). Stable channels are generally
2525+ maintained until the next stable branch is created.</para>
2626+ </listitem>
2727+ <listitem>
2828+ <para>The unstable channel, <literal
2929+ xlink:href="http://nixos.org/channels/nixos-unstable">nixos-unstable</literal>.
3030+ This corresponds to NixOS’s main development branch, and may thus
3131+ see radical changes between channel updates. It’s not recommended
3232+ for production systems.</para>
3333+ </listitem>
3434+</itemizedlist>
3535+3636+To see what channels are available, go to <link
3737+xlink:href="http://nixos.org/channels"/>. (Note that the URIs of the
3838+various channels redirect to a directory that contains the channel’s
3939+latest version and includes ISO images and VirtualBox
4040+appliances.)</para>
4141+4242+<para>When you first install NixOS, you’re automatically subscribed to
4343+the NixOS channel that corresponds to your installation source. For
4444+instance, if you installed from a 14.04 ISO, you will be subscribed to
4545+the <literal>nixos-14.04</literal> channel. To see which NixOS
4646+channel you’re subscribed to, run the following as root:
4747+4848+<screen>
4949+$ nix-channel --list | grep nixos
5050+nixos https://nixos.org/channels/nixos-unstable
5151+</screen>
5252+5353+To switch to a different NixOS channel, do
5454+5555+<screen>
5656+$ nix-channel --add http://nixos.org/channels/<replaceable>channel-name</replaceable> nixos
5757+</screen>
5858+5959+(Be sure to include the <literal>nixos</literal> parameter at the
6060+end.) For instance, to use the NixOS 14.04 stable channel:
6161+6262+<screen>
6363+$ nix-channel --add http://nixos.org/channels/nixos-14.04 nixos
6464+</screen>
6565+6666+But it you want to live on the bleeding edge:
6767+6868+<screen>
6969+$ nix-channel --add http://nixos.org/channels/nixos-unstable nixos
7070+</screen>
7171+7272+</para>
7373+7474+<para>You can then upgrade NixOS to the latest version in your chosen
7575+channel by running
7676+7777+<screen>
7878+$ nixos-rebuild switch --upgrade
7979+</screen>
8080+8181+which is equivalent to the more verbose <literal>nix-channel --update
8282+nixos; nixos-rebuild switch</literal>.</para>
8383+8484+<warning><para>It is generally safe to switch back and forth between
8585+channels. The only exception is that a newer NixOS may also have a
8686+newer Nix version, which may involve an upgrade of Nix’s database
8787+schema. This cannot be undone easily, so in that case you will not be
8888+able to go back to your original channel.</para></warning>
8989+9090+</chapter>
···11-<appendix xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-release-notes">
44-55-<title>Release notes</title>
66-77-<!--==================================================================-->
88-99-<section xml:id="sec-release-14.10">
1010-1111-<title>Release 14.10 (“Caterpillar”, 2014/10/??)</title>
1212-1313-<para>When upgrading from a previous release, please be aware of the
1414-following incompatible changes:
1515-1616-<itemizedlist>
1717-1818- <listitem><para>The host side of a container virtual Ethernet pair
1919- is now called <literal>ve-<replaceable>container-name</replaceable></literal>
2020- rather than <literal>c-<replaceable>container-name</replaceable></literal>.</para></listitem>
2121-2222-</itemizedlist>
2323-2424-</para>
2525-2626-</section>
2727-2828-2929-<!--==================================================================-->
3030-3131-<section xml:id="sec-release-14.04">
11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-release-14.04">
326337<title>Release 14.04 (“Baboon”, 2014/04/30)</title>
348···183157184158</para>
185159186186-</section>
187187-188188-<!--==================================================================-->
189189-190190-<section xml:id="sec-release-13.10">
191191-192192-<title>Release 13.10 (“Aardvark”, 2013/10/31)</title>
193193-194194-<para>This is the first stable release branch of NixOS.</para>
195195-196196-</section>
197197-198198-</appendix>
160160+</chapter>
+17
nixos/doc/manual/release-notes/release-notes.xml
···11+<part xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="ch-release-notes">
66+77+<title>Release Notes</title>
88+99+<partintro>
1010+<para>This section lists the release notes for each stable version of NixOS.</para>
1111+</partintro>
1212+1313+<xi:include href="rl-1410.xml" />
1414+<xi:include href="rl-1404.xml" />
1515+<xi:include href="rl-1310.xml" />
1616+1717+</part>
+11
nixos/doc/manual/release-notes/rl-1310.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-release-13.10">
66+77+<title>Release 13.10 (“Aardvark”, 2013/10/31)</title>
88+99+<para>This is the first stable release branch of NixOS.</para>
1010+1111+</chapter>
+22
nixos/doc/manual/release-notes/rl-1410.xml
···11+<chapter xmlns="http://docbook.org/ns/docbook"
22+ xmlns:xlink="http://www.w3.org/1999/xlink"
33+ xmlns:xi="http://www.w3.org/2001/XInclude"
44+ version="5.0"
55+ xml:id="sec-release-14.10">
66+77+<title>Release 14.10 (“Caterpillar”, 2014/10/??)</title>
88+99+<para>When upgrading from a previous release, please be aware of the
1010+following incompatible changes:
1111+1212+<itemizedlist>
1313+1414+ <listitem><para>The host side of a container virtual Ethernet pair
1515+ is now called <literal>ve-<replaceable>container-name</replaceable></literal>
1616+ rather than <literal>c-<replaceable>container-name</replaceable></literal>.</para></listitem>
1717+1818+</itemizedlist>
1919+2020+</para>
2121+2222+</chapter>
-369
nixos/doc/manual/running.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-running">
44-55-<title>Running NixOS</title>
66-77-<para>This chapter describes various aspects of managing a running
88-NixOS system, such as how to use the <command>systemd</command>
99-service manager.</para>
1010-1111-1212-<!--===============================================================-->
1313-1414-<section xml:id="sec-systemctl"><title>Service management</title>
1515-1616-<para>In NixOS, all system services are started and monitored using
1717-the systemd program. Systemd is the “init” process of the system
1818-(i.e. PID 1), the parent of all other processes. It manages a set of
1919-so-called “units”, which can be things like system services
2020-(programs), but also mount points, swap files, devices, targets
2121-(groups of units) and more. Units can have complex dependencies; for
2222-instance, one unit can require that another unit must be successfully
2323-started before the first unit can be started. When the system boots,
2424-it starts a unit named <literal>default.target</literal>; the
2525-dependencies of this unit cause all system services to be started,
2626-file systems to be mounted, swap files to be activated, and so
2727-on.</para>
2828-2929-<para>The command <command>systemctl</command> is the main way to
3030-interact with <command>systemd</command>. Without any arguments, it
3131-shows the status of active units:
3232-3333-<screen>
3434-$ systemctl
3535--.mount loaded active mounted /
3636-swapfile.swap loaded active active /swapfile
3737-sshd.service loaded active running SSH Daemon
3838-graphical.target loaded active active Graphical Interface
3939-<replaceable>...</replaceable>
4040-</screen>
4141-4242-</para>
4343-4444-<para>You can ask for detailed status information about a unit, for
4545-instance, the PostgreSQL database service:
4646-4747-<screen>
4848-$ systemctl status postgresql.service
4949-postgresql.service - PostgreSQL Server
5050- Loaded: loaded (/nix/store/pn3q73mvh75gsrl8w7fdlfk3fq5qm5mw-unit/postgresql.service)
5151- Active: active (running) since Mon, 2013-01-07 15:55:57 CET; 9h ago
5252- Main PID: 2390 (postgres)
5353- CGroup: name=systemd:/system/postgresql.service
5454- ├─2390 postgres
5555- ├─2418 postgres: writer process
5656- ├─2419 postgres: wal writer process
5757- ├─2420 postgres: autovacuum launcher process
5858- ├─2421 postgres: stats collector process
5959- └─2498 postgres: zabbix zabbix [local] idle
6060-6161-Jan 07 15:55:55 hagbard postgres[2394]: [1-1] LOG: database system was shut down at 2013-01-07 15:55:05 CET
6262-Jan 07 15:55:57 hagbard postgres[2390]: [1-1] LOG: database system is ready to accept connections
6363-Jan 07 15:55:57 hagbard postgres[2420]: [1-1] LOG: autovacuum launcher started
6464-Jan 07 15:55:57 hagbard systemd[1]: Started PostgreSQL Server.
6565-</screen>
6666-6767-Note that this shows the status of the unit (active and running), all
6868-the processes belonging to the service, as well as the most recent log
6969-messages from the service.
7070-7171-</para>
7272-7373-<para>Units can be stopped, started or restarted:
7474-7575-<screen>
7676-$ systemctl stop postgresql.service
7777-$ systemctl start postgresql.service
7878-$ systemctl restart postgresql.service
7979-</screen>
8080-8181-These operations are synchronous: they wait until the service has
8282-finished starting or stopping (or has failed). Starting a unit will
8383-cause the dependencies of that unit to be started as well (if
8484-necessary).</para>
8585-8686-<!-- - cgroups: each service and user session is a cgroup
8787-8888-- cgroup resource management -->
8989-9090-</section>
9191-9292-9393-<!--===============================================================-->
9494-9595-<section xml:id="sec-rebooting"><title>Rebooting and shutting down</title>
9696-9797-<para>The system can be shut down (and automatically powered off) by
9898-doing:
9999-100100-<screen>
101101-$ shutdown
102102-</screen>
103103-104104-This is equivalent to running <command>systemctl
105105-poweroff</command>.</para>
106106-107107-<para>To reboot the system, run
108108-109109-<screen>
110110-$ reboot
111111-</screen>
112112-113113-which is equivalent to <command>systemctl reboot</command>.
114114-Alternatively, you can quickly reboot the system using
115115-<literal>kexec</literal>, which bypasses the BIOS by directly loading
116116-the new kernel into memory:
117117-118118-<screen>
119119-$ systemctl kexec
120120-</screen>
121121-122122-</para>
123123-124124-<para>The machine can be suspended to RAM (if supported) using
125125-<command>systemctl suspend</command>, and suspended to disk using
126126-<command>systemctl hibernate</command>.</para>
127127-128128-<para>These commands can be run by any user who is logged in locally,
129129-i.e. on a virtual console or in X11; otherwise, the user is asked for
130130-authentication.</para>
131131-132132-</section>
133133-134134-135135-<!--===============================================================-->
136136-137137-<section xml:id="sec-user-sessions"><title>User sessions</title>
138138-139139-<para>Systemd keeps track of all users who are logged into the system
140140-(e.g. on a virtual console or remotely via SSH). The command
141141-<command>loginctl</command> allows querying and manipulating user
142142-sessions. For instance, to list all user sessions:
143143-144144-<screen>
145145-$ loginctl
146146- SESSION UID USER SEAT
147147- c1 500 eelco seat0
148148- c3 0 root seat0
149149- c4 500 alice
150150-</screen>
151151-152152-This shows that two users are logged in locally, while another is
153153-logged in remotely. (“Seats” are essentially the combinations of
154154-displays and input devices attached to the system; usually, there is
155155-only one seat.) To get information about a session:
156156-157157-<screen>
158158-$ loginctl session-status c3
159159-c3 - root (0)
160160- Since: Tue, 2013-01-08 01:17:56 CET; 4min 42s ago
161161- Leader: 2536 (login)
162162- Seat: seat0; vc3
163163- TTY: /dev/tty3
164164- Service: login; type tty; class user
165165- State: online
166166- CGroup: name=systemd:/user/root/c3
167167- ├─ 2536 /nix/store/10mn4xip9n7y9bxqwnsx7xwx2v2g34xn-shadow-4.1.5.1/bin/login --
168168- ├─10339 -bash
169169- └─10355 w3m nixos.org
170170-</screen>
171171-172172-This shows that the user is logged in on virtual console 3. It also
173173-lists the processes belonging to this session. Since systemd keeps
174174-track of this, you can terminate a session in a way that ensures that
175175-all the session’s processes are gone:
176176-177177-<screen>
178178-$ loginctl terminate-session c3
179179-</screen>
180180-181181-</para>
182182-183183-</section>
184184-185185-186186-<!--===============================================================-->
187187-188188-<section xml:id="sec-cgroups"><title>Control groups</title>
189189-190190-<para>To keep track of the processes in a running system, systemd uses
191191-<emphasis>control groups</emphasis> (cgroups). A control group is a
192192-set of processes used to allocate resources such as CPU, memory or I/O
193193-bandwidth. There can be multiple control group hierarchies, allowing
194194-each kind of resource to be managed independently.</para>
195195-196196-<para>The command <command>systemd-cgls</command> lists all control
197197-groups in the <literal>systemd</literal> hierarchy, which is what
198198-systemd uses to keep track of the processes belonging to each service
199199-or user session:
200200-201201-<screen>
202202-$ systemd-cgls
203203-├─user
204204-│ └─eelco
205205-│ └─c1
206206-│ ├─ 2567 -:0
207207-│ ├─ 2682 kdeinit4: kdeinit4 Running...
208208-│ ├─ <replaceable>...</replaceable>
209209-│ └─10851 sh -c less -R
210210-└─system
211211- ├─httpd.service
212212- │ ├─2444 httpd -f /nix/store/3pyacby5cpr55a03qwbnndizpciwq161-httpd.conf -DNO_DETACH
213213- │ └─<replaceable>...</replaceable>
214214- ├─dhcpcd.service
215215- │ └─2376 dhcpcd --config /nix/store/f8dif8dsi2yaa70n03xir8r653776ka6-dhcpcd.conf
216216- └─ <replaceable>...</replaceable>
217217-</screen>
218218-219219-Similarly, <command>systemd-cgls cpu</command> shows the cgroups in
220220-the CPU hierarchy, which allows per-cgroup CPU scheduling priorities.
221221-By default, every systemd service gets its own CPU cgroup, while all
222222-user sessions are in the top-level CPU cgroup. This ensures, for
223223-instance, that a thousand run-away processes in the
224224-<literal>httpd.service</literal> cgroup cannot starve the CPU for one
225225-process in the <literal>postgresql.service</literal> cgroup. (By
226226-contrast, it they were in the same cgroup, then the PostgreSQL process
227227-would get 1/1001 of the cgroup’s CPU time.) You can limit a service’s
228228-CPU share in <filename>configuration.nix</filename>:
229229-230230-<programlisting>
231231-systemd.services.httpd.serviceConfig.CPUShares = 512;
232232-</programlisting>
233233-234234-By default, every cgroup has 1024 CPU shares, so this will halve the
235235-CPU allocation of the <literal>httpd.service</literal> cgroup.</para>
236236-237237-<para>There also is a <literal>memory</literal> hierarchy that
238238-controls memory allocation limits; by default, all processes are in
239239-the top-level cgroup, so any service or session can exhaust all
240240-available memory. Per-cgroup memory limits can be specified in
241241-<filename>configuration.nix</filename>; for instance, to limit
242242-<literal>httpd.service</literal> to 512 MiB of RAM (excluding swap)
243243-and 640 MiB of RAM (including swap):
244244-245245-<programlisting>
246246-systemd.services.httpd.serviceConfig.MemoryLimit = "512M";
247247-systemd.services.httpd.serviceConfig.ControlGroupAttribute = [ "memory.memsw.limit_in_bytes 640M" ];
248248-</programlisting>
249249-250250-</para>
251251-252252-<para>The command <command>systemd-cgtop</command> shows a
253253-continuously updated list of all cgroups with their CPU and memory
254254-usage.</para>
255255-256256-</section>
257257-258258-259259-<!--===============================================================-->
260260-261261-<section xml:id="sec-logging"><title>Logging</title>
262262-263263-<para>System-wide logging is provided by systemd’s
264264-<emphasis>journal</emphasis>, which subsumes traditional logging
265265-daemons such as syslogd and klogd. Log entries are kept in binary
266266-files in <filename>/var/log/journal/</filename>. The command
267267-<literal>journalctl</literal> allows you to see the contents of the
268268-journal. For example,
269269-270270-<screen>
271271-$ journalctl -b
272272-</screen>
273273-274274-shows all journal entries since the last reboot. (The output of
275275-<command>journalctl</command> is piped into <command>less</command> by
276276-default.) You can use various options and match operators to restrict
277277-output to messages of interest. For instance, to get all messages
278278-from PostgreSQL:
279279-280280-<screen>
281281-$ journalctl -u postgresql.service
282282--- Logs begin at Mon, 2013-01-07 13:28:01 CET, end at Tue, 2013-01-08 01:09:57 CET. --
283283-...
284284-Jan 07 15:44:14 hagbard postgres[2681]: [2-1] LOG: database system is shut down
285285--- Reboot --
286286-Jan 07 15:45:10 hagbard postgres[2532]: [1-1] LOG: database system was shut down at 2013-01-07 15:44:14 CET
287287-Jan 07 15:45:13 hagbard postgres[2500]: [1-1] LOG: database system is ready to accept connections
288288-</screen>
289289-290290-Or to get all messages since the last reboot that have at least a
291291-“critical” severity level:
292292-293293-<screen>
294294-$ journalctl -b -p crit
295295-Dec 17 21:08:06 mandark sudo[3673]: pam_unix(sudo:auth): auth could not identify password for [alice]
296296-Dec 29 01:30:22 mandark kernel[6131]: [1053513.909444] CPU6: Core temperature above threshold, cpu clock throttled (total events = 1)
297297-</screen>
298298-299299-</para>
300300-301301-<para>The system journal is readable by root and by users in the
302302-<literal>wheel</literal> and <literal>systemd-journal</literal>
303303-groups. All users have a private journal that can be read using
304304-<command>journalctl</command>.</para>
305305-306306-</section>
307307-308308-309309-<!--===============================================================-->
310310-311311-<section xml:id="sec-nix-gc"><title>Cleaning up the Nix store</title>
312312-313313-<para>Nix has a purely functional model, meaning that packages are
314314-never upgraded in place. Instead new versions of packages end up in a
315315-different location in the Nix store (<filename>/nix/store</filename>).
316316-You should periodically run Nix’s <emphasis>garbage
317317-collector</emphasis> to remove old, unreferenced packages. This is
318318-easy:
319319-320320-<screen>
321321-$ nix-collect-garbage
322322-</screen>
323323-324324-Alternatively, you can use a systemd unit that does the same in the
325325-background:
326326-327327-<screen>
328328-$ systemctl start nix-gc.service
329329-</screen>
330330-331331-You can tell NixOS in <filename>configuration.nix</filename> to run
332332-this unit automatically at certain points in time, for instance, every
333333-night at 03:15:
334334-335335-<programlisting>
336336-nix.gc.automatic = true;
337337-nix.gc.dates = "03:15";
338338-</programlisting>
339339-340340-</para>
341341-342342-<para>The commands above do not remove garbage collector roots, such
343343-as old system configurations. Thus they do not remove the ability to
344344-roll back to previous configurations. The following command deletes
345345-old roots, removing the ability to roll back to them:
346346-<screen>
347347-$ nix-collect-garbage -d
348348-</screen>
349349-You can also do this for specific profiles, e.g.
350350-<screen>
351351-$ nix-env -p /nix/var/nix/profiles/per-user/eelco/profile --delete-generations old
352352-</screen>
353353-Note that NixOS system configurations are stored in the profile
354354-<filename>/nix/var/nix/profiles/system</filename>.</para>
355355-356356-<para>Another way to reclaim disk space (often as much as 40% of the
357357-size of the Nix store) is to run Nix’s store optimiser, which seeks
358358-out identical files in the store and replaces them with hard links to
359359-a single copy.
360360-<screen>
361361-$ nix-store --optimise
362362-</screen>
363363-Since this command needs to read the entire Nix store, it can take
364364-quite a while to finish.</para>
365365-366366-</section>
367367-368368-369369-</chapter>
-199
nixos/doc/manual/troubleshooting.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="ch-troubleshooting">
44-55-<title>Troubleshooting</title>
66-77-88-<!--===============================================================-->
99-1010-<section xml:id="sec-boot-problems"><title>Boot problems</title>
1111-1212-<para>If NixOS fails to boot, there are a number of kernel command
1313-line parameters that may help you to identify or fix the issue. You
1414-can add these parameters in the GRUB boot menu by pressing “e” to
1515-modify the selected boot entry and editing the line starting with
1616-<literal>linux</literal>. The following are some useful kernel command
1717-line parameters that are recognised by the NixOS boot scripts or by
1818-systemd:
1919-2020-<variablelist>
2121-2222- <varlistentry><term><literal>boot.shell_on_fail</literal></term>
2323- <listitem><para>Start a root shell if something goes wrong in
2424- stage 1 of the boot process (the initial ramdisk). This is
2525- disabled by default because there is no authentication for the
2626- root shell.</para></listitem>
2727- </varlistentry>
2828-2929- <varlistentry><term><literal>boot.debug1</literal></term>
3030- <listitem><para>Start an interactive shell in stage 1 before
3131- anything useful has been done. That is, no modules have been
3232- loaded and no file systems have been mounted, except for
3333- <filename>/proc</filename> and
3434- <filename>/sys</filename>.</para></listitem>
3535- </varlistentry>
3636-3737- <varlistentry><term><literal>boot.trace</literal></term>
3838- <listitem><para>Print every shell command executed by the stage 1
3939- and 2 boot scripts.</para></listitem>
4040- </varlistentry>
4141-4242- <varlistentry><term><literal>single</literal></term>
4343- <listitem><para>Boot into rescue mode (a.k.a. single user mode).
4444- This will cause systemd to start nothing but the unit
4545- <literal>rescue.target</literal>, which runs
4646- <command>sulogin</command> to prompt for the root password and
4747- start a root login shell. Exiting the shell causes the system to
4848- continue with the normal boot process.</para></listitem>
4949- </varlistentry>
5050-5151- <varlistentry><term><literal>systemd.log_level=debug systemd.log_target=console</literal></term>
5252- <listitem><para>Make systemd very verbose and send log messages to
5353- the console instead of the journal.</para></listitem>
5454- </varlistentry>
5555-5656-</variablelist>
5757-5858-For more parameters recognised by systemd, see
5959-<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.</para>
6060-6161-<para>If no login prompts or X11 login screens appear (e.g. due to
6262-hanging dependencies), you can press Alt+ArrowUp. If you’re lucky,
6363-this will start rescue mode (described above). (Also note that since
6464-most units have a 90-second timeout before systemd gives up on them,
6565-the <command>agetty</command> login prompts should appear eventually
6666-unless something is very wrong.)</para>
6767-6868-</section>
6969-7070-7171-<!--===============================================================-->
7272-7373-<section xml:id="sec-maintenance-mode"><title>Maintenance mode</title>
7474-7575-<para>You can enter rescue mode by running:
7676-7777-<screen>
7878-$ systemctl rescue</screen>
7979-8080-This will eventually give you a single-user root shell. Systemd will
8181-stop (almost) all system services. To get out of maintenance mode,
8282-just exit from the rescue shell.</para>
8383-8484-</section>
8585-8686-8787-<!--===============================================================-->
8888-8989-<section xml:id="sec-rollback"><title>Rolling back configuration changes</title>
9090-9191-<para>After running <command>nixos-rebuild</command> to switch to a
9292-new configuration, you may find that the new configuration doesn’t
9393-work very well. In that case, there are several ways to return to a
9494-previous configuration.</para>
9595-9696-<para>First, the GRUB boot manager allows you to boot into any
9797-previous configuration that hasn’t been garbage-collected. These
9898-configurations can be found under the GRUB submenu “NixOS - All
9999-configurations”. This is especially useful if the new configuration
100100-fails to boot. After the system has booted, you can make the selected
101101-configuration the default for subsequent boots:
102102-103103-<screen>
104104-$ /run/current-system/bin/switch-to-configuration boot</screen>
105105-106106-</para>
107107-108108-<para>Second, you can switch to the previous configuration in a running
109109-system:
110110-111111-<screen>
112112-$ nixos-rebuild switch --rollback</screen>
113113-114114-This is equivalent to running:
115115-116116-<screen>
117117-$ /nix/var/nix/profiles/system-<replaceable>N</replaceable>-link/bin/switch-to-configuration switch</screen>
118118-119119-where <replaceable>N</replaceable> is the number of the NixOS system
120120-configuration. To get a list of the available configurations, do:
121121-122122-<screen>
123123-$ ls -l /nix/var/nix/profiles/system-*-link
124124-<replaceable>...</replaceable>
125125-lrwxrwxrwx 1 root root 78 Aug 12 13:54 /nix/var/nix/profiles/system-268-link -> /nix/store/202b...-nixos-13.07pre4932_5a676e4-4be1055
126126-</screen>
127127-128128-</para>
129129-130130-</section>
131131-132132-133133-<!--===============================================================-->
134134-135135-<section xml:id="sec-nix-store-corruption"><title>Nix store corruption</title>
136136-137137-<para>After a system crash, it’s possible for files in the Nix store
138138-to become corrupted. (For instance, the Ext4 file system has the
139139-tendency to replace un-synced files with zero bytes.) NixOS tries
140140-hard to prevent this from happening: it performs a
141141-<command>sync</command> before switching to a new configuration, and
142142-Nix’s database is fully transactional. If corruption still occurs,
143143-you may be able to fix it automatically.</para>
144144-145145-<para>If the corruption is in a path in the closure of the NixOS
146146-system configuration, you can fix it by doing
147147-148148-<screen>
149149-$ nixos-rebuild switch --repair
150150-</screen>
151151-152152-This will cause Nix to check every path in the closure, and if its
153153-cryptographic hash differs from the hash recorded in Nix’s database,
154154-the path is rebuilt or redownloaded.</para>
155155-156156-<para>You can also scan the entire Nix store for corrupt paths:
157157-158158-<screen>
159159-$ nix-store --verify --check-contents --repair
160160-</screen>
161161-162162-Any corrupt paths will be redownloaded if they’re available in a
163163-binary cache; otherwise, they cannot be repaired.</para>
164164-165165-</section>
166166-167167-168168-<!--===============================================================-->
169169-170170-<section xml:id="sec-nix-network-issues"><title>Nix network issues</title>
171171-172172-<para>Nix uses a so-called <emphasis>binary cache</emphasis> to
173173-optimise building a package from source into downloading it as a
174174-pre-built binary. That is, whenever a command like
175175-<command>nixos-rebuild</command> needs a path in the Nix store, Nix
176176-will try to download that path from the Internet rather than build it
177177-from source. The default binary cache is
178178-<uri>http://cache.nixos.org/</uri>. If this cache is unreachable, Nix
179179-operations may take a long time due to HTTP connection timeouts. You
180180-can disable the use of the binary cache by adding <option>--option
181181-use-binary-caches false</option>, e.g.
182182-183183-<screen>
184184-$ nixos-rebuild switch --option use-binary-caches false
185185-</screen>
186186-187187-If you have an alternative binary cache at your disposal, you can use
188188-it instead:
189189-190190-<screen>
191191-$ nixos-rebuild switch --option binary-caches http://my-cache.example.org/
192192-</screen>
193193-194194-</para>
195195-196196-</section>
197197-198198-199199-</chapter>