Merge pull request #265710 from ShamrockLee/lib-copy-function-args

lib.mirrorFunctionArgs: init

authored by Silvan Mosberger and committed by GitHub 7eddbf56 5d6c7322

+47 -11
+10 -8
lib/customisation.nix
··· 76 76 Type: 77 77 makeOverridable :: (AttrSet -> a) -> AttrSet -> a 78 78 */ 79 - makeOverridable = f: lib.setFunctionArgs 80 - (origArgs: let 79 + makeOverridable = f: 80 + let 81 + # Creates a functor with the same arguments as f 82 + mirrorArgs = lib.mirrorFunctionArgs f; 83 + in 84 + mirrorArgs (origArgs: 85 + let 81 86 result = f origArgs; 82 87 83 - # Creates a functor with the same arguments as f 84 - copyArgs = g: lib.setFunctionArgs g (lib.functionArgs f); 85 88 # Changes the original arguments with (potentially a function that returns) a set of new attributes 86 89 overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs); 87 90 88 91 # Re-call the function but with different arguments 89 - overrideArgs = copyArgs (newArgs: makeOverridable f (overrideWith newArgs)); 92 + overrideArgs = mirrorArgs (newArgs: makeOverridable f (overrideWith newArgs)); 90 93 # Change the result of the function call by applying g to it 91 - overrideResult = g: makeOverridable (copyArgs (args: g (f args))) origArgs; 94 + overrideResult = g: makeOverridable (mirrorArgs (args: g (f args))) origArgs; 92 95 in 93 96 if builtins.isAttrs result then 94 97 result // { ··· 102 105 lib.setFunctionArgs result (lib.functionArgs result) // { 103 106 override = overrideArgs; 104 107 } 105 - else result) 106 - (lib.functionArgs f); 108 + else result); 107 109 108 110 109 111 /* Call the package function in the file `fn` with the required
+1 -1
lib/default.nix
··· 74 74 importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum 75 75 info showWarnings nixpkgsVersion version isInOldestRelease 76 76 mod compare splitByAndCompare 77 - functionArgs setFunctionArgs isFunction toFunction 77 + functionArgs setFunctionArgs isFunction toFunction mirrorFunctionArgs 78 78 toHexString toBaseDigits inPureEvalMode; 79 79 inherit (self.fixedPoints) fix fix' converge extends composeExtensions 80 80 composeManyExtensions makeExtensible makeExtensibleWithCustomName;
+34
lib/trivial.nix
··· 449 449 (f ? __functor && isFunction (f.__functor f)); 450 450 451 451 /* 452 + `mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`) 453 + but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`). 454 + 455 + Type: 456 + mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c) 457 + 458 + Example: 459 + addab = {a, b}: a + b 460 + addab { a = 2; b = 4; } 461 + => 6 462 + lib.functionArgs addab 463 + => { a = false; b = false; } 464 + addab1 = attrs: addab attrs + 1 465 + addab1 { a = 2; b = 4; } 466 + => 7 467 + lib.functionArgs addab1 468 + => { } 469 + addab1' = lib.mirrorFunctionArgs addab addab1 470 + addab1' { a = 2; b = 4; } 471 + => 7 472 + lib.functionArgs addab1' 473 + => { a = false; b = false; } 474 + */ 475 + mirrorFunctionArgs = 476 + # Function to provide the argument metadata 477 + f: 478 + let 479 + fArgs = functionArgs f; 480 + in 481 + # Function to set the argument metadata to 482 + g: 483 + setFunctionArgs g fArgs; 484 + 485 + /* 452 486 Turns any non-callable values into constant functions. 453 487 Returns callable values as is. 454 488
+2 -2
pkgs/development/interpreters/python/python-packages-base.nix
··· 13 13 14 14 # Derivations built with `buildPythonPackage` can already be overridden with `override`, `overrideAttrs`, and `overrideDerivation`. 15 15 # This function introduces `overridePythonAttrs` and it overrides the call to `buildPythonPackage`. 16 - makeOverridablePythonPackage = f: origArgs: 16 + makeOverridablePythonPackage = f: lib.mirrorFunctionArgs f (origArgs: 17 17 let 18 18 args = lib.fix (lib.extends 19 19 (_: previousAttrs: { ··· 30 30 overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs); 31 31 __functor = self: result; 32 32 } 33 - else result; 33 + else result); 34 34 35 35 mkPythonDerivation = if python.isPy3k then 36 36 ./mk-python-derivation.nix