Making patches for overlays?

I’m checking out the overlay page on the nixos wiki, and it refers to making overlays that apply a patch. It gives the example of modifying the package ‘sl’, by adding a ‘patches’ attribute:

final: prev:
{
  sl = prev.sl.overrideAttrs (old: {
    patches = (old.patches or []) ++ [
      (prev.fetchpatch {
        url = "https://github.com/charlieLehman/sl/commit/e20abbd7e1ee26af53f34451a8f7ad79b27a4c0a.patch";
        hash = "07sx98d422589gxr8wflfpkdd0k44kbagxl3b51i56ky2wfix7rc";
      })
      # alternatively if you have a local patch,
      /path/to/file.patch
      # or a relative path (relative to the current nix file)
      ./relative.patch
    ];
  });
}

How does one created a patch of the correct format for that? I’ve tried this before and had it not work. I normally never use patches so that’s unsurprising. How exactly does one make a patch for a package in nixpkgs? I’d probably opt for the relative patch.

My specific need right now is to apply an overlay so that the current yt-dlp package looks like the one from master.

You edit the sources, and then use git diff to get a unified diff, which you can then use as a patch for patches.

Ok, checked out branc nixpkgs-25.11, copied in the file from master and did

git diff -U > ytpatch

Which yields this file:

diff --git a/pkgs/by-name/yt/yt-dlp/package.nix b/pkgs/by-name/yt/yt-dlp/package.nix
index 5de9b20cc..9178e8c41 100644
--- a/pkgs/by-name/yt/yt-dlp/package.nix
+++ b/pkgs/by-name/yt/yt-dlp/package.nix
@@ -21,14 +21,14 @@ python3Packages.buildPythonApplication rec {
   # The websites yt-dlp deals with are a very moving target. That means that
   # downloads break constantly. Because of that, updates should always be backported
   # to the latest stable release.
-  version = "2026.01.29";
+  version = "2026.01.31";
   pyproject = true;

   src = fetchFromGitHub {
     owner = "yt-dlp";
     repo = "yt-dlp";
     tag = version;
-    hash = "sha256-nw/L71aoAJSCbW1y8ir8obrFPSbVlBA0UtlrxL6YtCQ=";
+    hash = "sha256-3sXXyWuQI6KTOQIkkOfJhCTBBh3Zkv59ENhkrz9Sgxc=";
   };

   postPatch = ''
@@ -51,7 +51,7 @@ python3Packages.buildPythonApplication rec {
   ];

   # expose optional-dependencies, but provide all features
-  dependencies = lib.flatten (lib.attrValues optional-dependencies);
+  dependencies = lib.concatAttrValues optional-dependencies;

   optional-dependencies = {
     default = with python3Packages; [
@@ -100,7 +100,10 @@ python3Packages.buildPythonApplication rec {
         ++ lib.optional rtmpSupport rtmpdump;
     in
     lib.optionals (packagesToBinPath != [ ]) [
-      ''--prefix PATH : "${lib.makeBinPath packagesToBinPath}"''
+      "--prefix"
+      "PATH"
+      ":"
+      ''"${lib.makeBinPath packagesToBinPath}"''
     ];

   checkPhase = ''

Copied that file over to the same dir has my configuration.nix, and added this yt-dlp dealio to this nixpkgs.overlays section:

  nixpkgs.overlays = [ (final: prev: {
    timeclonk = inputs.timeclonk.packages.${pkgs.system}.timeclonk;
    zknotes = inputs.zknotes.packages.${pkgs.system}.zknotes;
    yt-dlp = prev.yt-dlp.overrideAttrs (old: {
      patches = (old.patches or []) ++ [
        ./ytpatch
        ];
      });
  }) ];

now nixos-rebuild yields:

bburdette@HOSS:~/code/bburdette-nix-configs/hoss]$ sudo nixos-rebuild switch --impure
[sudo] password for bburdette:
warning: Git tree '/home/bburdette/code/bburdette-nix-configs' is dirty
building the system configuration...
warning: Git tree '/home/bburdette/code/bburdette-nix-configs' is dirty
evaluation warning: 'system' has been renamed to/replaced by 'stdenv.hostPlatform.system'
error: Cannot build '/nix/store/xa82741jr09blavx17p29vw1dp0pybyw-yt-dlp-2025.12.08.drv'.
       Reason: builder failed with exit code 1.
       Output paths:
         /nix/store/bx0s1fnwhs0zrsg3bgsy0xa4wwic28p4-yt-dlp-2025.12.08-dist
         /nix/store/fx1hkjna8q57b2g5rvwpbbdams72s4p6-yt-dlp-2025.12.08
       Last 25 log lines:
       > Sourcing pypa-install-hook
       > Using pypaInstallPhase
       > Sourcing python-imports-check-hook.sh
       > Using pythonImportsCheckPhase
       > Sourcing python-namespaces-hook
       > Sourcing python-catch-conflicts-hook.sh
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/3424yzpcnz2y3ifypgakm26ghgpyk56s-source
       > source root is source
       > setting SOURCE_DATE_EPOCH to timestamp 315619200 of file "source/yt_dlp/webvtt.py"
       > Running phase: patchPhase
       > applying patch /nix/store/3bwyvwkg5vf0spmjcq2j1xms6b9vx3k1-ytpatch
       > can't find file to patch at input line 5
       > Perhaps you used the wrong -p or --strip option?
       > The text leading up to this was:
       > --------------------------
       > |diff --git a/pkgs/by-name/yt/yt-dlp/package.nix b/pkgs/by-name/yt/yt-dlp/package.nix
       > |index 5de9b20cc..9178e8c41 100644
       > |--- a/pkgs/by-name/yt/yt-dlp/package.nix
       > |+++ b/pkgs/by-name/yt/yt-dlp/package.nix
       > --------------------------
       > File to patch:
       > Skip this patch? [y]
       > Skipping patch.
       > 3 out of 3 hunks ignored
       For full logs, run:
         nix log /nix/store/xa82741jr09blavx17p29vw1dp0pybyw-yt-dlp-2025.12.08.drv
error: Cannot build '/nix/store/q8jpfhccg81ka79ajw664k6qaqnvhn8f-unit-zknotes-onsave.service.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/bps632lr1k4a83cdzmmwrgy35mdqfgca-unit-zknotes-onsave.service
error: Cannot build '/nix/store/p1xb8dypsgb8fnrnxy3b6d5dsmqvv721-system-units.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/zaw59z7z8w4vq9ip7qzaswaffi6f892w-system-units
error: Cannot build '/nix/store/j3vkc5n5glkbx7jhv5j3fwlcsx6rvq5m-etc.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/2z33b2mnd9d87is9fk2i207h7p1x3vxq-etc
error: Cannot build '/nix/store/7scrar2ah3lp4z3dn71l6i7n485z1864-nixos-system-HOSS-25.11.20260128.fa83fd8.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/sg1kkdxvz2d4dmmgj3kyzkspvn3s6b2n-nixos-system-HOSS-25.11.20260128.fa83fd8
Command 'nix --extra-experimental-features 'nix-command flakes' build --print-out-paths '/home/bburdette/code/bburdette-nix-configs/hoss#nixosConfigurations."HOSS".config.system.build.toplevel' --no-link --impure' returned non-zero exit status 1.

patches is to patch the programs sourcecode. If you want to override the derivations src, then you override the derivations src, not patches. Though as far as I remember, overriding a python packages sources requires some other techniques that I have absolutely no insights into.

1 Like

If you’re trying to patch nixpkgs, the way to apply the patch would be using something like pkgs.applyPatches, or using your own custom fork/branch.

In this case I would just use .overridePythonAttrs (equivalent of .overrideAttrs but for python packages) and, yknow, override the attrs that you want to override, don’t use a patch.

1 Like

Ok well that explains a lot. I thought the patch the documentation was discussing was for the nix code in nixpkgs, but actually its trying to apply it to the yt-dlp code being fetched from github.

Good to know about .overridePythonAttrs. I got started on modifying the attrs with this but its rather a hassle compared to just diffing a file, IMO. Maybe next time, since I noticed that the yt-dlp version in 25.11 is newer than the version in unstable! I guess unstable isn’t always the latest.

Any any rate, yt-dlp is working again, crisis averted.

When a PR is applied to unstable and immediately backported to stable, as happens often with rapidly-moving targets like yt-dlp, it’s fairly common that the change hits stable before unstable, just because unstable often takes longer to make it through CI. It’s a bit counterintuitive, but that’s just how things come out.

1 Like