1<chapter xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:xi="http://www.w3.org/2001/XInclude"
4 version="5.0"
5 xml:id="sec-installation">
6
7<title>Installing NixOS</title>
8
9<orderedlist>
10
11 <listitem><para>Boot from the CD.</para></listitem>
12
13 <listitem><para>The CD contains a basic NixOS installation. (It
14 also contains Memtest86+, useful if you want to test new hardware).
15 When it’s finished booting, it should have detected most of your
16 hardware.</para></listitem>
17
18 <listitem><para>The NixOS manual is available on virtual console 8
19 (press Alt+F8 to access).</para></listitem>
20
21 <listitem><para>You get logged in as <literal>root</literal>
22 (with empty password).</para></listitem>
23
24 <listitem><para>If you downloaded the graphical ISO image, you can
25 run <command>systemctl start display-manager</command> to start KDE. If you
26 want to continue on the terminal, you can use
27 <command>loadkeys</command> to switch to your preferred keyboard layout.
28 (We even provide neo2 via <command>loadkeys de neo</command>!)</para></listitem>
29
30 <listitem><para>The boot process should have brought up networking (check
31 <command>ip a</command>). Networking is necessary for the
32 installer, since it will download lots of stuff (such as source
33 tarballs or Nixpkgs channel binaries). It’s best if you have a DHCP
34 server on your network. Otherwise configure networking manually
35 using <command>ifconfig</command>.</para>
36 <para>To manually configure the network on the graphical installer,
37 first disable network-manager with
38 <command>systemctl stop network-manager</command>.</para></listitem>
39
40 <listitem><para>If you would like to continue the installation from a different
41 machine you need to activate the SSH daemon via <literal>systemctl start sshd</literal>.
42 In order to be able to login you also need to set a password for
43 <literal>root</literal> using <literal>passwd</literal>.</para></listitem>
44
45 <listitem><para>The NixOS installer doesn’t do any partitioning or
46 formatting yet, so you need to do that yourself. Use the following
47 commands:
48
49 <itemizedlist>
50
51 <listitem><para>For partitioning:
52 <command>fdisk</command>.</para></listitem>
53
54 <listitem><para>For initialising Ext4 partitions:
55 <command>mkfs.ext4</command>. It is recommended that you assign a
56 unique symbolic label to the file system using the option
57 <option>-L <replaceable>label</replaceable></option>, since this
58 makes the file system configuration independent from device
59 changes. For example:
60
61<screen>
62# mkfs.ext4 -L nixos /dev/sda1</screen>
63
64 </para></listitem>
65
66 <listitem><para>For creating swap partitions:
67 <command>mkswap</command>. Again it’s recommended to assign a
68 label to the swap partition: <option>-L
69 <replaceable>label</replaceable></option>.</para></listitem>
70
71 <listitem><para>For creating LVM volumes, the LVM commands, e.g.,
72
73<screen>
74# pvcreate /dev/sda1 /dev/sdb1
75# vgcreate MyVolGroup /dev/sda1 /dev/sdb1
76# lvcreate --size 2G --name bigdisk MyVolGroup
77# lvcreate --size 1G --name smalldisk MyVolGroup</screen>
78
79 </para></listitem>
80
81 <listitem><para>For creating software RAID devices, use
82 <command>mdadm</command>.</para></listitem>
83
84 </itemizedlist>
85
86 </para></listitem>
87
88 <listitem><para>Mount the target file system on which NixOS should
89 be installed on <filename>/mnt</filename>, e.g.
90
91<screen>
92# mount /dev/disk/by-label/nixos /mnt
93</screen>
94
95 </para></listitem>
96
97 <listitem><para>If your machine has a limited amount of memory, you
98 may want to activate swap devices now (<command>swapon
99 <replaceable>device</replaceable></command>). The installer (or
100 rather, the build actions that it may spawn) may need quite a bit of
101 RAM, depending on your configuration.</para></listitem>
102
103 <listitem>
104
105 <para>You now need to create a file
106 <filename>/mnt/etc/nixos/configuration.nix</filename> that
107 specifies the intended configuration of the system. This is
108 because NixOS has a <emphasis>declarative</emphasis> configuration
109 model: you create or edit a description of the desired
110 configuration of your system, and then NixOS takes care of making
111 it happen. The syntax of the NixOS configuration file is
112 described in <xref linkend="sec-configuration-syntax"/>, while a
113 list of available configuration options appears in <xref
114 linkend="ch-options"/>. A minimal example is shown in <xref
115 linkend="ex-config"/>.</para>
116
117 <para>The command <command>nixos-generate-config</command> can
118 generate an initial configuration file for you:
119
120<screen>
121# nixos-generate-config --root /mnt</screen>
122
123 You should then edit
124 <filename>/mnt/etc/nixos/configuration.nix</filename> to suit your
125 needs:
126
127<screen>
128# nano /mnt/etc/nixos/configuration.nix
129</screen>
130
131 If you’re using the graphical ISO image, other editors may be
132 available (such as <command>vim</command>). If you have network
133 access, you can also install other editors — for instance, you can
134 install Emacs by running <literal>nix-env -i
135 emacs</literal>.</para>
136
137 <para>You <emphasis>must</emphasis> set the option
138 <option>boot.loader.grub.device</option> to specify on which disk
139 the GRUB boot loader is to be installed. Without it, NixOS cannot
140 boot.</para>
141
142 <para>Another critical option is <option>fileSystems</option>,
143 specifying the file systems that need to be mounted by NixOS.
144 However, you typically don’t need to set it yourself, because
145 <command>nixos-generate-config</command> sets it automatically in
146 <filename>/mnt/etc/nixos/hardware-configuration.nix</filename>
147 from your currently mounted file systems. (The configuration file
148 <filename>hardware-configuration.nix</filename> is included from
149 <filename>configuration.nix</filename> and will be overwritten by
150 future invocations of <command>nixos-generate-config</command>;
151 thus, you generally should not modify it.)</para>
152
153 <note><para>Depending on your hardware configuration or type of
154 file system, you may need to set the option
155 <option>boot.initrd.kernelModules</option> to include the kernel
156 modules that are necessary for mounting the root file system,
157 otherwise the installed system will not be able to boot. (If this
158 happens, boot from the CD again, mount the target file system on
159 <filename>/mnt</filename>, fix
160 <filename>/mnt/etc/nixos/configuration.nix</filename> and rerun
161 <filename>nixos-install</filename>.) In most cases,
162 <command>nixos-generate-config</command> will figure out the
163 required modules.</para></note>
164
165 </listitem>
166
167 <listitem><para>Do the installation:
168
169<screen>
170# nixos-install</screen>
171
172 Cross fingers. If this fails due to a temporary problem (such as
173 a network issue while downloading binaries from the NixOS binary
174 cache), you can just re-run <command>nixos-install</command>.
175 Otherwise, fix your <filename>configuration.nix</filename> and
176 then re-run <command>nixos-install</command>.</para>
177
178 <para>As the last step, <command>nixos-install</command> will ask
179 you to set the password for the <literal>root</literal> user, e.g.
180
181<screen>
182setting root password...
183Enter new UNIX password: ***
184Retype new UNIX password: ***
185</screen>
186
187 </para>
188
189 </listitem>
190
191 <listitem><para>If everything went well:
192
193<screen>
194# reboot</screen>
195
196 </para></listitem>
197
198 <listitem>
199
200 <para>You should now be able to boot into the installed NixOS. The
201 GRUB boot menu shows a list of <emphasis>available
202 configurations</emphasis> (initially just one). Every time you
203 change the NixOS configuration (see <link
204 linkend="sec-changing-config">Changing Configuration</link> ), a
205 new item is added to the menu. This allows you to easily roll back
206 to a previous configuration if something goes wrong.</para>
207
208 <para>You should log in and change the <literal>root</literal>
209 password with <command>passwd</command>.</para>
210
211 <para>You’ll probably want to create some user accounts as well,
212 which can be done with <command>useradd</command>:
213
214<screen>
215$ useradd -c 'Eelco Dolstra' -m eelco
216$ passwd eelco</screen>
217
218 </para>
219
220 <para>You may also want to install some software. For instance,
221
222<screen>
223$ nix-env -qa \*</screen>
224
225 shows what packages are available, and
226
227<screen>
228$ nix-env -i w3m</screen>
229
230 install the <literal>w3m</literal> browser.</para>
231
232 </listitem>
233
234</orderedlist>
235
236<para>To summarise, <xref linkend="ex-install-sequence" /> shows a
237typical sequence of commands for installing NixOS on an empty hard
238drive (here <filename>/dev/sda</filename>). <xref linkend="ex-config"
239/> shows a corresponding configuration Nix expression.</para>
240
241<example xml:id='ex-install-sequence'><title>Commands for Installing NixOS on <filename>/dev/sda</filename></title>
242<screen>
243# fdisk /dev/sda # <lineannotation>(or whatever device you want to install on)</lineannotation>
244# mkfs.ext4 -L nixos /dev/sda1
245# mkswap -L swap /dev/sda2
246# swapon /dev/sda2
247# mount /dev/disk/by-label/nixos /mnt
248# nixos-generate-config --root /mnt
249# nano /mnt/etc/nixos/configuration.nix
250# nixos-install
251# reboot</screen>
252</example>
253
254<example xml:id='ex-config'><title>NixOS Configuration</title>
255<screen>
256{ config, pkgs, ... }:
257
258{
259 imports =
260 [ # Include the results of the hardware scan.
261 ./hardware-configuration.nix
262 ];
263
264 boot.loader.grub.device = "/dev/sda";
265
266 # Note: setting fileSystems is generally not
267 # necessary, since nixos-generate-config figures them out
268 # automatically in hardware-configuration.nix.
269 #fileSystems."/".device = "/dev/disk/by-label/nixos";
270
271 # Enable the OpenSSH server.
272 services.sshd.enable = true;
273}</screen>
274</example>
275
276<xi:include href="installing-uefi.xml" />
277<xi:include href="installing-usb.xml" />
278<xi:include href="installing-pxe.xml" />
279<xi:include href="installing-virtualbox-guest.xml" />
280
281</chapter>