9.4 Starting a flake from scratch with nix flake init
Lesson 62 of 68 · 18:49
Video chapters
-
0:00 Starting a Flake from Scratch
-
2:06 Understanding the Default Flake Template
-
6:00 Mapping Packages Across Systems
-
9:13 Adding Dev Shells and Simple Overrides
-
13:10 Using Overlays for More Flexible Outputs
-
15:44 Custom Flake Template with Per-System Imports
Transcript
Starting a Flake from Scratch
0:00 Okay, so we have had a look at how Flakes generally look like, but let's just start with our own. We will start from scratch with an empty folder and for that we can practically just use the command nix flake init. nix flake init will create a new Flake out of nothing and then we can start filling it up with our own stuff. Of course Flakes already start to be somewhat complicated because we need to define for each architecture what packages exist, so there's some minimal scaffolding to be done that we never had to deal with when we were not using Flakes, so that's where Flakes get more complicated.
0:39 So we will first have a look at this template and then have a look at this other template, which is a bit more complicated, but also more powerful in some regard. So let's start with the first one and yeah, we start an empty folder. We can just run nix flake init and then when we have a look at the flake.nix file which was just created for us, I mean it's just this one file right, we can see this is the very example that we had on the slide.
1:10 What we do is we have some inputs here, which is of course just nixos-unstable. What's interesting here is you generally don't really want to depend on Nix packages master, so if you wrote it like this, you would get Nix packages master. This is fine and works, but Nix packages master is typically much newer from the commit state than the nixos-unstable branch, and everything that is in the nixos-unstable branch can be considered being cached already, so you generally want to depend on nixos-unstable.
The full transcript comes with the lesson.
About this lesson
Time to write your own flake. Starting in an empty folder, nix flake init generates a template, and this lesson explains every line of it, then grows it into something real: your own patched package, a dev shell for nix develop, and static and cross builds. You also meet a second template that reimports nixpkgs, for the day you need your own config options or overlays.
In this video
- Why your flake inputs should point at
nixos-unstableinstead ofmaster(the answer is caching) - The
builtins.mapAttrspattern that covers all architectures withoutflake-utilsorflake-parts - Creating a new package with
overrideAttrs, then gettingpkgsStaticandpkgsCrossvariants almost for free with one small change - When the standard template is not enough: unfree packages, nixpkgs config, and the template that handles them
- The boilerplate problem that made everyone's flakes look different again, and the two styles worth sticking to
After this lesson
You can start a flake for a new project in minutes and pick the right template for it, without pulling in helper libraries.
Builds on: Anatomy of a flake: inputs, outputs and the lock file (9.2)