From ca6c0900829c0612cc949ee20f1ec313f6b0a3c9 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sat, 28 Oct 2023 01:56:02 +0000 Subject: [PATCH] makeOverridablePythonPackage: take care of overrideAttrs Make it possible to mix overridePythonAttrs and overrideAttrs, i.e. ((.overrideAttrs (_: { foo = "a"; })).overridePythonAttrs (_: { })).foo now works Co-authored-by: Matt Sturgeon --- .../python/python-packages-base.nix | 5 +++ pkgs/test/overriding.nix | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/pkgs/development/interpreters/python/python-packages-base.nix b/pkgs/development/interpreters/python/python-packages-base.nix index 22c829bf736c..97ec3f5076f2 100644 --- a/pkgs/development/interpreters/python/python-packages-base.nix +++ b/pkgs/development/interpreters/python/python-packages-base.nix @@ -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: lib.mirrorFunctionArgs f ( @@ -26,6 +29,8 @@ let result // { overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs); + overrideAttrs = + newArgs: makeOverridablePythonPackage (args: (f args).overrideAttrs newArgs) origArgs; } else result diff --git a/pkgs/test/overriding.nix b/pkgs/test/overriding.nix index 16ad0ec909c9..a41400d0d6c1 100644 --- a/pkgs/test/overriding.nix +++ b/pkgs/test/overriding.nix @@ -407,6 +407,14 @@ let p.overridePythonAttrs (previousAttrs: { overridePythonAttrsFlag = previousAttrs.overridePythonAttrsFlag or 0 + 1; }); + overrideAttrsFooBar = + drv: + drv.overrideAttrs ( + finalAttrs: previousAttrs: { + FOO = "a"; + BAR = finalAttrs.FOO; + } + ); in { overridePythonAttrs = { @@ -418,6 +426,37 @@ let (applyOverridePythonAttrs (applyOverridePythonAttrs python-package-stub)).overridePythonAttrsFlag; expected = 2; }; + overrideAttrs-overridePythonAttrs-test-overrideAttrs = { + expr = { + inherit (applyOverridePythonAttrs (overrideAttrsFooBar python-package-stub)) + FOO + BAR + ; + }; + expected = { + FOO = "a"; + BAR = "a"; + }; + }; + overrideAttrs-overridePythonAttrs-test-overridePythonAttrs = { + expr = + (applyOverridePythonAttrs (overrideAttrsFooBar python-package-stub)) ? overridePythonAttrsFlag; + expected = true; + }; + overrideAttrs-overridePythonAttrs-test-commutation = { + expr = overrideAttrsFooBar (applyOverridePythonAttrs python-package-stub); + expected = applyOverridePythonAttrs (overrideAttrsFooBar python-package-stub); + }; + chain-of-overrides = rec { + expr = lib.pipe python-package-stub [ + (p: p.overrideAttrs { inherit (expected) a; }) + (p: p.overridePythonAttrs { inherit (expected) b; }) + (p: p.overrideAttrs { inherit (expected) c; }) + (p: p.overridePythonAttrs { inherit (expected) d; }) + (builtins.intersectAttrs expected) + ]; + expected = lib.genAttrs [ "a" "b" "c" "d" ] lib.id; + }; }; in