Skip to content
Open
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
4 changes: 4 additions & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ checkConfigOutput '^{}$' config.value.mkmerge ./types-anything/mk-mods.nix
checkConfigOutput '^true$' config.value.mkbefore ./types-anything/mk-mods.nix
checkConfigOutput '^1$' config.value.nested.foo ./types-anything/mk-mods.nix
checkConfigOutput '^"baz"$' config.value.nested.bar.baz ./types-anything/mk-mods.nix
# Function reflection works
checkConfigOutput '^{"p":1,"q":2}$' 'config.applied.merging-args.value' ./types-anything/functions.nix
checkConfigOutput '^{"a":false,"b":false}$' 'config.applied.merging-args.args' ./types-anything/functions.nix


## types.functionTo
checkConfigOutput '^"input is input"$' config.result ./functionTo/trivial.nix
Expand Down
16 changes: 15 additions & 1 deletion lib/tests/modules/types-anything/functions.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,32 @@
};

options.applied = lib.mkOption {
default = lib.mapAttrs (name: fun: fun null) config.value;
default = lib.mapAttrs (name: fun:
if name == "merging-args"
then
{
args =
let x = lib.functionArgs fun;
in builtins.deepSeq x x;
value =
let x = fun { a = 1; b = 2; };
# can't pass --strict easily
in builtins.deepSeq x x;
}
else fun null) config.value;
};

config = lib.mkMerge [
{
value.single-lambda = x: x;
value.multiple-lambdas = x: { inherit x; };
value.merging-lambdas = x: { inherit x; };
value.merging-args = args@{ a, ... }: { p = a; };
}
{
value.multiple-lambdas = x: [ x ];
value.merging-lambdas = y: { inherit y; };
value.merging-args = args@{ b, ... }: { q = b; };
}
];

Expand Down
28 changes: 22 additions & 6 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let
isList
isString
isStorePath
functionArgs
setFunctionArgs
throwIf
toDerivation
toList
Expand Down Expand Up @@ -303,12 +305,26 @@ rec {
set = (attrsOf anything).merge;
# This is the type of packages, only accept a single definition
stringCoercibleSet = mergeOneOption;
lambda = loc: defs: arg: anything.merge
(loc ++ [ "<function body>" ])
(map (def: {
file = def.file;
value = def.value arg;
}) defs);
lambda = loc: defs:
let
args = zipAttrsWith
(name:
# All params must have a default for the combination
# to have one. AND on a list is `all id`.
all (b: b))
(map
(def: functionArgs def.value)
defs);
mergedFun = arg: anything.merge
(loc ++ [ "<function body>" ])
(map (def: {
file = def.file;
value = def.value arg;
}) defs);
in
if args == {}
then mergedFun
else setFunctionArgs mergedFun args;
# Otherwise fall back to only allowing all equal definitions
}.${commonType} or mergeEqualOption;
in mergeFunction loc defs;
Expand Down
Loading