Introduction to Nix

Nix is a hyper text markup language, and the origin of the *NIX philosophy "do one thing and do it well". It is also an operating system when you add "OS" to the end of the word.

Nix is a key tool in the journey to web scale because Nix gets you perfect reproducibility every time.

Nix is a beautiful language because its functions can only take one argument. This is referred to as "purity" in functional programming, and fledgling functional programmers often get rings to symbolize their commitment to one-parameter functions until marriage.

This purity combined with Python-like syntax leads to beautiful self-documenting code that anyone -- even non-programmers -- can understand. For instance, this is repo.nix from the other first WSSS post, Conversion to Scalability.

 1let
 2  nixpkgs = import <nixpkgs> {};
 3  allPkgs = nixpkgs // pkgs;
 4  callPackage = path: overrides:
 5    let f = import path;
 6    in f ((builtins.intersectAttrs (builtins.functionArgs f) allPkgs) // overrides);
 7  pkgs = with nixpkgs; {
 8    first = callPackage ./do-dir.nix { dir_name = ./source-static-site; };
 9  };
10in pkgs

I wrote the above on my own, and it's beautiful.

Despite our best effort, newcomers to the language may struggle to understand how Nix works. Thankfully experts like me have written tutorials and extensive documentation. The tutorials are undated and tell newcomers how to accomplish tasks in outdated ways, which is great because it teaches them the full history of the language's best practices. The documentation is generally written for people already familiar with all the jargon and tools. For instance, it provides the reader with snippets of code without instructions on what to do with them.

Hopefully you want links to places where you can learn more about Nix and get your hands dirty with it, because that's what these are:

Nix Pills (the first link) is probably the most coherent way to learn Nix. It guides you through reconstructing how the Nixpkgs repository works from first principles, which is great for deep understanding but not so much when you just want to Get Shit Done.

During your journey, if you figure out why the following things are true and beautifully inconsistent, let me know.

To get a nix-shell prompt, you use nix-shell:

1[pastly@home:~]$ nix-shell -p vim
2
3[nix-shell:~]$

To get a nix-repl prompt, you use nix repl:

1[pastly@home:~]$ nix repl
2Welcome to Nix 2.13.5. Type :? for help.
3
4nix-repl> 

To build a derivation, you use nix-build: nix-build repo.nix -A foo

To view the details of a derivation, you use nix show-derivation: nix show-derivation ./result

When you're ready to continue reading about my WSSS project, head over to the second first post.

Matt Traudt (pastly)

Tech, Pets, and Vettes


2023-10-09