Skip to content

Commit

Permalink
mkPackageOption: add defaultText option
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandapip1 committed Jan 28, 2025
1 parent 76ce79f commit b761645
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

/**
Expand Down
1 change: 1 addition & 0 deletions lib/tests/modules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/tests/modules/declare-mkPackageOption.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit b761645

Please sign in to comment.