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
This commit is contained in:
Matt Sturgeon 2025-04-12 05:56:30 +01:00
parent 4c26f96059
commit 6107d48bcb
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 23 additions and 4 deletions

View file

@ -30,6 +30,7 @@ let
inherit (lib.attrsets) inherit (lib.attrsets)
attrByPath attrByPath
optionalAttrs optionalAttrs
showAttrPath
; ;
inherit (lib.strings) inherit (lib.strings)
concatMapStrings concatMapStrings
@ -40,6 +41,7 @@ let
; ;
inherit (lib.lists) inherit (lib.lists)
last last
toList
; ;
prioritySuggestion = '' prioritySuggestion = ''
Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions. Use `lib.mkForce value` or `lib.mkDefault value` to change the priority on any of these definitions.
@ -310,14 +312,14 @@ rec {
}: }:
let let
name' = if isList name then last name else name; name' = if isList name then last name else name;
default' = if isList default then default else [ default ]; default' = toList default;
defaultText = concatStringsSep "." default'; defaultText = showAttrPath default';
defaultValue = attrByPath default' (throw "${defaultText} cannot be found in ${pkgsText}") pkgs; defaultValue = attrByPath default' (throw "${defaultText} cannot be found in ${pkgsText}") pkgs;
defaults = defaults =
if default != null then if default != null then
{ {
default = defaultValue; default = defaultValue;
defaultText = literalExpression ("${pkgsText}." + defaultText); defaultText = literalExpression "${pkgsText}.${defaultText}";
} }
else else
optionalAttrs nullable { optionalAttrs nullable {
@ -333,7 +335,7 @@ rec {
} }
// optionalAttrs (example != null) { // optionalAttrs (example != null) {
example = literalExpression ( example = literalExpression (
if isList example then "${pkgsText}." + concatStringsSep "." example else example if isList example then "${pkgsText}.${showAttrPath example}" else example
); );
} }
); );

View file

@ -332,6 +332,9 @@ checkConfigOutput '^"null or package"$' options.nullablePackage.type.description
checkConfigOutput '^"hello"$' config.nullablePackageWithDefault.pname ./declare-mkPackageOption.nix checkConfigOutput '^"hello"$' config.nullablePackageWithDefault.pname ./declare-mkPackageOption.nix
checkConfigOutput '^"myPkgs\.hello"$' options.packageWithPkgsText.defaultText.text ./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-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 # submoduleWith

View file

@ -57,5 +57,19 @@ in
}; };
in in
lib.mkPackageOption myPkgs "hello" { }; 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"
];
};
}; };
} }