lib.options.mkPackageOption: use lib.showAttrPath

Make use of `lib.showAttrPath` instead of manually doing `concatStringsSep "."`.

This means edge-cases such as the attr-path including names that are not
valid nix identifiers will be handled better.

See:
- https://nix.dev/manual/nix/2.26/language/identifiers
- https://nixos.org/manual/nixpkgs/unstable/#function-library-lib.attrsets.showAttrPath

Changed files
+23 -4
lib
+6 -4
lib/options.nix
···
inherit (lib.attrsets)
attrByPath
optionalAttrs
+
showAttrPath
;
inherit (lib.strings)
concatMapStrings
···
;
inherit (lib.lists)
last
+
toList
;
prioritySuggestion = ''
Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.
···
}:
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 {
···
}
// optionalAttrs (example != null) {
example = literalExpression (
-
if isList example then "${pkgsText}." + concatStringsSep "." example else example
+
if isList example then "${pkgsText}.${showAttrPath example}" else example
);
}
);
+3
lib/tests/modules.sh
···
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
+14
lib/tests/modules/declare-mkPackageOption.nix
···
};
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"
+
];
+
};
};
}