Nix 101 Course / Section 2: The Nix language
Premium

2.3 Nix syntax: the complete tour

Lesson 10 of 68 · 40:38

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 Nix syntax overview
  2. 2:12 Functions and partial application
  3. 8:36 Pattern matching and let bindings
  4. 18:00 With scopes, strings, and set merging
  5. 31:00 Importing files and file-based expressions
Transcript

Nix syntax overview

0:00 All right. So after we set up our editor and had a look at the Nix REPL to see how it helps us playing around with Nix expressions and so on, let's finally have a look at the actual language and syntax and so on. So in this course, we were, or in this video, we will look at the most important things, things that you should use. I'll also showcase a few things that you shouldn't use, but you will see them in the wild anyway. So we will be equipped with understanding all of it and all the ridiculousities like old things or special syntax. Let's ignore that for now, just to get productive and working. Okay. First of all, let's have a look at a few examples. We have already seen how the REPL looks like when we play with that. In general, when you launch the Nix REPL, you can say X equals one and S equals something, something, and then it defines a variable. You can play with that. And in Nix expressions in real files that we then use to build our packages in our systems, it will look similar, but different. But first of all, let's have a look at different things and characteristics here. So X equals one kind of assigns just a number. So that's easy. I think everyone will just get that. The next line is interesting. It defines an attribute set. An attribute set is basically the same thing like a dictionary in Python. And the side effect of

1:34 assigning it doesn't lead to any output. If you want to print it, you actually have to say S and then it will print that. That is also pretty similar to Python still. However, there's this inherit code word, which is used a lot. And in general, when you see inherit, you would think about object orientation, but forget what you knew about inheritance from object-oriented languages. What inherit actually does here is we create a new attribute set T. So we can see T is, of course, the recipient of the value here. And curly bracket, open, curly bracket, close. That

Functions and partial application

2:15 particularly tells us now, OK, there's an attribute set being constructed like here. But this one here is slightly different. We're not saying X equals something. We're saying inherit X. What that means is that we take the X out here. So this basically expects that there is a variable X already. And it puts it into the attribute set. And of course, X gets the value one when we look at the printout later, because it was of value one. So this is the same like writing X equals X. But better. The reason for that is why this is better is that imagine X would be like a very long variable name where you can add a typo. And then you say very long variable name equals very long variable name with a typo or something or the other way around. And then you're already not getting what you want. Inherit very long variable name semicolon basically places the variable with its content into the attribute set. And that's a very good thing to do. You also see this very often. So we're introducing it very early. There's also one complication of inherit, which is interesting. For example, here you're saying from the attribute S, which is up here, right? We take the members A and B. So we're not taking S into this attribute set. We're taking A and B, which has the effect of having A equals one, B equals one, and X equals one in this.

The full transcript comes with the lesson.

About this lesson

One video, the whole Nix syntax. This Nix language tutorial covers attribute sets, functions, let and in blocks, string interpolation and imports, plus the constructs you should recognize but avoid. After it, real world Nix code stops looking cryptic.

In this video

  • Attribute sets, inherit, the ? test and the or fallback operator (no, inherit has nothing to do with object orientation)
  • Functions, currying and partial application, explained side by side with Python lambdas
  • Attribute set parameters with default values, the ... ellipsis, and one @ pattern that is a genuine anti-pattern
  • Scoping with let and in, and the with construct with its variable shadowing trap
  • Multi-line strings and interpolation, merging sets with // and its surprise on nested sets, and importing Nix files

After this lesson

You can read almost any Nix expression in the wild, and you know which constructs are good style and which ones will hurt the next reader.