query
On this page

applyPatches

pkgs.applyPatches

Docs pulled from | This Revision | about 1 hour ago


Applies a list of patches to a source directory.

Examples

applyPatches usage example

# Patching nixpkgs:

applyPatches {
  src = pkgs.path;
  patches = [
    (pkgs.fetchpatch {
      url = "https://github.com/NixOS/nixpkgs/commit/1f770d20550a413e508e081ddc08464e9d08ba3d.patch";
      sha256 = "1nlzx171y3r3jbk0qhvnl711kmdk57jlq4na8f8bs8wz2pbffymr";
    })
  ];
}

Noogle detected

Implementation

The following is the current implementation of this function.

applyPatches =
    {
      src,
      name ?
        (
          if builtins.typeOf src == "path" then
            builtins.baseNameOf src
          else if builtins.isAttrs src && builtins.hasAttr "name" src then
            src.name
          else
            throw "applyPatches: please supply a `name` argument because a default name can only be computed when the `src` is a path or is an attribute set with a `name` attribute."
        )
        + "-patched",
      patches ? [ ],
      prePatch ? "",
      postPatch ? "",
      ...
    }@args:
    assert lib.assertMsg (
      !args ? meta
    ) "applyPatches will not merge 'meta', change it in 'src' instead";
    assert lib.assertMsg (
      !args ? passthru
    ) "applyPatches will not merge 'passthru', change it in 'src' instead";
    if patches == [ ] && prePatch == "" && postPatch == "" then
      src # nothing to do, so use original src to avoid additional drv
    else
      let
        keepAttrs = names: lib.filterAttrs (name: val: lib.elem name names);
        # enables tools like nix-update to determine what src attributes to replace
        extraPassthru = lib.optionalAttrs (lib.isAttrs src) (
          keepAttrs [
            "rev"
            "tag"
            "url"
            "outputHash"
            "outputHashAlgo"
          ] src
        );
      in
      stdenvNoCC.mkDerivation (
        {
          inherit
            name
            src
            patches
            prePatch
            postPatch
            ;
          preferLocalBuild = true;
          allowSubstitutes = false;
          phases = "unpackPhase patchPhase installPhase";
          installPhase = "cp -R ./ $out";
        }
        # Carry (and merge) information from the underlying `src` if present.
        // (optionalAttrs (src ? meta) {
          inherit (src) meta;
        })
        // (optionalAttrs (extraPassthru != { } || src ? passthru) {
          passthru = extraPassthru // src.passthru or { };
        })
        # Forward any additional arguments to the derivation
        // (removeAttrs args [
          "src"
          "name"
          "patches"
          "prePatch"
          "postPatch"
        ])
      );