Nixpkgs/Patching Nixpkgs: Difference between revisions

Provide an explanation about how to patch nixpkgs directly
 
m fix indent of example code
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
Sometimes it may be required to patch 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
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.
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
For the sake of example, let's say you are using the software package Ghidra, and the latest version available on
nixpkgs unstable is [https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=ghidra Ghidra 11.1.2].
Nixpkgs unstable is [https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=ghidra 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
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
trivial update. You may find there is already a pending PR to Nixpkgs with the updated version and
changes you want, such as [https://github.com/NixOS/nixpkgs/pull/344917 this] PR introducing Ghidra 11.2. If you didn't
changes you want, such as [https://github.com/NixOS/nixpkgs/pull/344917 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.
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.
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 <code>flake.nix</code> that has an input <code>nixpkgs-unstable</code> that is 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 Flake (<code>flake.nix</code>) that has an input <code>nixpkgs-unstable</code> pointing to <code>nixos-unstable</code>:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
inputs = {
inputs = {
    # ...
  # ...
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
  nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    # ...
  # ...
};
};
</syntaxHighlight>
</syntaxHighlight>
Line 29: Line 29:


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


In the above example we create a derivation with the patch applied, called <code>pkgs-unstable'</code>. We then import that new
In the above example we create a derivation with the patch applied, called <code>pkgs-unstable'</code>. We then import that new
derivation which we assign to <code>pkgs-unstable</code>. Now we can use <code>pkgs-unstable</code> to access the
derivation which we assign to <code>pkgs-unstable</code>. Now we can use <code>pkgs-unstable</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.


== Resources ==
== Resources ==
Line 58: Line 58:
The following are resources that go into more depth on this topic.
The following are resources that go into more depth on this topic.


# [https://ertt.ca/nix/patch-nixpkgs/ Patching <nixpkgs>]
* [https://ertt.ca/nix/patch-nixpkgs/ Patching <nixpkgs>]