Patching dependencies

With uv2nix, you can apply patches to your Python dependencies.

Use this superpower judiciously: if you're building a Python library, you probably don't want to apply patches to your dependencies that users of your library would also somehow have to apply.

Applying a patch

This overlay applies a patch to the arpeggio library.

arpeggio.patch:

--- /dev/null
+++ b/arpeggio/patched.py
@@ -0,0 +1 @@
+print("You have patched arpeggio. Congratulations!")

Note how the overlay forces us to build from sdist, which requires specifying the build system. See Overriding build systems for more details.

final: prev: {
  arpeggio =
    (prev.arpeggio.override {
      # We build from sdist (not a wheel) to apply a patch to the source
      # code.
      # Alternatively, if you're using a wheel, you could apply patches to the
      # Python code in `postInstall`/`postFixup`, but YMMV.
      sourcePreference = "sdist";
    }).overrideAttrs
      (old: {
        patches = (old.patches or [ ]) ++ [
          ./arpeggio.patch
        ];

        nativeBuildInputs = old.nativeBuildInputs ++ [
          (final.resolveBuildSystem {
            setuptools = [ ];
          })
        ];
      });
}