How Nix programming language helps with NixOS distribution

How Nix programming language helps with NixOS distribution

While NixOS can be used as an everyday operating system, it mainly exists as a platform to showcase the powerful package manager Nix. The distribution is created with the Nix package manager using the description in the functional assembly language. The declarative configuration provides the ability to reliably update the system.

The NixOS distribution provides an easy way to distribute your machine configurations in the same way that GNU has already become a common way to share your configurations. A declarative description of the system conditions that everything you write on one machine will also behave on another. Nix is a purely functional programming language, and we have  a package manager with the same name, which is devoid of virtually all the shortcomings.

The distribution also allows you to distribute anything, such as domain settings to manage the base configuration layer for the company, standard browser plugins, and more. The main advantage of this approach is that it does not restrict the freedom of users working for the company. They do not have the feeling that someone else is controlling their work computer. Each of them can simply go to the updated configuration by changing the URL and continue working.

Here you also have the opportunity to use the configurations prepared by other users as a base for your own, so that there is no need to solve problems that someone has already solved.

Why is Nix good?

Nix provides the ability to roll forward and backward through snapshots of packages, which it calls generations, allowing an administrator to roll back changes or forward through available versions. So, NixOS is a fault-tolerant system since updates do not overwrite existing packages, but add new ones with switching links to them.

Since batch transactions are atomic, this means that updates and installations will not break the operating system in the event of a power failure. The administrator can simply revert to the original snapshot of the installed packages. It turns out that there are the old and current generations of the system, as it were, independent of each other and after the old ones are not in demand, they can be removed.

In terms of usage, for the most part, using Nix is similar to using most other command-line package managers. Probably one of the best things about Nix is that it has many more features than Haskell packages. Sometimes it is possible to use a package that has a more complex dependency, such as the C library. Unfortunately, there is no way to commit them so well in stack files. However, Nix can install these dependencies as easily as Haskell packages.

Nix is an advanced project management system. Among other things, NixOS also serves as an excellent example of how to work with the community, as pull requests are processed much faster and better than all distributions currently in existence. Even if you are not directly involved in the development, you will not face the same problems that you may encounter in Gentoo or Debian.

And it’s great that no third-party repo containing ebuild scripts for packages, that is, overlay, or their counterparts from other distributions are no longer necessary. You can submit your patches upstream right awayю

If let’s say, you need to accomplish a certain task using linear programming in Haskell the GNU compilation will be a useful tool for this task in this case, and if you want to interact with the GLPK suite through Haskell, you should probably use the glpk-hs library. If you create a new Stack project, you can add it as a dependency.

The usual solution to this problem would be to download the C library, compile it, and install it. But this is a tedious process. The more such dependencies you have, the more difficult it is for someone to use your library. They will have many more steps to set themselves up to use or contribute. And it can be difficult to reproduce all of these steps on a remote server. This can make continuous integration and deployment more painful.

One way to add system dependencies is to use librarySystemDepends. But this work flow forces you to use Nix for existing Haskell dependencies as well. For our little GLPK project to work, you need to create a complete nix file, glpk-hs, with both the Haskell dependency and glpk with the system dependency.

Declarative containers and virtual machine generation

Container virtualization is loved and used by many programmers because it allows one to run individual applications or entire operating systems using the host OS kernel. Containers in Nix make it possible to use the same declarations that were previously used for the host system.

It turns out that the only difference between such a container and the main system is another namespace in the configuration file. This means that you get a logically isolated environment. At the same time, containers in NixOS do not imply isolation to increase security.

As for virtual machines, using them within the framework of only the Nix package manager in other distributions and using Nix within NixOS, in this case, will not change anything. Bringing versatility both within the parent distribution and in the GNU/Linux ecosystem, in general, is another advantage that you can like Nix for.

You can see that NixOS greatly simplifies the base layer of the system in terms of configuration, and also allows you not to reinvent the wheel when using the same configuration on different machines, even when these are not just different distributions, but different operating systems.

So, is it worth studying Nix?

Nix’s approach to securing software transactions and making it easy to switch between installed versions of software makes it very attractive. Nix can work with any Linux filesystem, avoiding the overhead of extended filesystems, and works independently of both the distribution and any other package manager installed. This makes Nix a very attractive way to get access to more software and allows you to experiment with new packages with minimal risk.

Modern digital technology needs have created a huge demand for various programs for different platforms and operating systems that need to be used together. Package managers will remain in demand for many years to come, and it looks like the need for them will only grow, and Nix is one of the best. 

About The Author