Nix 101 Course / Section 3: Dev shells: the productivity unlock
Premium

3.5 nix develop and flake-based dev shells

Lesson 22 of 68 · 13:25

placeholder thumbnail

Sign in, then buy this course to watch

Free account · no credit card needed

Video chapters

  1. 0:00 From package to flake
  2. 2:06 Generating a basic flake
  3. 4:36 Supporting multiple systems
  4. 8:14 Default package and dev shell
  5. 11:24 Flake tradeoffs and next steps
Transcript

From package to flake

0:00 So now we have a nice project template with a building package that we can even just build without a development shell, so that is good for users. We have the development shell with additional packages, so now you know how it works creating a dev shell and a package, while all the details here will get more understanding for that, how to do that correctly in the following chapters after this chapter here. The other thing is that we know how to distinguish now between packages and inputs from, where packages simply add all the packages that you plainly want to have available, while inputs from looks into the package and then extracts its development environment, which is super useful.

0:48 This is actually the significance of this is huge. You can have multiple projects like a Python sub project, the C++ and the Rust sub project and get the super shell for all of them. However, in this video we want to transform this into a flake. We haven't used flakes so far until now. I wanted to demonstrate to you that this structure here is basically the same like a flake, but with less boilerplate code. So we'll just add the amount of boilerplate lines and make it flakified kind of. Flakes have different advantages and disadvantages, but so you know how to do both variants and then you later decide what works for you.

1:32 Let's start. So the general thing is that a flake file can be generated from a template. We can say nix flake init and then we don't have to write all the boilerplate code ourselves. Okay, now we generated that and that dropped. Close the shell so we see more. Flake.nix file into this folder here.

The full transcript comes with the lesson.

About this lesson

You have a working package and dev shell without flakes. This lesson turns the same tmux project into a flake.nix, so you see exactly what flakes add, what they cost in boilerplate, and when each style is the better fit. You end up entering the shell with nix develop instead of nix-shell, from a definition anyone can build straight from a Git URL.

In this video

  • Generating a starting point with nix flake init and reading its inputs and outputs
  • The per-architecture problem: why a naive flake only builds on one platform, and a template that generates outputs for every system you support
  • Moving the tmux package and dev shell into flake outputs, checked with nix flake show
  • Small quality of life details: the default package, build logs with -L, and a shell hook for environment variables
  • An honest look at the tradeoffs, including flake-utils and flake-parts, and why some companies with huge monorepos went back to plain Nix expressions

After this lesson

You can flakify an existing project, support Linux and macOS on multiple architectures from one file, and decide with clear criteria whether a project should use flakes at all.