NixOS and the case for declarative configuration


The entire system is managed by the package manager and with a program called nix-rebuild. As I already mentioned, the entire configuration of a NixOS system resides in the configuration.nix file, which is also humanly readable (Listing 2). With a call to nixos-rebuild test, you can test the build. nixos-rebuild switch switches the boot manager to the newly created system (Figure 2).

Listing 2



Figure 2: A system rebuild goes through all the dependencies and updates the boot configuration.

Again, the Nix tool goes through the system's dependencies (Figure 3) and installs new packages that are required for the configuration. It is thus sufficient, for example, to set the desktop system to XFCE, as shown in Listing 2. nix-rebuild then takes care of installing the XFCE packages, including dependencies.

Figure 3: NixOS resolves all dependencies when rebuilding the system.

Besides the described test version of the rebuild, NixOS even offers the option of creating a virtual machine on the new system. A call to nixos-rebuild build-vm creates both the new system and a script named ./result/bin/run-nixos-vm; the script generates an image and starts it with the Qemu emulator (which relies on KVM – if it is in place).

To change a service configuration, first take a look at some existing Nix definitions that determine what parameters are available. You will find these existing definitions with the other Nix settings below /etc/nix. The options for the SSH daemon reside in /etc/nixos/nixpkgs/nixos/modules/services/networking/ssh/sshd.nix. For example, you will find the setting for the root login, as used in Listing 2, and the startup file for systemd.

Nix Files

Instead of using prebuilt programs and daemons, you can, of course, put together your own packages with your own software. To do so, you need to learn the configuration language in which the Nix package definitions are written. As already mentioned, Nix is oriented on functional programming principles; accordingly, you need to define a function for a package.

In the Nix language, a function is initiated with curly brackets and a colon: {stdenv, perl}:. The two strings in the curly brackets are the arguments for the function, which is defined after the colon; these strings represent the packages on which the package you are defining depends. In the example, this is the default package, which is the prerequisite for most Nix packages; the default is a standard Unix environment, including the GCC compiler, the Bash shell, basic tools such as cp, grep, tar, and so on. The standard way to build a package is by derivative from another package. The Stdenv package provides the mkDerivation function for derivations. You need to fill out some fields, for example, to specify how the program is compiled. If you omit an item, Nix uses the default settings for derivation. More information on writing Nix files is available in the Nix tools manual [2].


Perhaps the most interesting part of the Nix project builds on the components described so far, putting them in the application context for which the project's principles are best suited: virtualization and cloud computing. When it comes to providing a large number of virtual machines, a declarative definition of the configuration is a good idea. It's all the better if, as with NixOS, you can ensure the functionality of the VMs and integrate tests at the same time.

The basis for NixOps is the NixOps tool, which is very easy to install if you have used the Nix package manager. The easiest approach is to use NixOS, which lets you install NixOps is in a jiffy with a call to nix-env -i nixops. The description of a single VM, which will then run in VirtualBox, is shown in Listing 3. The example comes from the NixOps manual [2] and demonstrates how little information is needed for a virtual machine. It just takes a few lines to launch a web server and set the document root to a particular directory – in this case, the Valgrind documentation. Another file, shown in Listing 4, sets the deployment variables, that is, using VirtualBox as the target environment and defining the RAM size.

Listing 4



Listing 3



The following call finally generates a virtual machine named test from the descriptions:

nixops create ./trivial.nix ./trivial-vbox.nix -d test

Inspecting the state of the new VM with:

nixops info -d test

shows that it currently has a status of New. At this point, nothing has happened; there is no VM: what you have basically done is to reserve the VM. You need to run the nixops deploy -d test to deploy the system; the command looks for the necessary files, assembles the VM, and launches it on the deployment platform (VirtualBox, in this case). As the NixOps manual shows, you can use the same approach to build multiple machine setups, for example, for load balancers.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • NixOS 22.5 Is Now Available

    The latest release of NixOS with a much-improved package manager and a user-friendly graphical installer.

  • NixOS 21.11 Now Available for Download

    NixOS “Porcupine” has been made available for installation and includes numerous improvements.

  • Nix and NixOS

    NixOS and the Nix package manager offer a promising new approach to the challenge of managing packages in Linux.

  • Next Gen Distributions

    With systemd poised to revolutionize the init process, we look at Linux distributions that have left the familiar path, use new approaches and techniques, and are paving the way for the next generation of Linux distros.

  • News

    In the news: EndeavorOS 21.4 Has Arrived; NixOS 21.11 Now Available for Download; KDE Plasma Developers Introduce a Gnome-Like Overview; Rocky Linux 8.5 Now Available with Secure Boot Support; CronRAT Malware Targets Linux Servers; AlmaLinux OS 8.5 Now Available

comments powered by Disqus