5.7 Pinning nixpkgs: the same build on every machine
Lesson 36 of 68 · 18:53
Video chapters
-
0:00 Why Pin Nixpkgs
-
2:50 How Nixpkgs Is Resolved
-
6:00 Fixed-Output Derivations and Hashes
-
10:50 Pinning with Nixtamal
-
16:20 Flakes vs Custom Pinning Tools
Transcript
Why Pin Nixpkgs
0:00 Okay, so now we know how to use mkDerivation to build a derivation, and we have seen that it's actually not that much work if the packages like halfway standard conforming and make derivation can just find out what it needs to do, and if mkDerivation can't, in one of those phases we know what to write and everything, and so we are actually now able to build many packages. We will later learn a few more tricks to do this and also the higher level builders and stuff like that, so that is like the minimal amount of work for any given tech stack, but we somehow got Nix packages always from somewhere, so remember we practically used Nix packages from here, and Nix packages in those angle brackets means that we just get some version of Nix packages, right, it doesn't specify it further, and we are going to learn how to use that.
0:59 Now the first question you might have is, okay, yeah, so you didn't use Flakes, which do that, right, why did you move away from Flakes and then show how to do pinning while Flakes already do that? The thing is that we will use pinning in multiple ways, either to get Nix packages, but then also we will later use Fetchers, and they all use basically the same mechanisms, and we will also have a closer look at how Flakes do that later, but by only using and knowing Flakes you are missing out how the mechanisms work that are below that, that Nix practically hide from us, which is comfortable, but at the same time you are missing out how this actually works.
1:43 So when you reference Nix packages in the REPL, then this kind of boils down to some path of Nix packages, and that is like a real checkout, we can have a quick look at that. So when I open the REPL now and say to string NixPkgs, that's some source folder. Now when I look into that, you see that this is like a Nix expression collection with a default.nix file, so if I just run import and then NixPkgs, it actually opens the default Nix file.
The full transcript comes with the lesson.
About this lesson
Your mkDerivation call can be perfect and still build differently on every computer, because <nixpkgs> in angle brackets means some version of nixpkgs, not one specific version. This lesson shows how to pin nixpkgs to an exact commit so colleagues, CI, and caches all evaluate the same thing. On the way you meet fixed output derivations, the mechanism that lets a sealed off sandbox download from the internet safely.
In this video
- What
<nixpkgs>andNIX_PATHactually resolve to on your machine, and why committing angle brackets to production code is a trap - Pinning nixpkgs with
fetchTarball, a GitHub commit id, and a hash - Fixed output derivations: why this one kind of build is allowed to use the network, and what stops manipulated downloads from ever entering your store
- Pinning tools compared:
niv,npins, andnixtamal, including features that flakes do not offer - Hands-on: pinning the hello project so
nix buildgives everyone the identical result, with no extra tool needed at build time
After this lesson
You can pin a project to an exact nixpkgs revision and get identical builds and cache hits on every machine, and you understand the trust model that makes sandboxed downloads safe.
Builds on: Minimal mkDerivation (5.2)
Links: