NixOS and the case for declarative configuration
Rebuild
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
configuration.nix
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.
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].
NixOps
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
trivial-vbox.nix
Listing 3
trivial.nix
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.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs