The radical concept of NixOS and why I love it!
Tags: linux, learning, nixos
Published: 2024-06-12 03:47:35

NixOS is one of the most exciting developments in the Linux community in recent years. It is an independently developed Linux distribution based on the Nix package manger. It expands the concept of declarative and reproducible builds found in the Nix package manager to the entire system! Bringing unmatched stability to your system and finally put an end to the age old "Well it works on my system!".

![Well it works on my system meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oo2htoivor0gzb4t0zhl.jpg)

## The Nix Package Manager

Like many other distributions, the core of NixOS is it's package manager, which goes by the same name. It should be noted that you can use the Nix package manager with any distribution and is not limited to just NixOS. However it is far easier to use the Nix package manager with NixOS.

The Nix package manager takes a unique declarative approach to package management. By using this approach it allows nix to do a few really cool things including atomic updates and rollbacks.

Rather than running install commands like you would with other package managers like apt and pacman you declare all packages in your `configuration.nix` file. Then run an upgrade process.

When running the upgrade process Nix stores all packages in the `/nix/store` directory and symbolically links them to the rest of the system to the destination you would expect them to be. This allows you to have multiple versions or variants of a package installed at the same time without conflict. This is also the key to the rollback system.

All nix package manager operations are atomic and never overwrite data in the `/nix/store`. This allows you to rollback to any of your previous builds in the event of a failing update.

### Available packages

A side note about the Nix package manager that cannot be ignore is the massive amount of fresh packages found in the repository.

![Number of fresh packages in different package managers](https://repology.org/graph/map_repo_size_fresh.svg)

According to [Repology](https://repology.org/repositories/graphs) Nix package managers latest stable branch (24.05) has more **fresh** packages than any other package repo around. In fact about twice as many fresh packages as found in both the Arch repo and AUR combined!

## NixOS declarative system configuration

The biggest advantage of using NixOS, aside from offering the best support for the Nix package manager, is the declarative system configuration. NixOS takes the concept of a declarative configuration used by the Nix package manager and expands it to the entire system!

In a single configuration file `hardware.nix` you can define all of your system configuration including file system, mounting points, GPU configuration and anything else. You can even change your boot manager from Grub to Systemd boot with simply changing this single file.

The first version of the `hardware.nix` file is generated during install of NixOS with the help of a modified [Calamares](https://calamares.io/) installer, making it a very easy process and most likely will not have to be touched again.

But the beauty is that if you ever wanted to distro hop and switch back to nix, you simply need to include these 2 config files and the system would be completely restored.

## NixOS Home Manager

Home manager is not an official feature of NixOS but a tool that has been create to support the NixOS platform. it allows you to declarative configure your home directory.

One very powerful use is to make your VS Code configuration declarative and reproducible. Bellow I have included my personal VS Code config using home manager! 

``` nix
programs.vscode = {
    enable = true;
    userSettings = {
      "window.titleBarStyle" = "custom";
      "editor.tabSize" = "2";
			"editor.minimap.enabled" = false;
      "editor.rulers" = [80 120];
      "editor.fontFamily" = "'Source Code Pro', 'monospace', monospace";
      "workbench.colorTheme" = "Gruvbox Dark Medium";
			"git.enableSmartCommit" = true;
			"git.confirmSync" = false;
    };
		extensions = with pkgs.vscode-extensions; [
			jdinhlife.gruvbox
			vscode-icons-team.vscode-icons
			eamodio.gitlens
			donjayamanne.githistory
			mhutchie.git-graph
			esbenp.prettier-vscode
			dbaeumer.vscode-eslint
			ms-azuretools.vscode-docker
			irongeek.vscode-env
			yzhang.markdown-all-in-one
			bbenoist.nix
			bmewburn.vscode-intelephense-client
			ms-python.python
			ms-dotnettools.csharp
			ms-vscode.cpptools
			redhat.java
			redhat.vscode-yaml
			christian-kohler.path-intellisense
			golang.go
			bradlc.vscode-tailwindcss
			redhat.vscode-xml
		] ++ pkgs.vscode-utils.extensionsFromVscodeMarketplace [
      {
				name = "volar";
        publisher = "vue";
        version = "2.0.10";
        sha256 = "sha256-L5z7Rg8ybHTCGwO9HHExg0BfTBvO34Uz40NrNpzDgBk=";
      } {
				name = "astro-vscode";
        publisher = "astro-build";
        version = "2.8.6";
        sha256 = "sha256-sVLTOMdn+vDOpPGwTf0MJ+7tdQdJUVESdQ2HdmP0c1o=";
      }
		];
  };
```

## Flakes

Flakes are a new and experimental feature of NixOS but allow you to pull your configuration out of the `/nix` directory and sore it as you would your dot files in a single git repo in your home folder. This feature has been adopted by most of the NixOS community, and it often used along with home manager.

You can use flakes to store all of your configurations in a single git repo in your home directory, and after very build of your flake produce a completely version locked config file. This is similar to a `package-lock.json` file. it stores the exact commit hash of the package to be 100% and totally reproducible across any system.

## More resources

If you are interested in using NixOS yourself or configuring a flake I would highly recommend these tutorials.

- [NixOS Config Guides for Nerds and Other Cool People - LibrePhoenix](https://youtube.com/playlist?list=PL_WcXIXdDWWpuypAEKzZF2b5PijTluxRG&si=D-A2Wdu3DaTGZRqi)
- [Nix tutorials - Vimjoyer](https://youtube.com/playlist?list=PLko9chwSoP-15ZtZxu64k_CuTzXrFpxPE&si=rolFSmkmcuKiqcWg)