Wait, what is nix?

Nix is a programming language. A deceptively simple yet powerful programming language. It's a good one, too.

What kind of programming language?

  • Interpreted, not Compiled
    • You need to run a subcommand from the nix binary to make your nix code do anything useful. The nix binary has lots of subcommands, but nix eval, nix build, and nix run will take care of 90% of your needs until you get into really advanced stuff.
  • Functional & Declarative
    • Nix is heavily inspired by functional programming languages like Haskell. But because of the unique way functions are written in Nix, oftentimes an entire nix file might look a lot more like JSON than Haskell. This is a feature, not a bug.
  • Immutable & Strongly Typed
    • Once a variable is set, it cannot be changed. Instead, the value can be changed while reassigning it to another variable. Nix provides guarantees around data types that makes it easy to catch errors at eval time.

Oh, but I don't want to learn a new programming language.

Fair. But Nix is special because there's a good chance you already intuitively know how to use it.

Most of the work you'll do in Nix at first will look something like this.

{
	services.nginx.enable = true;
}

Try and tell me you don't understand that? 😉

Ok, but what was that, actually?

That, my new friend, was a Nix expression. If expression sounds like a word you remember from math class, you're right. An expression is just something that can be evaluated by nix eval to produce a new Nix expression (even if that new expression is equal to the old one).

Here's some more Nix expressions for you!

Behold:

1 + 1

BTW, you can use # to write comments that aren't evaluated by nix.

1 + 1 # evalutes to 2

You can save these Nix expressions in a file, and if you have installed Nix on your system, run nix eval --file complex-math.nix to see the result.

# complex-math.nix
2 * 2 # the result will shock you!

If saving a file seems like a lot of work, you could just run nix eval --expr '2 * 2' to have the same mind-blowing experience.

Comments

edit content/_comments.md to adjust for your own site/repo

remove the file to disable comments