Nixpkgs/Patching Nixpkgs: Difference between revisions

m More indenting tweaks
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
 
(5 intermediate revisions by 3 users not shown)
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">
{ pkgs, ... }:


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


Line 58: Line 45:
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>]