Nixpkgs/Patching Nixpkgs: Difference between revisions

From NixOS Wiki
m Formatting
m nixpkgs to Nixpkgs
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>

Revision as of 00:45, 7 October 2024

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 Flake (flake.nix) that has an input nixpkgs-unstable pointing to nixos-unstable:

inputs = {
    # ...
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/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. We can use the latter.

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

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

  1. Patching <nixpkgs>