From 1653d671aad93624ec9f8a930b0bd67c2f9fc2d1 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Thu, 19 Oct 2023 06:20:06 +0800 Subject: [PATCH 1/3] tests.overriding: modularize Python tests Test with a pseudopackage python-package-stub. Modularize the transforming function and testing function. --- pkgs/test/overriding.nix | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/pkgs/test/overriding.nix b/pkgs/test/overriding.nix index 3daa01b6247de..16ad0ec909c95 100644 --- a/pkgs/test/overriding.nix +++ b/pkgs/test/overriding.nix @@ -390,14 +390,33 @@ let tests-python = let - p = pkgs.python3Packages.xpybutil.overridePythonAttrs (_: { - dontWrapPythonPrograms = true; - }); + python-package-stub = pkgs.python3Packages.callPackage ( + { + buildPythonPackage, + emptyDirectory, + }: + buildPythonPackage { + pname = "python-package-stub"; + version = "0.1.0"; + pyproject = true; + src = emptyDirectory; + } + ) { }; + applyOverridePythonAttrs = + p: + p.overridePythonAttrs (previousAttrs: { + overridePythonAttrsFlag = previousAttrs.overridePythonAttrsFlag or 0 + 1; + }); in { overridePythonAttrs = { - expr = !lib.hasInfix "wrapPythonPrograms" p.postFixup; - expected = true; + expr = (applyOverridePythonAttrs python-package-stub).overridePythonAttrsFlag; + expected = 1; + }; + overridePythonAttrs-nested = { + expr = + (applyOverridePythonAttrs (applyOverridePythonAttrs python-package-stub)).overridePythonAttrsFlag; + expected = 2; }; }; From e8302431efb34fe0694a8bbc8f8f84abca456618 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Thu, 23 Oct 2025 18:36:31 +0800 Subject: [PATCH 2/3] makeOverridablePythonPackage: simplify implementation Attach overridePythonAttrs by attribute set update instead of `stdenv.mkDerivation`'s `passthru`. Co-authored-by: Matt Sturgeon --- .../python/python-packages-base.nix | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/pkgs/development/interpreters/python/python-packages-base.nix b/pkgs/development/interpreters/python/python-packages-base.nix index e3a92bef52f35..22c829bf736c0 100644 --- a/pkgs/development/interpreters/python/python-packages-base.nix +++ b/pkgs/development/interpreters/python/python-packages-base.nix @@ -19,22 +19,13 @@ let lib.mirrorFunctionArgs f ( origArgs: let - args = lib.fix ( - lib.extends (_: previousAttrs: { - passthru = (previousAttrs.passthru or { }) // { - overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs); - }; - }) (_: origArgs) - ); - result = f args; - overrideWith = newArgs: args // (if pkgs.lib.isFunction newArgs then newArgs args else newArgs); + result = f origArgs; + overrideWith = newArgs: origArgs // lib.toFunction newArgs origArgs; in - if builtins.isAttrs result then + if lib.isAttrs result then result - else if builtins.isFunction result then - { + // { overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs); - __functor = self: result; } else result From ca6c0900829c0612cc949ee20f1ec313f6b0a3c9 Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Sat, 28 Oct 2023 01:56:02 +0000 Subject: [PATCH 3/3] 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 22c829bf736c0..97ec3f5076f20 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 16ad0ec909c95..a41400d0d6c1d 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