3.3 Deriving the dev shell from the package definition
Lesson 20 of 68 · 6:22
Video chapters
-
0:00 Why build from the package definition
-
1:10 Moving from dev shell to production build
-
2:34 Creating a derivation with mkDerivation
-
4:10 Running the build in Nix and CI
Transcript
Why build from the package definition
0:00 Okay, so we have our developer shell already, why change something now? I said earlier that Nix is kind of actually package description first and developer shell later. While this is pretty intuitive, we say, okay, we need this 15 packages, let's list them up, run the Nix shell, develop, it's all fine, how would you do that for production? On production, you would say, okay, CI would basically run the Nix shell too, and then run the commands.
0:32 But if it doesn't work, is it really the same thing? So very often, CIs are like, okay, you commit to push and hope that it works. And then two hours later into the build, something doesn't work. And you're like, so why, how, how is that different from my system now? Nix puts every package built into a sandbox, and it will be sandboxed just the same way on your system as on the CI. So I want to motivate you to look with me, how do we actually put the build into the sandboxed build process, and then derive the shell from that for free.
Moving from dev shell to production build
1:10 The advantage of this is that in general, we have with this Dev Shell first thinking with classical tools, you have the development shell and that helps the developer. But then when you want to deploy into production, you would have to build for production another time, and then very often the description of the dependencies and the build process might be different from what developers are doing day to day, and then what the DevOps are later deploying somewhere. So you have two organizations that have different opinion how to build the same piece of software.
The full transcript comes with the lesson.
About this lesson
Listing 15 packages in a shell.nix works, but it leaves you with two descriptions of your project: what developers use, and what CI and production actually build. This lesson flips the order. You describe the package build once, and Nix hands you the matching dev shell for free, so nix-shell and nix-build are guaranteed to agree.
In this video
- Why "works in my shell, fails in CI" happens, and how the build sandbox removes the guesswork
- Turning the
tmuxshell definition into a real package withstdenv.mkDerivation: the handful of attributes it needs - What
mkDerivationfigures out on its own about aconfigureandmakebased project, and the one step it cannot detect - The fallback trick that makes
nix-shellwork with noshell.nixin the repo at all - Running the exact same
nix-buildyour CI runs, before you commit anything
After this lesson
You can build a small autotools project as a Nix package and drop into its dev shell from the same file, with one single source of truth for developers, CI, and production.