How to Nixify most of the Linux systems
Or how to install NixOS in different ways
Due to the fact that I keep NixOS on most of my servers, I sometimes have to install it or reinstall it, and in general, this process takes some time and effort. Unfortunately, unlike more popular distributions like Ubuntu or Debian, which are pretty easy to install - NixOS can be difficult to install as it doesn’t come as one of the supported ones on servers, cloud providers and physical machines. On some of them, you can’t even put your own .iso file, and that means that you need to install NixOS on top of already functioning Ubuntu or Debian.
For example, Ubuntu or Debian can be turned into NixOS in different ways. There are two main approaches, at least those that I’m faimilar with.
Those are NixOS Infect, the older project, and the newer one, called NixOS Anywhere.
NixOS Infect
Talking about NixOS infect
- it’s a bash script, and it is many years old, I used it back in 2018-2019.
It does not change the structure of the file system, it does not change the kernel, it backups the existing distribution data, and you have everything that was on the system in /old-root
.
NixOS Anywhere
NixOS Anywhere, unlike NixOS Infect, is a new way to turn the system into NixOS, which uses the Linux kexec
.
It allows you to run another operating system on top of the current operating system in RAM. That is, in fact, the second full-fledged Linux is running in RAM, with its own kernel, with its own root, which, like a live USB, can be used to reinstall the system that ’s under the hood. And then, after the installation and a reboot, this system becomes NixOS.
The advantage of this approach is that, first of all, you can reformat disks, change any file systems, add encryption, make RAID, and many other things. In addition, this allows you to deploy the prebuilt NixOS image, that is, not to modify the existing system gradually until it meets the requirements - but to deploy a whole production-ready NixOS right on top of the operating system, which theoretically could allow you to deploy several similar systems at once in quick succession.
Conclusion
And, in general, it allows you to modify, configure and change a lot more on the fly. Of course, I prefer and have been using NixOS Anywhere for a long time, but due to the fact that it’s a more complicated process, it is not always suitable or desired. Also, various systems that do not have an iPMI or KVM, can be problematic to connect to and debug in case the installation goes wrong - for example, if the parameters of the kernel were incorrectly selected, or disk formatting went wrong, or there simply was a mistake in configuration. As for NixOS Infect, everything is more or less simple, despite the fact that the system can get stuck in the middle state. In general, according to my experience, it usually works pretty well - and can be easier to try multiple times in quick succession.
So, I have a little practice with both tools, and, oddly enough, sometimes even combine them. That is, first I try NixOS Anywhere, if I understand that it does not want to work for some reason, then I start first with the help of NixOS Infect, and then on top of it with the help of NixOS Anywhere format the disks, change the kernel and do everything else. And, oddly enough, it works better on top of NixOS than on top of other OS sometimes.
Caveats
NixOS Anywhere can be tricky to configure properly, partly because of complexity of the setup and configuring Disko, which is used for formatting the drives, in addition to the system itself, and also because it requires at least 1GB of real RAM, excluding swap - so for a VPS with 512MB it won’t work. Also, with NixOS Infect - I’d recommend running it in Digital Ocean mode, as it preserves the network settings from the previous system - for me it was often the key to get network connectivity after the reboot.
curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | PROVIDER=digitalocean NIX_CHANNEL=nixos-23.05 bash 2>&1 | tee /tmp/infect.log
Also, I prefer to leave the channel as is (23.05) - newer ones can have compatibility issues.