Why I switched to NixOS (And why you should, too)

Hello! Recently, I've switched to NixOS, and oh boy it was one of the best things I've done. My system has never been so stable, while so freeing at the same time. I also have a lot more control over it: what's installed, where each config is, enabled services, daemons and so on. The nixpkgs repository is huge. And I do mean it: it's bigger than the AUR!

I will dive in why is it good, it's cons, and whether or not you should use it. First, i'll talk about what even is NixOS and how does it stand out from other Linux distros.

Before talking about NixOS, let me introduce you to nix. nix is a functional package manager. This means that it treats packages like values in a purely functional programming language (like Haskell). The packages aren't installed where you would expect (like /usr/bin); instead, they are installed in /nix/store. And each package has its own unique subdirectory. Take Firefox for example:

/nix/store/b6gvzjyb2pg0kjfwrjmg1vfhh54ad73z-firefox-33.1/

This hash (b6gvzjyb...) is the unique identifier that will capture all the dependencies of this package. Thanks to the hashing, you are allowed to have multiple versions of the same package at the same time, without having to deal with dependencies conflicting or breaking.

Also, due to the unique identifier, nix is able to remove unused packages using a garbage collector. This means your system always have only what you want!

NixOS takes the nix package manager to the fullest, applying it's philosophy to a whole operating system. This means your kernel, drivers, monitor resolution, apps and configuration are all described in a functional, declarative manner.

The system does not follow the FHS standards, meaning there is no /bin, /lib or /usr. Everything it contains is in the nix store.

The system configuration can be found at /etc/nixos/, where you'll find two files:

  • hardware-configuration.nix: Auto-generated by the operating system. You will probably never have to touch here (at least I hope so!)
  • configuration.nix: Where your system configuration actually lives. Here's where you tell nix what packages should be installed, what services or daemons should be running, what desktop environment it should use, etc.

Here's an example on how to install git:

# configuration.nix
{
    # ...
    environment.systemPackages = [pkgs.git];
}

...And that's it. If you ever want to uninstall it, you just have to remove the pkgs.git from the list. It's that simple!

But, saving the file won't install git by itself. This would be a nightmare. You have to rebuild your system. And I mean it literally. Running nixos-rebuild will rebuild your whole system, from the ground up, installing or uninstalling any package you added or removed from the configuration, stopping or starting services you've enabled, and so on.

This might be a bit worrying to you, but fear not! Thanks to the nix store, your system is fully backed up before you perform any rebuild. So if you ever, let's say, delete your video driver, you can just select your previous build from the boot manager. To rebuild your system, run:

nixos-rebuild switch

This will rebuild your system, and then change to it immediately (without needing to reboot your machine). You can also easily roll back to the previous configuration using the --rollback argument.

If you want to just see how your changes affect your system without creating a backup (almost like a "throaway" rebuild), you can simply run:

nixos-rebuild test

There are many other ways to rebuild your system, including building a VM, but I will not cover those here. Also, those two are probably the commands you'll run the most, anyway.

Well, that's a simple explanation of how nix and NixOS works. You will probably want to dive deeper, and for that I can't recommend Vimjoyer's channel enough. There, you'll find more about the nix language, how to create modules, how to organize them and everything.

Pros

First, let me funnel down to who I think NixOS suits best:

Developers

NixOS gives you such an amazing DX. It supports isolated, reproducible development environments, meaning that the argument "it works on my machine" actually means it works. You can think of this as Docker containers, but better! Also, you can use NixOS to declare a server structure, meaning that if you ever change architecture (maybe by switching hosts), you can have all your hosting settings and services up in seconds by copying the old machine configuration.nix file!

Enthusiasts

If you enjoy exploring stuff in tech, such as ricing, creating your own configs, or just tickling around in general, NixOS is a great fit! It's familiar enough to make you feel comfortable, while being different enough to keep things interesting and fun to mess with.

Cons

Now, I think NixOS is not a good option for you if you:

Just want things to work fast

If you just want to download VSCode and want it to work, or if you only use a browser, or even if you find that setting Neovim by yourself "too much work", then I think NixOS is not for you. Setting up NixOS is tedious and takes time. While you can just copy someone else's file to have a solid starting point, I believe that not making the system suit your own needs is not only a waste of it's potential and will only be a frustating experience if you ever want to change something.

Are new to Linux

If you're new to the Linux world, I think NixOS is too much to take. Go explore Ubuntu, Mint, or even Fedora. Then you can dive down into Arch, Void or NixOS. Get a feeling of how Linux works, how it's built to then be able to see how lower-level systems bends it to get the most out of it.

That's it! Maybe I'll make more posts sharing the stuff I find on the system. Meanwhile, you can find my current dotfiles here. See you next time!