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