diff --git a/lib/options.nix b/lib/options.nix index 85ce6ca77e92..007a14f15b67 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -30,6 +30,7 @@ let inherit (lib.attrsets) attrByPath optionalAttrs + showAttrPath ; inherit (lib.strings) concatMapStrings @@ -40,6 +41,7 @@ let ; inherit (lib.lists) last + toList ; prioritySuggestion = '' Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions. @@ -310,14 +312,14 @@ rec { }: let name' = if isList name then last name else name; - default' = if isList default then default else [ default ]; - defaultText = concatStringsSep "." default'; + default' = toList default; + defaultText = showAttrPath default'; defaultValue = attrByPath default' (throw "${defaultText} cannot be found in ${pkgsText}") pkgs; defaults = if default != null then { default = defaultValue; - defaultText = literalExpression ("${pkgsText}." + defaultText); + defaultText = literalExpression "${pkgsText}.${defaultText}"; } else optionalAttrs nullable { @@ -333,7 +335,7 @@ rec { } // optionalAttrs (example != null) { example = literalExpression ( - if isList example then "${pkgsText}." + concatStringsSep "." example else example + if isList example then "${pkgsText}.${showAttrPath example}" else example ); } ); diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 66ce2df9fe87..3b19c8c63f26 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -332,6 +332,9 @@ checkConfigOutput '^"null or package"$' options.nullablePackage.type.description checkConfigOutput '^"hello"$' config.nullablePackageWithDefault.pname ./declare-mkPackageOption.nix checkConfigOutput '^"myPkgs\.hello"$' options.packageWithPkgsText.defaultText.text ./declare-mkPackageOption.nix checkConfigOutput '^"hello-other"$' options.packageFromOtherSet.default.pname ./declare-mkPackageOption.nix +checkConfigOutput '^"hello"$' config.packageInvalidIdentifier.pname ./declare-mkPackageOption.nix +checkConfigOutput '^"pkgs\.\\"123\\"\.\\"with\\\\\\"quote\\"\.hello"$' options.packageInvalidIdentifier.defaultText.text ./declare-mkPackageOption.nix +checkConfigOutput '^"pkgs\.\\"123\\"\.\\"with\\\\\\"quote\\"\.hello"$' options.packageInvalidIdentifierExample.example.text ./declare-mkPackageOption.nix # submoduleWith diff --git a/lib/tests/modules/declare-mkPackageOption.nix b/lib/tests/modules/declare-mkPackageOption.nix index 058c0addc0c3..3f4f91d7497e 100644 --- a/lib/tests/modules/declare-mkPackageOption.nix +++ b/lib/tests/modules/declare-mkPackageOption.nix @@ -57,5 +57,19 @@ in }; in lib.mkPackageOption myPkgs "hello" { }; + + packageInvalidIdentifier = + let + myPkgs."123"."with\"quote" = { inherit (pkgs) hello; }; + in + lib.mkPackageOption myPkgs [ "123" "with\"quote" "hello" ] { }; + + packageInvalidIdentifierExample = lib.mkPackageOption pkgs "hello" { + example = [ + "123" + "with\"quote" + "hello" + ]; + }; }; }