5.1 The build dance: from ./configure to your first mkDerivation
Lesson 30 of 68 · 9:07
Video chapters
-
0:00 From builtins.derivation to mkDerivation
-
1:35 Typical software build phases
-
4:10 Building Hello with mkDerivation
-
7:10 Installing and using the Nix package
Transcript
From builtins.derivation to mkDerivation
0:00 After we've seen how to use built-ins derivation to build a package, we've also seen that this is a lot of work and we couldn't imagine using that like in real life projects, because you have to define everything and everything by yourself. However, this was useful to learn how the sandbox works from inside. Now we're going to use built-ins mkDerivation to learn how packages are really built in Nix packages. The whole idea is that this is abstraction built on top of built-ins derivation, right? So we didn't learn something that we don't need. The thing is that mkDerivation builds on top of built-ins derivation, but gives us a lot of comfort, overridable hooks, and stuff like that, which we'll learn in this and in the rest of the class too. And later we will use the even higher level things, buildGoModule, buildRustPackage, buildPythonPackage, and so on. They are all very similar. But whenever something doesn't work, they all practically use the mechanism for mkDerivation. So it's super useful to know how that works. Also, whenever there's something or there's not something for your specializations that you like, you will always build on mkDerivation practically, I promise, because that contains the most basic abstractions and helpers that you practically use everywhere. Now, in general, when we build software, it's always the same.
1:24 You somehow obtain the source code, download it, unpack it, whatever, git clone it, or whatnot. But in the end, you had the situation where you didn't have the source code and then you have it. And then when you're inside the folder, you need to configure. That means
Typical software build phases
1:40 that very often you have a build system that says, okay, let's look where the compiler is, let's look where the system libraries are, and so on and so on, because they are very often on slightly different paths on every system. And then it kind of discovers that and generates all the build files. After that, we can run make. Make will simply build the software. If you use GNU Make, of course, in a Rust project, that would be cargo build, and so on and so on. And there will be some commands to run the unit tests, make test, make check, whatever, cargo check or cargo test. You can imagine that every build system has something like that, and they are called very similar. And then you can finally install the package. If it's good and you compiled it, let's put it on the system.
The full transcript comes with the lesson.
About this lesson
Almost every open source package installs the same way: download, unpack, ./configure, make, make install. In this lesson we build GNU hello by hand first, then let stdenv.mkDerivation do the whole ritual from a few lines of Nix. You see why the classic build dance maps directly onto Nix build phases, and why mkDerivation is the function behind nearly every package in nixpkgs.
In this video
- The manual build dance:
wget,tar,./configure,make,make check, and whymake installneedssudoon a normal system - What the configure step actually discovers about your machine, and how these steps got names like unpack phase and build phase
- Building the same package with a
mkDerivationcall that fits in a handful of lines, with no compiler installed in your shell - Where the finished package lands instead of
/usr/local/bin, and installing and removing it withnix profile - Why
buildGoModule,buildRustPackage, andbuildPythonPackageall stand onmkDerivation, so this one function pays off everywhere
After this lesson
You can turn a standard configure-and-make project into a real Nix package with a few lines of Nix, and you know the phase names that the rest of this section builds on.
Builds on: The derivation is a recipe (4.3)