Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkPackageOption: add defaultText option #375424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) lib.types.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";
};
Pandapip1 marked this conversation as resolved.
Show resolved Hide resolved

packageFromOtherSet =
let
myPkgs = {
Expand Down