lib/types: Make types.anything merge functions

Previously it would give an error if there were multiple function
definitions.
This commit is contained in:
Silvan Mosberger 2021-10-02 16:37:22 +02:00
parent 54e1db2152
commit 48293bd6b6
3 changed files with 18 additions and 4 deletions

View File

@ -254,8 +254,10 @@ checkConfigOutput / config.value.path ./types-anything/equal-atoms.nix
checkConfigOutput null config.value.null ./types-anything/equal-atoms.nix
checkConfigOutput 0.1 config.value.float ./types-anything/equal-atoms.nix
# Functions can't be merged together
checkConfigError "The option .* has conflicting definition values" config.value.multiple-lambdas ./types-anything/functions.nix
checkConfigError "The option .value.multiple-lambdas.<function body>. has conflicting option types" config.applied.multiple-lambdas ./types-anything/functions.nix
checkConfigOutput '<LAMBDA>' config.value.single-lambda ./types-anything/functions.nix
checkConfigOutput 'null' config.applied.merging-lambdas.x ./types-anything/functions.nix
checkConfigOutput 'null' config.applied.merging-lambdas.y ./types-anything/functions.nix
# Check that all mk* modifiers are applied
checkConfigError 'attribute .* not found' config.value.mkiffalse ./types-anything/mk-mods.nix
checkConfigOutput '{ }' config.value.mkiftrue ./types-anything/mk-mods.nix

View File

@ -1,16 +1,22 @@
{ lib, ... }: {
{ lib, config, ... }: {
options.value = lib.mkOption {
type = lib.types.anything;
};
options.applied = lib.mkOption {
default = lib.mapAttrs (name: fun: fun null) config.value;
};
config = lib.mkMerge [
{
value.single-lambda = x: x;
value.multiple-lambdas = x: x;
value.multiple-lambdas = x: { inherit x; };
value.merging-lambdas = x: { inherit x; };
}
{
value.multiple-lambdas = x: x;
value.multiple-lambdas = x: [ x ];
value.merging-lambdas = y: { inherit y; };
}
];

View File

@ -192,6 +192,12 @@ rec {
else (listOf 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);
# Otherwise fall back to only allowing all equal definitions
}.${commonType} or mergeEqualOption;
in mergeFunction loc defs;