Skip to content

Commit

Permalink
makeOverridablePythonPackage: take care of overrideAttrs
Browse files Browse the repository at this point in the history
Make it possible to mix overridePythonAttrs and overrideAttrs, i.e.
((<pkg>.overrideAttrs (_: { foo = "a"; })).overridePythonAttrs (_: { })).foo now works
  • Loading branch information
ShamrockLee committed Dec 19, 2024
1 parent 5265cc5 commit d77a5ff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let

# Derivations built with `buildPythonPackage` can already be overridden with `override`, `overrideAttrs`, and `overrideDerivation`.
# This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`.
#
# Overridings specified through `overridePythonAttrs` will always be applied
# before those specified by `overrideAttrs`, even if invoked after them.
makeOverridablePythonPackage =
f:
let
Expand All @@ -33,9 +36,15 @@ let

overrideWith = newArgs: args // lib.toFunction newArgs args;
overridePythonAttrs = mirrorArgs (newArgs: makeOverridablePythonPackage f (overrideWith newArgs));

# Change the result of the function call by applying g to it
overrideResult = g: makeOverridablePythonPackage (mirrorArgs (args: g (f args))) origArgs;
in
if builtins.isAttrs result then
result
// lib.optionalAttrs (result ? overrideAttrs) {
overrideAttrs = fdrv: overrideResult (drv: drv.overrideAttrs fdrv);
}
else if builtins.isFunction result then
{
inherit overridePythonAttrs;
Expand Down
23 changes: 23 additions & 0 deletions pkgs/test/overriding.nix
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ let
dontWrapPythonPrograms = false;
});
checkOverridePythonAttrs = p: !lib.hasInfix "wrapPythonPrograms" p.postFixup;
overrideAttrsFooBar =
drv:
drv.overrideAttrs (
finalAttrs: previousAttrs: {
FOO = "a";
BAR = finalAttrs.FOO;
}
);
checkAttrsFooBar = drv: drv.FOO == "a" && drv.BAR == "a";
in
{
overridePythonAttrs = {
Expand All @@ -215,6 +224,20 @@ let
expr = revertOverridePythonAttrs (applyOverridePythonAttrs pip) == pip;
expected = true;
};
overrideAttrs-overridePythonAttrs-test-overrideAttrs = {
expr = checkAttrsFooBar (applyOverridePythonAttrs (overrideAttrsFooBar pip));
expected = true;
};
overrideAttrs-overridePythonAttrs-test-overridePythonAttrs = {
expr = checkOverridePythonAttrs (applyOverridePythonAttrs (overrideAttrsFooBar pip));
expected = true;
};
overrideAttrs-overridePythonAttrs-test-commutation = {
expr =
(applyOverridePythonAttrs (overrideAttrsFooBar pip))
== (overrideAttrsFooBar (applyOverridePythonAttrs pip));
expected = true;
};
};
in

Expand Down

0 comments on commit d77a5ff

Please sign in to comment.