From b76164558092d668d2f82661516013b11e468efc Mon Sep 17 00:00:00 2001 From: Gavin John Date: Mon, 20 Jan 2025 16:23:11 -0800 Subject: [PATCH] mkPackageOption: add defaultText option --- lib/options.nix | 23 ++++++++++--------- lib/tests/modules.sh | 1 + lib/tests/modules/declare-mkPackageOption.nix | 4 ++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/options.nix b/lib/options.nix index fe665040ad03bb..336943a245fbef 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -311,29 +311,30 @@ rec { { nullable ? false, default ? name, + defaultText ? null, example ? null, extraDescription ? "", pkgsText ? "pkgs" }: let - name' = if isList name then last name else name; - default' = if isList default then default else [ default ]; - defaultText = concatStringsSep "." default'; - defaultValue = attrByPath default' - (throw "${defaultText} cannot be found in ${pkgsText}") pkgs; + name' = if lib.isList name then lib.last name else name; + default' = if lib.isList default then default else [ default ]; + defaultText' = lib.showAttrPath default'; + defaultValue = lib.attrByPath default' + (throw "${defaultText'} cannot be found in ${pkgsText}") pkgs; defaults = if default != null then { default = defaultValue; - defaultText = literalExpression ("${pkgsText}." + defaultText); - } else optionalAttrs nullable { + defaultText = if defaultText != null then defaultText else lib.literalExpression ("${pkgsText}." + defaultText'); + } else lib.optionalAttrs nullable { default = null; }; in mkOption (defaults // { description = "The ${name'} package to use." + (if extraDescription == "" then "" else " ") + extraDescription; - type = with lib.types; (if nullable then nullOr else lib.id) package; - } // optionalAttrs (example != null) { - example = literalExpression - (if isList example then "${pkgsText}." + concatStringsSep "." example else example); + type = (if nullable then lib.types.nullOr else lib.id) package; + } // lib.optionalAttrs (example != null) { + example = lib.literalExpression + (if lib.isList example then "${pkgsText}." + concatStringsSep "." example else example); }); /** diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index beb191aed0c142..e85f526fef4bfb 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -322,6 +322,7 @@ checkConfigError 'The option .undefinedPackage. was accessed but has no value de checkConfigOutput '^null$' config.nullablePackage ./declare-mkPackageOption.nix checkConfigOutput '^"null or package"$' options.nullablePackageWithDefault.type.description ./declare-mkPackageOption.nix checkConfigOutput '^"myPkgs\.hello"$' options.packageWithPkgsText.defaultText.text ./declare-mkPackageOption.nix +checkConfigOutput 'if config.useGo then pkgs.hello-go else pkgs.hello' options.packageWithDefaultText.defaultText.text ./declare-mkPackageOption.nix checkConfigOutput '^"hello-other"$' options.packageFromOtherSet.default.pname ./declare-mkPackageOption.nix # submoduleWith diff --git a/lib/tests/modules/declare-mkPackageOption.nix b/lib/tests/modules/declare-mkPackageOption.nix index 058c0addc0c397..7b6283f37ac4d5 100644 --- a/lib/tests/modules/declare-mkPackageOption.nix +++ b/lib/tests/modules/declare-mkPackageOption.nix @@ -48,6 +48,10 @@ in pkgsText = "myPkgs"; }; + packageWithDefaultText = lib.mkPackageOption pkgs "hello" { + defaultText = lib.literalExpression "if config.useGo then pkgs.hello-go else pkgs.hello"; + }; + packageFromOtherSet = let myPkgs = {