1.2 Patch any nixpkgs package without forking it
Lesson 2 of 3 · 6:05
Video chapters
-
0:00 Running Nix Packages with nix run
-
0:48 Patching hello in the Nix REPL
-
2:56 Exploring the Source with nix develop
Transcript
Running Nix Packages with nix run
0:00 Hey, in this video I want to show you how to quickly patch stuff in Nix, like any random application. We can, of course, download anything from the Nix packages, cache and repository kind of to just make it run, but if you want to patch anything that we're running ourselves, it's very easy. Let's have a look. So, we can generally run any application that is in Nix packages with this syntax like that. The Nix run command just says from which repository, which is kind of a code name here for some for the Nix packages git repository, and then the hashtag below that says we want to run hello, which then it runs immediately.
0:38 If you run this command for the first time, it might take a minute to download everything, but because of me running that before recording this video already, it's in my cache, right?
Patching hello in the Nix REPL
0:48 So, how do we change this binary now? Instead of saying hello comma world, I want this application to say hello Nix user. We can say, okay, let's go with nix repl. In the nix repl, we can say packages equals import Nix packages. In those angle brackets, this basically says get me the latest Nix packages from the Flake, which we just did above too. And now we can say packages dot hello, which gives us the package derivation to actually build it.
Sign in for the full transcript.
About this lesson
You need one line changed in a program you did not write. The usual answer is to fork the repository, patch it, build it, and keep the fork alive forever. In Nix it takes about a minute in a REPL, and what you end up with is a handful of lines you can paste into a real project later. This demo patches GNU hello so that it greets somebody else, and then shows how to get the source code of any package in nixpkgs.
In this video
- Running a program straight out of nixpkgs without installing it, then changing what it prints
- The two REPL commands that build a package and drop you into a shell where the result is already on your
PATH overrideAttrs: changing parts of somebody else's recipe without copying the recipe- Rewriting a string in the source before it compiles, with a tool that fails loudly instead of silently doing nothing when your pattern is not there
nix developplus one phase name: unpacking the source of any package into a folder you can read, grep and poke at
After this lesson
Patching a dependency stops being a project and turns into a small edit you can try before you commit to it. You also know where the line runs between an experiment in the REPL and the version that belongs in a repository.