Merge pull request #163597 from hercules-ci/types-optionType-only-merge-when-necessary

lib.types.optionType: Only merge when necessary
This commit is contained in:
Silvan Mosberger 2022-03-11 04:20:55 +01:00 committed by GitHub
commit 61019e394c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View file

@ -311,6 +311,9 @@ checkConfigOutput '^"hello"$' config.theOption.str ./optionTypeMerging.nix
# Test that types.optionType correctly annotates option locations # Test that types.optionType correctly annotates option locations
checkConfigError 'The option .theOption.nested. in .other.nix. is already declared in .optionTypeFile.nix.' config.theOption.nested ./optionTypeFile.nix checkConfigError 'The option .theOption.nested. in .other.nix. is already declared in .optionTypeFile.nix.' config.theOption.nested ./optionTypeFile.nix
# Test that types.optionType leaves types untouched as long as they don't need to be merged
checkConfigOutput 'ok' config.freeformItems.foo.bar ./adhoc-freeformType-survives-type-merge.nix
cat <<EOF cat <<EOF
====== module tests ====== ====== module tests ======
$pass Pass $pass Pass

View file

@ -0,0 +1,14 @@
{ lib, ... }: {
options.dummy = lib.mkOption { type = lib.types.anything; default = {}; };
freeformType =
let
a = lib.types.attrsOf (lib.types.submodule { options.bar = lib.mkOption { }; });
in
# modifying types like this breaks type merging.
# This test makes sure that type merging is not performed when only a single declaration exists.
# Don't modify types in practice!
a // {
merge = loc: defs: { freeformItems = a.merge loc defs; };
};
config.foo.bar = "ok";
}

View file

@ -535,7 +535,9 @@ rec {
description = "optionType"; description = "optionType";
check = value: value._type or null == "option-type"; check = value: value._type or null == "option-type";
merge = loc: defs: merge = loc: defs:
let if length defs == 1
then (head defs).value
else let
# Prepares the type definitions for mergeOptionDecls, which # Prepares the type definitions for mergeOptionDecls, which
# annotates submodules types with file locations # annotates submodules types with file locations
optionModules = map ({ value, file }: optionModules = map ({ value, file }: