Best pattern to include multiples patches/PRs to nixpgs in flake.nix

Hello everyone!

It’s my first post here and I very recently on boarded Nix and NixOS, so please be patient if I miss something obvious.

What I am trying to achieve is the following:

Use a flake in a GitHub repo to declare the whole state and configuration of my infrastructure (currently 1 raspberry pi and 2 VPSs), with the ability to reconfigure or build disk images for each of the machine.

To automate partitioning and image creation I decided use “disko” from nix-community.

Everything is working flawlessly and I love that setup!

Now my problems arise from the fact that I couldn’t resist from the temptation to fork nixpkgs and make some changes to nixos core modules. Let’s say for instance that these two changes resulted in two PRs on github (branch feature1 and feature2 in my nixpkgs fork).

I would like to keep feature1 and feature2 rebased to nixpkgs master upstream, but at the same time use these features in my infrastructure (following nixos-unstable).

For the moment the only viable workflow I found is to periodically run the following:

  • Clone my nixpkgs fork locally
  • Rebase fork/feature1 and fork/feature2 to upstream/master and force push (to keep in sync for PRs)
  • Checkout upstream/nixos-unstable
  • Create and checkout branch fork/infra-production
  • Cherry-pick all commits between master…feature1 and between master…feature2
  • Force push fork/infra-production
  • Use fork/infra-production as nixpkgs input in my flake.nix

This seems incredibly inefficient and tedious! As an alternative, I considered creating a patches/ folder in my infra flake repositories, where to add a feature1.patch and feature2.patch file, then create a patched-nixpkgs derivation using the applyPatches function in flake.nix.

However, this approach runs into issues because my flake.nix has a second input, “disko”, whose input is set to follow the version of nixpkgs in my flake. It would continue to use the “unpatched” version, while I want to use the patched nixpkgs as input for the disko flake!

So here’s my question: Is there any way to completely “replace” nixpkgs with its patched version without creating a new nixpkgs branch? And if creating a new nixpkgs branch is really necessary, is there a more efficient way to do it (like using git shallow clones)?

Sorry for that long first post and huge thanks to the wonderful Nix community!

3 Likes

The problem is that patches are not derivations, and nixpkgs are derivations, so with flakes this is currently simply not possible. This is still an open issue at the moment: 3920

Also this kind of works as a temporary hack

Some months ago, I was also frustrated with this problem.

At the time, I made a tool that automates the fork/cherry-pick/push workflow. You just declare your patches as flake inputs, then it finds them and applies them to upstream nixpkgs, pushing the result to your own GitHub fork.

Since the patched nixpkgs fork is a proper repository, it avoids all of the applyPatches and IFD headaches.

It could help. Let me know if there’s problems! GitHub - katrinafyi/nix-patcher: a tool for patching Nix flake inputs, declaratively!

The trick to apply patches without checking them all out is this tool: GitHub - bluekeyes/patch2pr: Create pull requests from patches without cloning the repository

4 Likes