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
-
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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.