NixNixOSLinux

Nix & NixOS - First Impressions

Nix Logo
Author: Ariel Frischer|May 3, 2024|
7 min read

Introduction

I picked up NixOS out of curiosity to see what all the hype was about, and I'm blown away. As a software engineer who has tried too many Linux distributions, I was quite skeptical. I can confidently say now my distro search is over, NixOS is my final destination. So what is Nix/NixOS? The Nix terminology can be confusing to newcomers, so let's clear that up. This article will cover my first impressions, the Nix ecosystem, installation, usage examples, and the pros and cons of NixOS.

Nix* Ecosystem

Nix encompasses several distinct but interconnected components, each serving a unique purpose within the ecosystem:

  • Nix (The Package Manager)

Nix is a purely functional package manager meaning packages are built by functions, don’t have side-effects, and never change. Nix builds packages in isolation from each other and stores them in the Nix store in unique cryptographic hash subdirectories. This method allows multiple versions of a package to be installed simultaneously and prevents the rigmarole of conflicting software (Python v3.x, I'm looking at you). You don't need NixOS to run the Nix package manager; you can get started on macOS and Windows (WSL).

  • Nix (The Programming Language)

Nix is also a functional programming language, which means if you start with the same input, you are guaranteed to get the same output. This simple principle gives Nix its reproducibility anywhere and enables long build times to be skipped by using a cache system. If your build configuration input is identical to someone else's build input and that is in the cache, you don't need to build from source; you just pull the output from the cache.

  • NixOS (The Linux Distribution)

NixOS is a free, open-source Linux distribution that uses the Nix package manager to manage software in the system. NixOS is based on an immutable design and atomic update model and uses a declarative configuration system for reproducibility and portability.

  • Nixpkgs (The Package Repository)

Nixpkgs is a GitHub repository that is the largest package repository in the world. It allows the Nix package manager to install any package by name instead of referencing some obscure GitHub repo. It is the culmination of years of hard work meticulously building up a foundation of packages, making installation of any package in any language simple. An increasing number of contributors constantly create new package builds that keep Nix up to date.

Those are the main tools, but the ecosystem is vast. Check out the full wiki page here.

Installation of NixOS

I had a spare USB drive and turned it into a bootable drive with NixOS using the Rufus tool. For my default Windows laptop, I partitioned the drive into two for dual-booting. This gives me a partition to install Linux while keeping windows for gaming.

The installation process was straightforward. I booted into the USB and followed the installation instructions, which installed NixOS. There is a known issue/non-issue where it seems like the installer is stuck at 46% but that process just takes a while - there is a clickable log button that will show you the commands its running to reassure the installer is not stuck. The installer has many desktop environments. I chose KDE Plasma, but you can switch to whatever you want later with a simple line change in your configration.nix file. After installation, I rebooted, and it just worked out of the box.

Post-Installation Setup

I needed Firefox, so I added it to my configuration.nix and rebuilt with nixos-rebuild switch, and voila, it just existed in my start menu. That was my first "holy-cow" moment. I was used to spending a darn week installing all my programs, going through websites one by one, clicking next, next, next, agree, next, done. Previously, I would need to run multiple CLI commands which are all DIFFERENT depending on which package manager your distro is using and install them in the correct order because this program needs this program as a dependency. Nix saved me from that pain I was dreading.

So I went ham and added a bunch of common packages. It all worked on the first try. It felt stable somehow, and the icing on the cake was the BLAZINGLY fast build time. I couldn't really believe it was all working so smoothly; I was conditioned into random software breaking in Linux. After the first few days, I was quite satisfied with my system and thought that was it, but no, there is way more icing to this cake.

The Power of Nix

The distillation of all configuration in one file empowers users with simplicity, portability, and modularity. The NixOS approach streamlines system management, allowing for stable deployment on different machines. The modularity of Nix enables users to customize, extend, or reuse configurations with ease. Such a system not only simplifies the setup and maintenance of software but also significantly reduces the complexity associated with managing dependencies and system-specific variations.

Running an Old Windows Game

Recently, I wanted to revisit an old game I developed in 2012. To run this Windows-based executable on NixOS, I needed Wine, a Windows compatibility program. Using nix-shell -p wine, I quickly installed Wine CLI. Then ran my game with wine my_game.exe, and it just worked. When you exit nix-shell, the wine program is no longer available and did not clutter my user space. If you want to install anything permanently, you can; if you need to run a quick one-off, you can. Nix is clean and handles environment isolation brilliantly.

As a side note there is this incredible repository NixThePlanet which allows you to run "medieval" operating systems with a single nix command.

Stability and Reliability

NixOS introduces the concept of "generations," allowing you to roll back to previous versions of your system while booting. If anything breaks or is not running as intended, you can always revert to a previous generation. Surprisingly, my new ASUS laptop, manufactured with Windows in mind, actually crashes way more often than NixOS. I believe there is some issue with the Nvidia hybrid settings on Windows that causes constant crashes, giving the blue screen of death (BSOD).

Common Issues and Troubleshooting

While NixOS has many advantages, it is not without its challenges:

  • Learning Curve

There is a steep learning curve that involves learning the Nix language and all the details of the Nix ecosystem. As a software developer that has dabbled in Linux distros, I really would never recommend this to any Linux beginner. I would recommend it to software developers that really value stability and portability or any intermediate/advanced Linux users that want the same.

  • Documentation

Unfortunately, the documentation is not good enough, but as more folks start embracing Nix, it should gradually improve.

  • Limited Binary Compatibility

NixOS doesn’t use the traditional /bin, /sbin, /lib directory structure but instead uses its own store paths. This can lead to compatibility issues with binary packages that expect a traditional Linux filesystem layout. In my case this did cause issues with my Neovim configuration some Mason binaries did not work as usual but I was generally able to solve the issue quickly by installing the binaries through the nixpkgs repo instead - they were all available and easy to lookup.

  • Integration with Other Systems

Because of its unique approach, integrating Nix or NixOS with tools and platforms expecting a more conventional Linux environment can be more involved. Although there probably is a Nix way to get it running, it may become time-consuming. However, once it is set up, it should work forever. I hope as Nix continues to grow, we will begin to see more integration and simpler compatibility.

Conclusion: Embracing the Nix Philosophy

This is all just putting my feet in the water; there is much more to the Nix ecosystem that makes life simpler as a software engineer. If you're thinking about trying NixOS, I say just give it a shot. It will give you stability, easy reproducibility when adding a new machine, and peace of mind. The Nix philosophy has changed the way I view systems and software. This is how software should be.

Resources for Learning Nix