Introduction to Nix(Os)
Nix is a declarative package manager invented by Eelco Dolstra. Nix comes already bundled with NixOS so I will mostly speak on it because that’s how I interact with the Nix package manager.
NOTE
It is possible to install Nix on other UNIX like distros.
How NixOS Works
NixOS is a declarative and immutable Linux distro based on the Nix package manager which uses the Nix programming language to declare its various configuration files. To help you understand NixOS I will first elaborate on the definition that I have given it.
Declarative
Unlike other Linux distros NixOS is declarative. What this means is that in other Linux distros you tell the operating system how to perform tasks for you. Let’s say you’re on Arch and want to install firefox. You would:
sudo pacman -S firefox
If firefox relies on external libraries you would also need to install them along side it e.g
sudo pacman -S libimportant1
sudo pacman -S libimportant2
NOTE
This is an example and I know pacman and other package managers can do automatic dependancy resolution
All of these modifications happen in place and mutate your system in a way that is hard to keep track of.
However on NixOS you just tell the system that you want firefox and it will do everything for you. The way you do this is through a file called configuration.nix which holds the entire configuration of your system. For example if you want to install firefox you would enable it in the configuration file like this:
{
programs.firefox.enable = true;
}
The operating system will then do whatever is necessary to ensure that firefox is installed on your system including installing its dependencies. The benefit of this approach as compared to the first one is that your entire system is reproducible. You just copy the configuration file to a new machine that also has NixOS on it and bingo bango you have the same programs and same utilities. Another underrated benefit of using configuration files is leaving a trail. You can easily add this file to version control and therefore can track changes that you make to your system. Don’t like a change you made, just rollback your system to the point you like and just move on with life.
Immutable
NixOS is also immutable. This means that files and programs created through the configuration cannot be overridden or deleted. The way this works is NixOS stores everything in the nix store. This is a special directory on your machine which is managed by Nix. When you install or upgrade your system, NixOS does not delete the previous versions of the programs. What it does is it keeps the old versions and makes the new versions the default. This allows for easy rollbacks and also ensures that libraries and other dependencies aren’t accidentally deleted or made incompatible due to upgrades. The easy way to think about it is that the configuration file declares a state of the world. Every time you add or remove something instead of modifying the state of the world, NixOS gives you a new world where the modification exists. So now the two worlds exist in parallel in your nix store. These worlds known as generations. This happens automagically so there is no need to worry about it. You can also switch between the different generations at startup by choosing them in the bootloader menu.