Nixpkgs/Patching Nixpkgs: Difference between revisions
Provide an explanation about how to patch nixpkgs directly |
m More indenting tweaks |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Sometimes it may be required to patch | 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]. | |||
You know that the Ghidra developers have recently released 11.2, and want to use it before it's in | 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 | 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 | 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 | 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 | 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> | ||
Line 30: | Line 30: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
let | let | ||
pkgs-unstable' = | |||
(import nixpkgs-unstable { | (import nixpkgs-unstable { | ||
system = "x86_64-linux"; | |||
}).applyPatches | }).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"; | |||
}; | |||
# ... | # ... | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Line 52: | Line 52: | ||
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. | |||
== Resources == | == Resources == |
Latest revision as of 00:49, 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.