Nixpkgs/Patching Nixpkgs: Difference between revisions

oops
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
 
(One intermediate revision by one other user 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">
<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
        ];
      }).src;
   lib = nixpkgs-unstable'.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.