8.4 Fixpoint recursion: how overlays really work
Lesson 55 of 68 · 16:39
Video chapters
-
0:00 Introduction to fixed-point recursion
-
1:14 Building self-referential attribute sets
-
3:10 Lazy evaluation and infinite recursion
-
6:00 Visualizing the fixed-point graph
-
10:03 How Nix detects recursion loops
Transcript
Introduction to fixed-point recursion
0:00 Okay, so before we really look into how overlays work, let's have a look at how fixed point recursion or fixed point functions work in Nix. So the thing here is that because overlays might appear like completely weird to you and fixed point recursion is the basic mechanism on which they work. Let's go for this first, because if we can de-alienify this here for you, then it will be much more comprehensible how overlays actually work.
0:31 So this here, the function called fix is actually defined like this in packages.lib.fix. And the definition is a little bit weird because we say, okay, we accept a parameter f, in that case it's just a function. So fix is a higher order function that accepts another function as a parameter. So far so good. What does it do? It applies function f on x and the result of this application is the variable x that we threw in there.
1:04 So this specific part of this function is weird because it feels like the snake bites its own tail.
The full transcript comes with the lesson.
About this lesson
lib.fix looks like the snake biting its own tail: a function applied to its own result. This lesson takes fixpoint recursion apart in the REPL and on the drawing board until nothing is left but pointers and thunks. It is the exact mechanism nixpkgs overlays are built on, and understanding it puts you in a fairly small group of Nix users.
In this video
- The three line definition of
fixand why it feels like time traveling at first - Building an attribute set where
yandzare computed fromself, step by step in the REPL - Breaking it on purpose: which self references cause infinite recursion, and why the untouched attributes keep working anyway
- How Nix detects infinite recursion instantly instead of spinning forever
- The reveal: no magic and no endless loops, just an elegant way to describe a directed acyclic graph in memory, and nixpkgs is one giant such graph
After this lesson
The final: prev: shape of overlays stops looking alien. You can predict which expressions evaluate, which recurse forever, and why lazy evaluation makes the whole thing cheap.
Builds on: Lazy evaluation (2.11)