Software Technology

What is NixOS?

Written by prodigitalweb

Nix uses binary caching. So, it offers a unique compromise between the binary-oriented & source-oriented approach. Debian & other distributions use the binary-oriented approach. At the same time, the other approach is used by distributions like Gentoo. It is possible to use binaries for standard components. At the same time, custom-built packages & modules are possible to be used automatically, if a pre-built binary is unavailable. Eelco Dolstra and Armijn Hemel built the operating system. It was released initially in 2003. This is community-developed. And the maintenance is done under the stewardship of the NixOS Foundation.

What is NixOS?

NixOS indicates an open-source Linux distribution which can be used for free. It is based on the functional Nix package manager. It is composed with the help of the modules & packages that are defined in the nixpkgs project. This OS uses an atomic update model. The use of a declarative configuration allows portability as well as reproducibility.

Features of NixOS:

  • Declarative Configuration Model:

Hence, the Nix package manager builds the OS from a description in the Nix language. It includes the kernel, applications, system packages, & configuration files. When you build a new configuration, it will not overwrite the old configurations.

It is required to write specifications of the functionality to configure a NixOS system. Users want this on their machines in a global configuration file. It is mainly located in /etc/nixos. Let’s follow this minimal configuration of a machine which runs an SSH daemon:

{

boot.loader.grub.device = “/dev/sda”;

fileSystems.”/”.device = “/dev/sda1”;

services.sshd.enable = true;

}

You can find here a documentation of the available parameters and options. Once you change the configuration file, you should use the nixos-rebuild switch command to update the system. It can do all the essential things like downloading & compiling packages and creating configuration files in order to apply the new configuration.

  • Reliable And Atomic Upgrades:

You should know that the Nix files are declarative. Therefore, these will be evaluated, it will create the same result, regardless of the packages or configuration files available on the system. This operating system follows a transactional approach to configuration management. It helps to make configuration changes like atomic upgrades. Suppose a power failure interrupts an upgrade to a new configuration. You can still find the system available in a consistent state. It is going to boot in the old or the new configuration.

  • Rollbacks:

Once you complete a system update, you may find the new configuration undesirable. If this is the case, you have to roll back this using a special command (nixos-rebuild switch –rollback). Each system configuration version shows up automatically in your system boot menu. Suppose the new configuration crashes or it is now not booting accurately. In that case, you can select an older version. As the Rollbacks are lightweight operations, these do not involve files being restored from copies.

  • Reproducible System Configurations:

Due to the declarative configuration of NixOS, it becomes simpler to reproduce a system configuration on another machine. You can create the same system configuration by copying the configuration file to the target machine & then running the system update command except the system parts, which the package manager didn’t manage. This configuration includes kernel, applications, system services, etc.

  • Source-based Model With Binary Cache:

The operating system, which uses the Nix build language, specifies the way packages are built from the source. It makes it simple to adapt the system to the user’s requirements. The pre-built binaries are downloaded automatically from a cache server by the package manager when these are available.

You are capable of disabling the binary cache & force building from the source. And to do so, you need to use –option substitute false as an argument. It can provide the flexibility of a package management model based on source.

  • Consistency:

The Nix package manager makes sure that the running system is consistent with the system’s logical specifications. It indicates that the system will recreate all essential packages which are required to be rebuilt. If you change the kernel, the package manager is going to ensure that the external kernel modules will be recreated. If a library is updated, every system package uses the new version.

Implementation:

This OS is based on the Nix package manager storing all packages in isolation from each other in the package store. A cryptographic hash of all input identifies installed packages for their build. If you change a package’s build instructions, it will modify its hash. As a result, you can get different packages which you can install in the package store. In order to manage the configuration files, it is also possible to use the system. It ensures that newer configurations do not overwrite the older ones.

The Filesystem Hierarchy Standard is not followed by the operating system. But, the only exception is — it is possible to create a /bin/sh symlink to the bash’s version in the Nix store. However, the OS comes with an /etc directory that can be used to keep system-wide configuration files. In most cases, the files available in the directory are symlinks to generated files in /nix/store like /nix/store/s2sjbl85xnrc18rl4fhn56irkxqxyk4p-sshd_config.

Installation of NixOS:

ISO Image:

