lib/types: Test attrsWith type merging

Co-Authored-By: @hsjobeki

Changed files
+35 -20
lib
+4 -2
lib/tests/modules.sh
···
checkConfigOutput '^false$' config.conditionalWorks ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
checkConfigOutput '^"empty"$' config.value.foo ./declare-lazyAttrsOf.nix ./attrsOf-conditional-check.nix
+
# Check attrsWith type merging
+
checkConfigError 'The option `mergedLazyNonLazy'\'' in `.*'\'' is already declared in `.*'\''\.' options.mergedLazyNonLazy ./lazy-attrsWith.nix
+
checkConfigOutput '^11$' config.lazyResult ./lazy-attrsWith.nix
+
checkConfigError 'infinite recursion encountered' config.nonLazyResult ./lazy-attrsWith.nix
# Even with multiple assignments, a type error should be thrown if any of them aren't valid
checkConfigError 'A definition for option .* is not of type .*' \
···
# nested options work
checkConfigOutput '^34$' options.nested.nestedLine34.declarationPositions.0.line ./declaration-positions.nix
-
# AttrsWith tests
-
checkConfigOutput '^11$' config.result ./lazy-attrsWith.nix
cat <<EOF
====== module tests ======
+29 -17
lib/tests/modules/lazy-attrsWith.nix
···
{ lib, ... }:
let
inherit (lib) types mkOption;
+
+
lazyAttrsOf = mkOption {
+
# Same as lazyAttrsOf
+
type = types.attrsWith {
+
lazy = true;
+
elemType = types.int;
+
};
+
};
+
+
attrsOf = mkOption {
+
# Same as lazyAttrsOf
+
type = types.attrsWith {
+
elemType = types.int;
+
};
+
};
in
{
imports = [
···
(
{ ... }:
{
-
options.mergedLazy = mkOption {
-
# Same as lazyAttrsOf
-
type = types.attrsWith {
-
lazy = true;
-
elemType = types.int;
-
};
-
};
+
options.mergedLazyLazy = lazyAttrsOf;
+
options.mergedLazyNonLazy = lazyAttrsOf;
+
options.mergedNonLazyNonLazy = attrsOf;
}
)
# Module B
(
{ ... }:
{
-
options.mergedLazy = lib.mkOption {
-
# Same as lazyAttrsOf
-
type = types.attrsWith {
-
lazy = true;
-
elemType = types.int;
-
};
-
};
+
options.mergedLazyLazy = lazyAttrsOf;
+
options.mergedLazyNonLazy = attrsOf;
+
options.mergedNonLazyNonLazy = attrsOf;
}
)
# Result
···
{ config, ... }:
{
# Can only evaluate if lazy
-
config.mergedLazy.bar = config.mergedLazy.baz + 1;
-
config.mergedLazy.baz = 10;
-
options.result = mkOption { default = config.mergedLazy.bar; };
+
config.mergedLazyLazy.bar = config.mergedLazyLazy.baz + 1;
+
config.mergedLazyLazy.baz = 10;
+
options.lazyResult = mkOption { default = config.mergedLazyLazy.bar; };
+
+
# Can not only evaluate if not lazy
+
config.mergedNonLazyNonLazy.bar = config.mergedNonLazyNonLazy.baz + 1;
+
config.mergedNonLazyNonLazy.baz = 10;
+
options.nonLazyResult = mkOption { default = config.mergedNonLazyNonLazy.bar; };
}
)
];
+2 -1
lib/types.nix
···
getSubModules = elemType.getSubModules;
substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit lazy; };
functor = defaultFunctor "attrsWith" // {
-
wrapped = elemType;
+
# TODO: This breaks stuff
+
# wrapped = elemType;
payload = {
# Important!: Add new function attributes here in case of future changes
inherit elemType lazy;