Jump to content

Nixpkgs/Patching Nixpkgs: Difference between revisions

From Official NixOS Wiki
oops 2
N0099 (talk | contribs)
replace importing nixpkgs: https://discourse.nixos.org/t/1000-instances-of-nixpkgs/17347 with existing NixOS module param `pkgs`, and replace fetchurl with fetchpatch2 that will do normalization on generated patch
 
Line 12: Line 12:




First you will need to apply the Nixpkgs PR patch to some copy of Nixpkgs. In this example we will assume a Flake (<code>flake.nix</code>) that has an input <code>nixpkgs-unstable</code> pointing to <code>nixos-unstable</code>:
First you will need to apply the Nixpkgs PR patch to some copy of Nixpkgs. In this example we will assume a NixOS modules has parameter <code>pkgs</code> pointing to <code>nixos-unstable</code>.
 
<syntaxHighlight lang=nix>
inputs = {
  # ...
  nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
  # ...
};
</syntaxHighlight>
 
We will use the [https://noogle.dev/f/pkgs/applyPatches <code>applyPatches</code>] function to apply the unmerged PR changes to
We will use the [https://noogle.dev/f/pkgs/applyPatches <code>applyPatches</code>] function to apply the unmerged PR changes to
<code>nixpkgs-unstable</code>. First we need to covert the PR to a patch, which can be done by appending
<code>nixpkgs-unstable</code>. First we need to covert the PR to a patch, which can be done by appending
<code>.patch</code> to the github PR
<code>.patch</code> to the github PR
url. This will redirect to another link, for example https://github.com/NixOS/nixpkgs/pull/344917.patch will
url. This will redirect to another link, for example https://github.com/NixOS/nixpkgs/pull/344917.patch will
redirect to https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/344917.patch. We can use the latter.
redirect to https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/344917.patch. Then we can use <code>[https://nixos.org/manual/nixpkgs/stable/#fetchpatch fetchpatch]</code> or <code>[https://noogle.dev/f/pkgs/fetchpatch2 fetchpatch2]</code> to normalize this patch that generated by GitHub:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{ pkgs, ... }:
let
let
   nixpkgs-unstable' =
   nixpkgs-344917-drv =
    ((import nixpkgs-unstable {
     pkgs.applyPatches {
      system = "x86_64-linux";
       src = pkgs.path;
     }).applyPatches
      patches = [
       {
        (pkgs.fetchpatch2 {
        name = "nixpkgs-unstable-patched";
          url = "https://github.com/NixOS/nixpkgs/pull/344917.patch";
        src = inputs.nixpkgs-unstable;
          sha256 = "sha256-aws9J5ZNUyz4Z2RqPVEovBTNng4AdhzS03Bqg8jejWQ=";
        patches = [
        })
          (builtins.fetchurl {
      ];
            url = "https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/344917.patch";
    };
            sha256 = "sha256-aws9J5ZNUyz4Z2RqPVEovBTNng4AdhzS03Bqg8jejWQ=";
   nixpkgs-344917 = import nixpkgs-344917-drv { inherit (pkgs.stdenv) system; };
          })
in
        ];
      });
   lib = nixpkgs-unstable'.src.lib;
  pkgs-unstable-patched = import nixpkgs-unstable' {
    system = "x86_64-linux";
  };
# ...
# ...
</syntaxhighlight>
</syntaxhighlight>


In the above example we create a derivation with the patch applied, called <code>nixpkgs-unstable'</code>. Pay attention we take the <code>src</code> attribute to get back the top-level (unpatched) nixpkgs. We can then access <code>lib</code> functions normally. We then import that new
In the above example we create a derivation with the patch applied, called <code>nixpkgs-344917-drv</code>. Pay attention we take the <code>src</code> attribute to get back the original (unpatched) nixpkgs. We then import that new derivation which we assign to <code>nixpkgs-344917</code>. Now we can use <code>nixpkgs-344917</code> to access the
derivation which we assign to <code>pkgs-unstable-patched</code>. Now we can use <code>pkgs-unstable-patched</code> to access the
Ghidra 11.2 package, as well as any other packages normally available in nixpkgs.
Ghidra 11.2 package, as well as any other packages normally available in nixpkgs.



Latest revision as of 20:16, 13 January 2026

Sometimes it may be required to patch a copy of Nixpkgs directly, rather than use an overlay to patch an individual package. One scenario of where this might happen is if Nixpkgs doesn't contain a change you need, but you find some existing PR that has yet to be merged, and so want to leverage those changes prior to them being merged.

For the sake of example, let's say you are using the software package Ghidra, and the latest version available on Nixpkgs unstable is Ghidra 11.1.2. You know that the Ghidra developers have recently released 11.2, and want to use it before it's in Nixpkgs. You could try to create an overlay and manually update the package, but maybe in the process you realize it is not a trivial update. You may find there is already a pending PR to Nixpkgs with the updated version and changes you want, such as this PR introducing Ghidra 11.2. If you didn't want to wait for this PR to be merged into Nixpkgs, you could apply the PR patch directly to your Nixpkgs instead. Which we will do below.

There are of course other reasons you may wish to use a patched Nixpkgs and the method applies to any of those cases.


First you will need to apply the Nixpkgs PR patch to some copy of Nixpkgs. In this example we will assume a NixOS modules has parameter pkgs pointing to nixos-unstable. We will use the applyPatches function to apply the unmerged PR changes to nixpkgs-unstable. First we need to covert the PR to a patch, which can be done by appending .patch to the github PR url. This will redirect to another link, for example https://github.com/NixOS/nixpkgs/pull/344917.patch will redirect to https://patch-diff.githubusercontent.com/raw/NixOS/nixpkgs/pull/344917.patch. Then we can use fetchpatch or fetchpatch2 to normalize this patch that generated by GitHub:

{ pkgs, ... }:

let
  nixpkgs-344917-drv =
    pkgs.applyPatches {
      src = pkgs.path;
      patches = [
        (pkgs.fetchpatch2 {
          url = "https://github.com/NixOS/nixpkgs/pull/344917.patch";
          sha256 = "sha256-aws9J5ZNUyz4Z2RqPVEovBTNng4AdhzS03Bqg8jejWQ=";
        })
      ];
    };
  nixpkgs-344917 = import nixpkgs-344917-drv { inherit (pkgs.stdenv) system; };
in
# ...

In the above example we create a derivation with the patch applied, called nixpkgs-344917-drv. Pay attention we take the src attribute to get back the original (unpatched) nixpkgs. We then import that new derivation which we assign to nixpkgs-344917. Now we can use nixpkgs-344917 to access the Ghidra 11.2 package, as well as any other packages normally available in nixpkgs.

Resources

The following are resources that go into more depth on this topic.