1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="sec-installing-from-other-distro">
<title>Installing from another Linux distribution</title>
<para>
Because Nix (the package manager) & Nixpkgs (the Nix packages
collection) can both be installed on any (most?) Linux
distributions, they can be used to install NixOS in various creative
ways. You can, for instance:
</para>
<orderedlist numeration="arabic">
<listitem>
<para>
Install NixOS on another partition, from your existing Linux
distribution (without the use of a USB or optical device!)
</para>
</listitem>
<listitem>
<para>
Install NixOS on the same partition (in place!), from your
existing non-NixOS Linux distribution using
<literal>NIXOS_LUSTRATE</literal>.
</para>
</listitem>
<listitem>
<para>
Install NixOS on your hard drive from the Live CD of any Linux
distribution.
</para>
</listitem>
</orderedlist>
<para>
The first steps to all these are the same:
</para>
<orderedlist numeration="arabic">
<listitem>
<para>
Install the Nix package manager:
</para>
<para>
Short version:
</para>
<programlisting>
$ curl -L https://nixos.org/nix/install | sh
$ . $HOME/.nix-profile/etc/profile.d/nix.sh # …or open a fresh shell
</programlisting>
<para>
More details in the
<link xlink:href="https://nixos.org/nix/manual/#chap-quick-start">
Nix manual</link>
</para>
</listitem>
<listitem>
<para>
Switch to the NixOS channel:
</para>
<para>
If you've just installed Nix on a non-NixOS distribution, you
will be on the <literal>nixpkgs</literal> channel by default.
</para>
<programlisting>
$ nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable
</programlisting>
<para>
As that channel gets released without running the NixOS tests,
it will be safer to use the <literal>nixos-*</literal> channels
instead:
</para>
<programlisting>
$ nix-channel --add https://nixos.org/channels/nixos-version nixpkgs
</programlisting>
<para>
You may want to throw in a
<literal>nix-channel --update</literal> for good measure.
</para>
</listitem>
<listitem>
<para>
Install the NixOS installation tools:
</para>
<para>
You'll need <literal>nixos-generate-config</literal> and
<literal>nixos-install</literal>, but this also makes some man
pages and <literal>nixos-enter</literal> available, just in case
you want to chroot into your NixOS partition. NixOS installs
these by default, but you don't have NixOS yet..
</para>
<programlisting>
$ nix-env -f '<nixpkgs>' -iA nixos-install-tools
</programlisting>
</listitem>
<listitem>
<note>
<para>
The following 5 steps are only for installing NixOS to another
partition. For installing NixOS in place using
<literal>NIXOS_LUSTRATE</literal>, skip ahead.
</para>
</note>
<para>
Prepare your target partition:
</para>
<para>
At this point it is time to prepare your target partition.
Please refer to the partitioning, file-system creation, and
mounting steps of <xref linkend="sec-installation" />
</para>
<para>
If you're about to install NixOS in place using
<literal>NIXOS_LUSTRATE</literal> there is nothing to do for
this step.
</para>
</listitem>
<listitem>
<para>
Generate your NixOS configuration:
</para>
<programlisting>
$ sudo `which nixos-generate-config` --root /mnt
</programlisting>
<para>
You'll probably want to edit the configuration files. Refer to
the <literal>nixos-generate-config</literal> step in
<xref linkend="sec-installation" /> for more information.
</para>
<para>
Consider setting up the NixOS bootloader to give you the ability
to boot on your existing Linux partition. For instance, if
you're using GRUB and your existing distribution is running
Ubuntu, you may want to add something like this to your
<literal>configuration.nix</literal>:
</para>
<programlisting language="bash">
boot.loader.grub.extraEntries = ''
menuentry "Ubuntu" {
search --set=ubuntu --fs-uuid 3cc3e652-0c1f-4800-8451-033754f68e6e
configfile "($ubuntu)/boot/grub/grub.cfg"
}
'';
</programlisting>
<para>
(You can find the appropriate UUID for your partition in
<literal>/dev/disk/by-uuid</literal>)
</para>
</listitem>
<listitem>
<para>
Create the <literal>nixbld</literal> group and user on your
original distribution:
</para>
<programlisting>
$ sudo groupadd -g 30000 nixbld
$ sudo useradd -u 30000 -g nixbld -G nixbld nixbld
</programlisting>
</listitem>
<listitem>
<para>
Download/build/install NixOS:
</para>
<warning>
<para>
Once you complete this step, you might no longer be able to
boot on existing systems without the help of a rescue USB
drive or similar.
</para>
</warning>
<note>
<para>
On some distributions there are separate PATHS for programs
intended only for root. In order for the installation to
succeed, you might have to use
<literal>PATH="$PATH:/usr/sbin:/sbin"</literal> in
the following command.
</para>
</note>
<programlisting>
$ sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt
</programlisting>
<para>
Again, please refer to the <literal>nixos-install</literal> step
in <xref linkend="sec-installation" /> for more information.
</para>
<para>
That should be it for installation to another partition!
</para>
</listitem>
<listitem>
<para>
Optionally, you may want to clean up your non-NixOS
distribution:
</para>
<programlisting>
$ sudo userdel nixbld
$ sudo groupdel nixbld
</programlisting>
<para>
If you do not wish to keep the Nix package manager installed
either, run something like
<literal>sudo rm -rv ~/.nix-* /nix</literal> and remove the line
that the Nix installer added to your
<literal>~/.profile</literal>.
</para>
</listitem>
<listitem>
<note>
<para>
The following steps are only for installing NixOS in place
using <literal>NIXOS_LUSTRATE</literal>:
</para>
</note>
<para>
Generate your NixOS configuration:
</para>
<programlisting>
$ sudo `which nixos-generate-config`
</programlisting>
<para>
Note that this will place the generated configuration files in
<literal>/etc/nixos</literal>. You'll probably want to edit the
configuration files. Refer to the
<literal>nixos-generate-config</literal> step in
<xref linkend="sec-installation" /> for more information.
</para>
<para>
You'll likely want to set a root password for your first boot
using the configuration files because you won't have a chance to
enter a password until after you reboot. You can initalize the
root password to an empty one with this line: (and of course
don't forget to set one once you've rebooted or to lock the
account with <literal>sudo passwd -l root</literal> if you use
<literal>sudo</literal>)
</para>
<programlisting language="bash">
users.users.root.initialHashedPassword = "";
</programlisting>
</listitem>
<listitem>
<para>
Build the NixOS closure and install it in the
<literal>system</literal> profile:
</para>
<programlisting>
$ nix-env -p /nix/var/nix/profiles/system -f '<nixpkgs/nixos>' -I nixos-config=/etc/nixos/configuration.nix -iA system
</programlisting>
</listitem>
<listitem>
<para>
Change ownership of the <literal>/nix</literal> tree to root
(since your Nix install was probably single user):
</para>
<programlisting>
$ sudo chown -R 0:0 /nix
</programlisting>
</listitem>
<listitem>
<para>
Set up the <literal>/etc/NIXOS</literal> and
<literal>/etc/NIXOS_LUSTRATE</literal> files:
</para>
<para>
<literal>/etc/NIXOS</literal> officializes that this is now a
NixOS partition (the bootup scripts require its presence).
</para>
<para>
<literal>/etc/NIXOS_LUSTRATE</literal> tells the NixOS bootup
scripts to move <emphasis>everything</emphasis> that's in the
root partition to <literal>/old-root</literal>. This will move
your existing distribution out of the way in the very early
stages of the NixOS bootup. There are exceptions (we do need to
keep NixOS there after all), so the NixOS lustrate process will
not touch:
</para>
<itemizedlist>
<listitem>
<para>
The <literal>/nix</literal> directory
</para>
</listitem>
<listitem>
<para>
The <literal>/boot</literal> directory
</para>
</listitem>
<listitem>
<para>
Any file or directory listed in
<literal>/etc/NIXOS_LUSTRATE</literal> (one per line)
</para>
</listitem>
</itemizedlist>
<note>
<para>
Support for <literal>NIXOS_LUSTRATE</literal> was added in
NixOS 16.09. The act of "lustrating" refers to the
wiping of the existing distribution. Creating
<literal>/etc/NIXOS_LUSTRATE</literal> can also be used on
NixOS to remove all mutable files from your root partition
(anything that's not in <literal>/nix</literal> or
<literal>/boot</literal> gets "lustrated" on the
next boot.
</para>
<para>
lustrate /ˈlʌstreɪt/ verb.
</para>
<para>
purify by expiatory sacrifice, ceremonial washing, or some
other ritual action.
</para>
</note>
<para>
Let's create the files:
</para>
<programlisting>
$ sudo touch /etc/NIXOS
$ sudo touch /etc/NIXOS_LUSTRATE
</programlisting>
<para>
Let's also make sure the NixOS configuration files are kept once
we reboot on NixOS:
</para>
<programlisting>
$ echo etc/nixos | sudo tee -a /etc/NIXOS_LUSTRATE
</programlisting>
</listitem>
<listitem>
<para>
Finally, move the <literal>/boot</literal> directory of your
current distribution out of the way (the lustrate process will
take care of the rest once you reboot, but this one must be
moved out now because NixOS needs to install its own boot files:
</para>
<warning>
<para>
Once you complete this step, your current distribution will no
longer be bootable! If you didn't get all the NixOS
configuration right, especially those settings pertaining to
boot loading and root partition, NixOS may not be bootable
either. Have a USB rescue device ready in case this happens.
</para>
</warning>
<programlisting>
$ sudo mv -v /boot /boot.bak &&
sudo /nix/var/nix/profiles/system/bin/switch-to-configuration boot
</programlisting>
<para>
Cross your fingers, reboot, hopefully you should get a NixOS
prompt!
</para>
</listitem>
<listitem>
<para>
If for some reason you want to revert to the old distribution,
you'll need to boot on a USB rescue disk and do something along
these lines:
</para>
<programlisting>
# mkdir root
# mount /dev/sdaX root
# mkdir root/nixos-root
# mv -v root/* root/nixos-root/
# mv -v root/nixos-root/old-root/* root/
# mv -v root/boot.bak root/boot # We had renamed this by hand earlier
# umount root
# reboot
</programlisting>
<para>
This may work as is or you might also need to reinstall the boot
loader.
</para>
<para>
And of course, if you're happy with NixOS and no longer need the
old distribution:
</para>
<programlisting>
sudo rm -rf /old-root
</programlisting>
</listitem>
<listitem>
<para>
It's also worth noting that this whole process can be automated.
This is especially useful for Cloud VMs, where provider do not
provide NixOS. For instance,
<link xlink:href="https://github.com/elitak/nixos-infect">nixos-infect</link>
uses the lustrate process to convert Digital Ocean droplets to
NixOS from other distributions automatically.
</para>
</listitem>
</orderedlist>
</section>
|