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:
-
Install NixOS on another partition, from your existing Linux distribution (without the use of a USB or optical device!)
-
Install NixOS on the same partition (in place!), from your existing non-NixOS Linux distribution using
NIXOS_LUSTRATE. -
Install NixOS on your hard drive from the Live CD of any Linux distribution.
The first steps to all these are the same:
-
Install the Nix package manager:
Short version:
$ curl -L https://nixos.org/nix/install | sh$ . $HOME/.nix-profile/etc/profile.d/nix.sh # …or open a fresh shellMore details in the Nix manual
-
Switch to the NixOS channel:
If you’ve just installed Nix on a non-NixOS distribution, you will be on the
nixpkgschannel by default.$ nix-channel --listnixpkgs https://channels.nixos.org/nixpkgs-unstableAs that channel gets released without running the NixOS tests, it will be safer to use the
nixos-*channels instead:$ nix-channel --add https://channels.nixos.org/nixos-<version> nixpkgsWhere
<version>corresponds to the latest version available on channels.nixos.org.You must run
$ nix-channel --updatefor the channel change to take effect. -
Install the NixOS installation tools:
You’ll need
nixos-generate-configandnixos-install, but this also makes some man pages andnixos-enteravailable, just in case you want to chroot into your NixOS partition. NixOS installs these by default, but you don’t have NixOS yet..$ nix-env -f '<nixpkgs>' -iA nixos-install-tools -
::: {.note} The following 5 steps are only for installing NixOS to another partition. For installing NixOS in place using
NIXOS_LUSTRATE, skip ahead. :::Prepare your target partition:
At this point it is time to prepare your target partition. Please refer to the partitioning, file-system creation, and mounting steps of
If you’re about to install NixOS in place using
NIXOS_LUSTRATEthere is nothing to do for this step. -
Generate your NixOS configuration:
$ sudo `which nixos-generate-config` --root /mntYou’ll probably want to edit the configuration files. Refer to the
nixos-generate-configstep in for more information.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
configuration.nix:{boot.loader.grub.extraEntries = ''menuentry "Ubuntu" {search --set=ubuntu --fs-uuid 3cc3e652-0c1f-4800-8451-033754f68e6econfigfile "($ubuntu)/boot/grub/grub.cfg"}'';}(You can find the appropriate UUID for your partition in
/dev/disk/by-uuid) -
Create the
nixbldgroup and user on your original distribution:$ sudo groupadd -g 30000 nixbld$ sudo useradd -u 30000 -g nixbld -G nixbld nixbld -
Download/build/install NixOS:
$ sudo PATH="$PATH" `which nixos-install` --root /mntAgain, please refer to the
nixos-installstep in for more information.That should be it for installation to another partition!
-
Optionally, you may want to clean up your non-NixOS distribution:
$ sudo userdel nixbld$ sudo groupdel nixbldIf you do not wish to keep the Nix package manager installed either, run something like
sudo rm -rv ~/.nix-* /nixand remove the line that the Nix installer added to your~/.profile. -
::: {.note} The following steps are only for installing NixOS in place using
NIXOS_LUSTRATE: :::Generate your NixOS configuration:
$ sudo `which nixos-generate-config`Note that this will place the generated configuration files in
/etc/nixos. You’ll probably want to edit the configuration files. Refer to thenixos-generate-configstep in for more information.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 initialize 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
sudo passwd -l rootif you usesudo){ users.users.root.initialHashedPassword = ""; } -
Build the NixOS closure and install it in the
systemprofile:$ nix-env -p /nix/var/nix/profiles/system -f '<nixpkgs/nixos>' -I nixos-config=/etc/nixos/configuration.nix -iA system -
Change ownership of the
/nixtree to root (since your Nix install was probably single user):$ sudo chown -R 0:0 /nix -
Set up the
/etc/NIXOSand/etc/NIXOS_LUSTRATEfiles:/etc/NIXOSofficializes that this is now a NixOS partition (the bootup scripts require its presence)./etc/NIXOS_LUSTRATEtells the NixOS bootup scripts to move everything that’s in the root partition to/old-root. 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:-
The
/nixdirectory -
The
/bootdirectory -
Any file or directory listed in
/etc/NIXOS_LUSTRATE(one per line)
Let’s create the files:
$ sudo touch /etc/NIXOS$ sudo touch /etc/NIXOS_LUSTRATELet’s also make sure the NixOS configuration files are kept once we reboot on NixOS:
$ echo etc/nixos | sudo tee -a /etc/NIXOS_LUSTRATE -
-
Finally, install NixOS’s boot system, backing up the current boot system’s files in the process.
The details of this step can vary depending on the bootloader configuration in NixOS and the bootloader in use by the current system.
The commands below should work for:
-
BIOS systems.
-
UEFI systems where both the current system and NixOS mount the EFI System Partition on
/boot. Both systemd-boot and grub expect this by default in NixOS, but other distributions vary.
$ sudo mkdir /boot.bak && sudo mv /boot/* /boot.bak &&sudo NIXOS_INSTALL_BOOTLOADER=1 /nix/var/nix/profiles/system/bin/switch-to-configuration bootCross your fingers, reboot, hopefully you should get a NixOS prompt!
In other cases, most commonly where the EFI System Partition of the current system is instead mounted on
/boot/efi, the goal is to:-
Make sure
/boot(and the EFI System Partition, if mounted elsewhere) are mounted how the NixOS configuration would mount them. -
Clear them of files related to the current system, backing them up outside of
/boot. NixOS will move the backups into/old-rootalong with everything else when it first boots. -
Instruct the NixOS closure built earlier to install its bootloader with:
sudo NIXOS_INSTALL_BOOTLOADER=1 /nix/var/nix/profiles/system/bin/switch-to-configuration boot
-
-
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:
# 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# rebootThis may work as is or you might also need to reinstall the boot loader.
And of course, if you’re happy with NixOS and no longer need the old distribution:
sudo rm -rf /old-root -
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, nixos-infect uses the lustrate process to convert Digital Ocean droplets to NixOS from other distributions automatically.