It is essential to burn a CD image onto a blank CD/DVD disk in order to install NixOS on physical hardware. Or you can copy this onto a USB stick. You should also check out the manual to get installation instructions.

  • Graphical ISO Image:

Graphical ISO Image comes with the graphical NixOS installer & a Desktop Environment, along with several applications. The live CD enables you to get an impression of NixOS & the Nix package manager, without the need to install it. The image is available only in Plasma & GNOME variations. This available installer enables installing different desktops or installing without a desktop.

  • Minimal ISO Image:

Minimal ISO Image never contains a graphical user interface. So, this one is a lot smaller. It is essential to run the installer from the console. This one comes with several rescue tools.

VirtualBox:

VirtualBox comes with the Plasma Desktop & the VirtualBox guest additions. If you are willing to use this, you need to download the OVA file using the above button.

First, you need to open VirtualBox.

After that, you need to run the File → Import Appliance from the menu.

Next, you should select the downloaded OVA file previously.

Then, you should click on Import.

After that, you have to start the virtual machine. Logging in is possible as a user demo and password demo. If you wish to obtain a root shell, you have to run sudo -i in the terminal (konsole).

Amazon EC2:

You can deploy NixOS to Amazon EC2 using a pre-generated AMI image, which you choose.

Via AWS Management Console:

With the help of this console, you are able to create an instance. Hence, you only need to select the region and click on the Launch button.

Via Command Line:

Creating an instance from the command line is also possible. For example, if you are willing to make an instance in region eu-west-1 with the help of the EC2 API tools, you only need to run the following:

$ nix-shell -p ec2_api_tools

(nix-shell) $ ec2-run-instances

Usages:

Declarative Configuration:

The ability to declaratively configure the entire system is a distinguishing feature of this operating system. In this case, you have to specify a configuration file that helps to specify the whole system state, including the packages that you need to install as well as all the different system settings & options. The configuration file’s location will be usually at /etc/nixos/configuration. Nix. However, you can use the environment variable NIX_PATH to specify another location. Once you modify the configuration file, you need to run the nixos-rebuild switch to make the new configuration active. In this case, you should know that the switch is atomic and it can be rolled back when necessary. You are able to keep the configuration files under /etc/nixos in a version control system like Git if you want.

Imperative Operations:

Although you can configure the OS declaratively as much as possible, some domains still need imperative operations. User environment management and channel management are a few of them.

  • User Environments:

Along with declarative system configuration, users of this operating system can use the nix-env command, which allows them to install packages at the user level without the need of changing the system state.

  • Channels:

Nix packages & NixOS modules are split through Nix channels. These mechanisms let you distribute Nix expressions & the binary caches for them. With the help of these channels, you can determine the OS version you use. These can be categorized into stable & unstable channels and large & small channels. In most cases, users want to go with the stable/large channel, which is currently nixos-21.11.

Just like you install packages via nix-env, it will be possible to manage channels at the user level. This operating system uses the channels set for the root user with the purpose of updating the system-wide configuration. Channels Set for other users are capable of controlling only the user environment for that user. Are you willing to change the channel that the system-level configuration uses? If so, then you have to make sure that you run the correct nix-channel command as root. Remember, the updating channels are not going to cause a rebuild in itself. Are you willing to update channels & rebuild? Then, running the nixos-rebuild upgrade switch is possible in order to do both in a single step.

The Bottom Line:

In this article, we have covered almost all necessary details related to NixOS. We have discussed how to install & implement this operating system, and other details as well. Still, if you have any queries or doubts, do ask us via comments.

Frequently Asked Questions

  • What is NixOS used for?

With the help of the declarative configuration model of this operating system, it will be possible to reproduce a system configuration on another machine easily.

  • What is the difference between Nix and NixOS?

The design of Nix is done in order to configure the environment it is in. With Nix, it will be possible to define what needs to be in the environment. After that, Nix will generate that environment for the user in place. With NixOS, it will be possible to define the total operating system, and NixOS will help to get the OS into that state.

  • Why use Nix over Docker?

In this case, you should know that both of these are designed for totally different purposes. Docker, which is a deployment tool, provides a reproducible run-time environment. On the other hand, Nix which is a package management tool provides a reproducible build.

About the author

prodigitalweb