Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/customisation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,22 @@ rec {
Type:
makeOverridable :: (AttrSet -> a) -> AttrSet -> a
*/
makeOverridable = f: lib.setFunctionArgs
(origArgs: let
makeOverridable = f:
let
# Creates a functor with the same arguments as f
mirrorArgs = lib.mirrorFunctionArgs f;
in
mirrorArgs (origArgs:
let
result = f origArgs;

# Creates a functor with the same arguments as f
copyArgs = g: lib.setFunctionArgs g (lib.functionArgs f);
# Changes the original arguments with (potentially a function that returns) a set of new attributes
overrideWith = newArgs: origArgs // (if lib.isFunction newArgs then newArgs origArgs else newArgs);

# Re-call the function but with different arguments
overrideArgs = copyArgs (newArgs: makeOverridable f (overrideWith newArgs));
overrideArgs = mirrorArgs (newArgs: makeOverridable f (overrideWith newArgs));
# Change the result of the function call by applying g to it
overrideResult = g: makeOverridable (copyArgs (args: g (f args))) origArgs;
overrideResult = g: makeOverridable (mirrorArgs (args: g (f args))) origArgs;
in
if builtins.isAttrs result then
result // {
Expand All @@ -102,8 +105,7 @@ rec {
lib.setFunctionArgs result (lib.functionArgs result) // {
override = overrideArgs;
}
else result)
(lib.functionArgs f);
else result);


/* Call the package function in the file `fn` with the required
Expand Down
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let
importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
info showWarnings nixpkgsVersion version isInOldestRelease
mod compare splitByAndCompare
functionArgs setFunctionArgs isFunction toFunction
functionArgs setFunctionArgs isFunction toFunction mirrorFunctionArgs
toHexString toBaseDigits inPureEvalMode;
inherit (self.fixedPoints) fix fix' converge extends composeExtensions
composeManyExtensions makeExtensible makeExtensibleWithCustomName;
Expand Down
34 changes: 34 additions & 0 deletions lib/trivial.nix
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,40 @@ rec {
isFunction = f: builtins.isFunction f ||
(f ? __functor && isFunction (f.__functor f));

/*
`mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`)
but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`).

Type:
mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c)

Example:
addab = {a, b}: a + b
addab { a = 2; b = 4; }
=> 6
lib.functionArgs addab
=> { a = false; b = false; }
addab1 = attrs: addab attrs + 1
addab1 { a = 2; b = 4; }
=> 7
lib.functionArgs addab1
=> { }
addab1' = lib.mirrorFunctionArgs addab addab1
addab1' { a = 2; b = 4; }
=> 7
lib.functionArgs addab1'
=> { a = false; b = false; }
*/
mirrorFunctionArgs =
# Function to provide the argument metadata
f:
let
fArgs = functionArgs f;
in
# Function to set the argument metadata to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: This doesn't render in nixdoc right now, but I think we can keep it anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that, too. It would be fine as long as the function description doesn't depend on the argument name used here.

Nevertheless, I do hope that the second one could get rendered in the future.

g:
setFunctionArgs g fArgs;

/*
Turns any non-callable values into constant functions.
Returns callable values as is.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/interpreters/python/python-packages-base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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: origArgs:
makeOverridablePythonPackage = f: lib.mirrorFunctionArgs f (origArgs:
let
args = lib.fix (lib.extends
(_: previousAttrs: {
Expand All @@ -30,7 +30,7 @@ let
overridePythonAttrs = newArgs: makeOverridablePythonPackage f (overrideWith newArgs);
__functor = self: result;
}
else result;
else result);

mkPythonDerivation = if python.isPy3k then
./mk-python-derivation.nix
Expand Down