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