Zachary W. Huang

Home Projects Blog Guides Resume

Nix

Disclaimer: This is as much to help me in my own understanding of Nix as it is for others, so take this with a grain of nixpills.

What is Nix?

Nix is a lot of things, all of which revolve around the Nix programming language. Nix is a pure, lazy, functional programming language with dynamic typing. Nix has a built-in function called derivation which creates unique derivations, composable build actions which can depend on other build actions (forming a dependency tree). This allows Nix to act as a reproducible build system, which is its main purpose. Language abstractions can then be used to build up to a high-level package manager. What if we also use Nix to describe every single part of our operating system? We get NixOS.

Why Nix?

One of the main use cases of Nix is for reproducible builds. However, I think the most important part of Nix when it comes to an average user is composability. Personally, my impression of Nix is that it is a language for composable configuration. Like any other programming language, it provides a small set of primitives (numbers, strings, booleans, etc) along with methods for abstraction (functions, sets). By building up layers of abstraction, we can write all of our build steps, configuration/options, etc, in the same uniform language.

And one more thing - each derivation is immutable. Derivations are identified by a unique hash (calculated from the derivations that it depends on). When we refer to a derivation, we use the hash - this means that there is absolutely no ambiguity when it comes to dependencies. In addition, Nix keeps all of its derivations in a store (located at /nix/store). Since derivations refer to other derivations by their hash, we can keep multiple versions of a library/program on our system without any conflict (since each piece of software knows exactly which derivations it relies on).

TLDR: Nix allows us to specify packages/dependencies at a high level while also making dependency hell impossible.

In Conclusion

Nix gives us:

  • no more dependency conflicts
  • reproducible builds
  • a consistent, language-agnostic build/config language

Even if you don’t deploy software for a living (like me), you can still take advantage of Nix. You can “nixify” your development environment, making it easy to install your editors/tools/dotfiles on any computer in a single step. And by using nixpkgs (a collection of nix derivations for all your favorite software), you can ensure that you won’t need to worry about dependency conflicts between your games, applications, editors, cli tools, etc (I’m looking at you, Steam).

However…

Nix is not, however, a silver bullet. Using Nix is not as easy as sudo apt install xxxx. The language is rather esoteric (to 99% of people), and the Nix way™ has a very steep learning curve. Considering most developers probably don’t even bother to learn more about git than a few commands (relevant XKCD), I doubt that Nix will be embraced by all in the future. But it can’t be denied that Nix offers a unique set of benefits that can’t be found in any other tool.

RSS icon github logo linkedin logo

Zachary W. Huang © 2021-2024