Add Patches to Nixpkgs

Posted on November 26, 2023
Tags: nix

edit 2024-06-15: fix snippet

Following the trail from discourse to a closed GitHub pull request to a comment in a second GitHub pull request :) I’m happy to share that we can now easily apply patches to nixpkgs itself.

This is pretty neat because now, if you want to apply an open pull request immediately to your project, you can easily do it. Well, the code is not particularly elegant but it’s easier than cloning nixpkgs and merging the PR yourself…

This is what applying a patch looks like now:

let
  system = "x86_64-linux";
  originPkgs = nixpkgs.legacyPackages.${system};

  patches = [
    (originPkgs.fetchpatch {
      url = "https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/315018.patch";
      hash = "sha256-8jcGyO/d+htfv/ZajxXh89S3OiDZAr7/fsWC1JpGczM=";
    })
  ];
  patchedNixpkgs = originPkgs.applyPatches {
    name = "nixpkgs-patched";
    src = nixpkgs;
    inherit patches;
  };
in
  {
    nixpkgs = import patchednixpkgs { inherit system; };
  };

On GitHub, to get a patch from a PR, you must go to the PR (for example https://github.com/NixOS/nixpkgs/pull/268168), edit the URL to add a .patch suffix (https://github.com/NixOS/nixpkgs/pull/268168.patch) and that will redirect you to the final URL (the one in the snippet above for this example).