···11+<!-- vim: set expandtab ts=2 softtabstop=2 shiftwidth=2 smarttab textwidth=80 wrapmargin=2 -->
22+<section
33+ xmlns="http://docbook.org/ns/docbook"
44+ xmlns:xlink="http://www.w3.org/1999/xlink"
55+ xmlns:xi="http://www.w3.org/2001/XInclude"
66+ version="5.0"
77+ xml:id="sec-installing-from-other-distro">
88+99+ <title>Installing from another Linux distribution</title>
1010+1111+ <para>
1212+ Because Nix (the package manager) & Nixpkgs (the Nix packages
1313+ collection) can both be installed on any (most?) Linux distributions,
1414+ they can be used to install NixOS in various creative ways. You can,
1515+ for instance:
1616+ </para>
1717+1818+ <orderedlist>
1919+ <listitem><para>Install NixOS on another partition, from your existing
2020+ Linux distribution (without the use of a USB or optical
2121+ device!)</para></listitem>
2222+2323+ <listitem><para>Install NixOS on the same partition (in place!), from
2424+ your existing non-NixOS Linux distribution using
2525+ <literal>NIXOS_LUSTRATE</literal>.</para></listitem>
2626+2727+ <listitem><para>Install NixOS on your hard drive from the Live CD of
2828+ any Linux distribution.</para></listitem>
2929+ </orderedlist>
3030+3131+ <para>The first steps to all these are the same:</para>
3232+3333+ <orderedlist>
3434+ <listitem>
3535+ <para>Install the Nix package manager:</para>
3636+3737+ <para>Short version:</para>
3838+3939+ <screen>
4040+$ bash <(curl https://nixos.org/nix/install)
4141+$ . $HOME/.nix-profile/etc/profile.d/nix.sh # …or open a fresh shell</screen>
4242+4343+ <para>More details in the <link
4444+ xlink:href="https://nixos.org/nix/manual/#chap-quick-start">
4545+ Nix manual</link></para>
4646+ </listitem>
4747+4848+ <listitem>
4949+ <para>Switch to the NixOS channel:</para>
5050+5151+ <para>If you've just installed Nix on a non-NixOS distribution, you
5252+ will be on the <literal>nixpkgs</literal> channel by
5353+ default.</para>
5454+5555+ <screen>
5656+$ nix-channel --list
5757+nixpkgs https://nixos.org/channels/nixpkgs-unstable</screen>
5858+5959+ <para>As that channel gets released without running the NixOS
6060+ tests, it will be safer to use the <literal>nixos-*</literal>
6161+ channels instead:</para>
6262+6363+ <screen>
6464+$ nix-channel --add https://nixos.org/channels/nixos-<replaceable>version</replaceable> nixpkgs</screen>
6565+6666+ <para>You may want to throw in a <literal>nix-channel
6767+ --update</literal> for good measure.</para>
6868+ </listitem>
6969+7070+ <listitem>
7171+ <para>Install the NixOS installation tools:</para>
7272+7373+ <para>You'll need <literal>nixos-generate-config</literal> and
7474+ <literal>nixos-install</literal> and we'll throw in some man
7575+ pages and <literal>nixos-enter</literal> just in case you want
7676+ to chroot into your NixOS partition. They are installed by
7777+ default on NixOS, but you don't have NixOS yet..</para>
7878+7979+ <screen>$ nix-env -iE "_: with import <nixpkgs/nixos> { configuration = {}; }; with config.system.build; [ nixos-generate-config nixos-install nixos-enter manual.manpages ]"</screen>
8080+ </listitem>
8181+8282+ <listitem>
8383+ <note><para>The following 5 steps are only for installing NixOS to
8484+ another partition. For installing NixOS in place using
8585+ <literal>NIXOS_LUSTRATE</literal>, skip ahead.</para></note>
8686+8787+ <para>Prepare your target partition:</para>
8888+8989+ <para>At this point it is time to prepare your target partition.
9090+ Please refer to the partitioning, file-system creation, and
9191+ mounting steps of <xref linkend="sec-installation" /></para>
9292+9393+ <para>If you're about to install NixOS in place using
9494+ <literal>NIXOS_LUSTRATE</literal> there is nothing to do for
9595+ this step.</para>
9696+ </listitem>
9797+9898+ <listitem>
9999+ <para>Generate your NixOS configuration:</para>
100100+101101+ <screen>$ sudo `which nixos-generate-config` --root /mnt</screen>
102102+103103+ <para>You'll probably want to edit the configuration files. Refer
104104+ to the <literal>nixos-generate-config</literal> step in <xref
105105+ linkend="sec-installation" /> for more information.</para>
106106+107107+ <para>Consider setting up the NixOS bootloader to give you the
108108+ ability to boot on your existing Linux partition. For instance,
109109+ if you're using GRUB and your existing distribution is running
110110+ Ubuntu, you may want to add something like this to your
111111+ <literal>configuration.nix</literal>:</para>
112112+113113+ <programlisting>
114114+boot.loader.grub.extraEntries = ''
115115+ menuentry "Ubuntu" {
116116+ search --set=ubuntu --fs-uuid 3cc3e652-0c1f-4800-8451-033754f68e6e
117117+ configfile "($ubuntu)/boot/grub/grub.cfg"
118118+ }
119119+'';</programlisting>
120120+121121+ <para>(You can find the appropriate UUID for your partition in
122122+ <literal>/dev/disk/by-uuid</literal>)</para>
123123+ </listitem>
124124+125125+ <listitem>
126126+ <para>Create the <literal>nixbld</literal> group and user on your
127127+ original distribution:</para>
128128+129129+ <screen>
130130+$ sudo groupadd -g 30000 nixbld
131131+$ sudo useradd -u 30000 -g nixbld -G nixbld nixbld</screen>
132132+ </listitem>
133133+134134+ <listitem>
135135+ <para>Download/build/install NixOS:</para>
136136+137137+ <warning><para>Once you complete this step, you might no longer be
138138+ able to boot on existing systems without the help of a
139139+ rescue USB drive or similar.</para></warning>
140140+141141+ <screen>$ sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt</screen>
142142+143143+ <para>Again, please refer to the <literal>nixos-install</literal>
144144+ step in <xref linkend="sec-installation" /> for more
145145+ information.</para>
146146+147147+ <para>That should be it for installation to another partition!</para>
148148+ </listitem>
149149+150150+ <listitem>
151151+ <para>Optionally, you may want to clean up your non-NixOS distribution:</para>
152152+153153+ <screen>
154154+$ sudo userdel nixbld
155155+$ sudo groupdel nixbld</screen>
156156+157157+ <para>If you do not wish to keep the Nix package mananager
158158+ installed either, run something like <literal>sudo rm -rv
159159+ ~/.nix-* /nix</literal> and remove the line that the Nix
160160+ installer added to your <literal>~/.profile</literal>.</para>
161161+ </listitem>
162162+163163+ <listitem>
164164+ <note><para>The following steps are only for installing NixOS in
165165+ place using
166166+ <literal>NIXOS_LUSTRATE</literal>:</para></note>
167167+168168+ <para>Generate your NixOS configuration:</para>
169169+170170+ <screen>$ sudo `which nixos-generate-config` --root /</screen>
171171+172172+ <para>Note that this will place the generated configuration files
173173+ in <literal>/etc/nixos</literal>. You'll probably want to edit
174174+ the configuration files. Refer to the
175175+ <literal>nixos-generate-config</literal> step in <xref
176176+ linkend="sec-installation" /> for more information.</para>
177177+178178+ <para>You'll likely want to set a root password for your first boot
179179+ using the configuration files because you won't have a chance
180180+ to enter a password until after you reboot. You can initalize
181181+ the root password to an empty one with this line: (and of course
182182+ don't forget to set one once you've rebooted or to lock the
183183+ account with <literal>sudo passwd -l root</literal> if you use
184184+ <literal>sudo</literal>)</para>
185185+186186+ <programlisting>users.extraUsers.root.initialHashedPassword = "";</programlisting>
187187+ </listitem>
188188+189189+ <listitem>
190190+ <para>Build the NixOS closure and install it in the
191191+ <literal>system</literal> profile:</para>
192192+193193+ <screen>$ nix-env -p /nix/var/nix/profiles/system -f '<nixpkgs/nixos>' -I nixos-config=/etc/nixos/configuration.nix -iA system</screen>
194194+ </listitem>
195195+196196+ <listitem>
197197+ <para>Change ownership of the <literal>/nix</literal> tree to root
198198+ (since your Nix install was probably single user):</para>
199199+200200+ <screen>$ sudo chown -R 0.0 /nix</screen>
201201+ </listitem>
202202+203203+ <listitem>
204204+ <para>Set up the <literal>/etc/NIXOS</literal> and
205205+ <literal>/etc/NIXOS_LUSTRATE</literal> files:</para>
206206+207207+ <para><literal>/etc/NIXOS</literal> officializes that this is now a
208208+ NixOS partition (the bootup scripts require its presence).</para>
209209+210210+ <para><literal>/etc/NIXOS_LUSTRATE</literal> tells the NixOS bootup
211211+ scripts to move <emphasis>everything</emphasis> that's in the
212212+ root partition to <literal>/old-root</literal>. This will move
213213+ your existing distribution out of the way in the very early
214214+ stages of the NixOS bootup. There are exceptions (we do need to
215215+ keep NixOS there after all), so the NixOS lustrate process will
216216+ not touch:</para>
217217+218218+ <itemizedlist>
219219+ <listitem><para>The <literal>/nix</literal>
220220+ directory</para></listitem>
221221+222222+ <listitem><para>The <literal>/boot</literal>
223223+ directory</para></listitem>
224224+225225+ <listitem><para>Any file or directory listed in
226226+ <literal>/etc/NIXOS_LUSTRATE</literal> (one per
227227+ line)</para></listitem>
228228+ </itemizedlist>
229229+230230+ <para>Let's create the files:</para>
231231+232232+ <screen>
233233+$ sudo touch /etc/NIXOS
234234+$ sudo touch /etc/NIXOS_LUSTRATE</screen>
235235+236236+ <para>Let's also make sure the NixOS configuration files are kept
237237+ once we reboot on NixOS:</para>
238238+239239+ <screen>
240240+$ echo etc/nixos | sudo tee -a /etc/NIXOS_LUSTRATE</screen>
241241+ </listitem>
242242+243243+ <listitem>
244244+ <para>Finally, move the <literal>/boot</literal> directory of your
245245+ current distribution out of the way (the lustrate process will
246246+ take care of the rest once you reboot, but this one must be
247247+ moved out now because NixOS needs to install its own boot
248248+ files:</para>
249249+250250+ <warning><para>Once you complete this step, your current
251251+ distribution will no longer be bootable! If you didn't get
252252+ all the NixOS configuration right, especially those
253253+ settings pertaining to boot loading and root partition,
254254+ NixOS may not be bootable either. Have a USB rescue device
255255+ ready in case this happens. </para></warning>
256256+257257+ <screen>
258258+$ sudo mv -v /boot /boot.bak &&
259259+ sudo /nix/var/nix/profiles/system/bin/switch-to-configuration boot</screen>
260260+261261+ <para>Cross your fingers, reboot, hopefully you should get a NixOS
262262+ prompt!</para>
263263+ </listitem>
264264+ <listitem>
265265+ <para>If for some reason you want to revert to the old
266266+ distribution, you'll need to boot on a USB rescue disk and do
267267+ something along these lines:</para>
268268+269269+ <screen>
270270+# mkdir root
271271+# mount /dev/sdaX root
272272+# mkdir root/nixos-root
273273+# mv -v root/* root/nixos-root/
274274+# mv -v root/nixos-root/old-root/* root/
275275+# mv -v root/boot.bak root/boot # We had renamed this by hand earlier
276276+# umount root
277277+# reboot</screen>
278278+279279+ <para>This may work as is or you might also need to reinstall the
280280+ boot loader</para>
281281+282282+ <para>And of course, if you're happy with NixOS and no longer need
283283+ the old distribution:</para>
284284+285285+ <screen>sudo rm -rf /old-root</screen>
286286+ </listitem>
287287+288288+ <listitem>
289289+ <para>It's also worth noting that this whole process can be
290290+ automated. This is especially useful for Cloud VMs, where
291291+ provider do not provide NixOS. For instance, <link
292292+ xlink:href="https://github.com/elitak/nixos-infect">nixos-infect</link>
293293+ uses the lustrate process to convert Digital Ocean droplets to
294294+ NixOS from other distributions automatically.</para>
295295+ </listitem>
296296+ </orderedlist>
297297+</section>