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 13aafb0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
32 changes: 17 additions & 15 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ 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`.
makeOverridablePythonPackage =
f:
#
# Overridings specified through `overridePythonAttrs` will always be applied
# before those specified by `overrideAttrs`, even if invoked after them.
makeOverridablePythonPackage = f:
let
mirrorArgs = lib.mirrorFunctionArgs f;
in
Expand All @@ -31,19 +33,19 @@ let
);
result = f args;

overrideWith = newArgs: args // lib.toFunction newArgs args;
overridePythonAttrs = mirrorArgs (newArgs: makeOverridablePythonPackage f (overrideWith newArgs));
in
if builtins.isAttrs result then
result
else if builtins.isFunction result then
{
inherit overridePythonAttrs;
__functor = self: result;
}
else
result
);
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;
__functor = self: result;
}
else result);

mkPythonDerivation =
if python.isPy3k then ./mk-python-derivation.nix else ./python2/mk-python-derivation.nix;
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 13aafb0

Please sign in to comment.