about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2023-05-22 11:59:10 +0200
committerGitHub <noreply@github.com>2023-05-22 11:59:10 +0200
commitfc89970a268de31561c98a4b61e40b6f0636d50a (patch)
treed25b25c311d5c75ad1186e5f166c54cf4e787bf1 /lib
parentd5413b971fa00f32ae424e7c075289b039cf352b (diff)
parentac9915b1eaaa93edcb7da8af2b2f797f6c3da4a2 (diff)
downloadnixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar.gz
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar.bz2
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar.lz
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar.xz
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.tar.zst
nixlib-fc89970a268de31561c98a4b61e40b6f0636d50a.zip
Merge pull request #232808 from ncfavier/mkPackageOption-nullable
lib/options: nullable mkPackageOption
Diffstat (limited to 'lib')
-rw-r--r--lib/options.nix21
-rwxr-xr-xlib/tests/modules.sh5
-rw-r--r--lib/tests/modules/declare-mkPackageOption.nix19
3 files changed, 38 insertions, 7 deletions
diff --git a/lib/options.nix b/lib/options.nix
index d71d9421b7b1..af7914bb5137 100644
--- a/lib/options.nix
+++ b/lib/options.nix
@@ -155,6 +155,8 @@ rec {
       # Name for the package, shown in option description
       name:
       {
+        # Whether the package can be null, for example to disable installing a package altogether.
+        nullable ? false,
         # The attribute path where the default package is located (may be omitted)
         default ? name,
         # A string or an attribute path to use as an example (may be omitted)
@@ -164,19 +166,24 @@ rec {
       }:
       let
         name' = if isList name then last name else name;
+      in mkOption ({
+        type = with lib.types; (if nullable then nullOr else lib.id) package;
+        description = "The ${name'} package to use."
+          + (if extraDescription == "" then "" else " ") + extraDescription;
+      } // (if default != null then let
         default' = if isList default then default else [ default ];
         defaultPath = concatStringsSep "." default';
         defaultValue = attrByPath default'
           (throw "${defaultPath} cannot be found in pkgs") pkgs;
-      in mkOption {
+      in {
+        default = defaultValue;
         defaultText = literalExpression ("pkgs." + defaultPath);
-        type = lib.types.package;
-        description = "The ${name'} package to use."
-          + (if extraDescription == "" then "" else " ") + extraDescription;
-        ${if default != null then "default" else null} = defaultValue;
-        ${if example != null then "example" else null} = literalExpression
+      } else if nullable then {
+        default = null;
+      } else { }) // lib.optionalAttrs (example != null) {
+        example = literalExpression
           (if isList example then "pkgs." + concatStringsSep "." example else example);
-      };
+      });
 
   /* Like mkPackageOption, but emit an mdDoc description instead of DocBook. */
   mkPackageOptionMD = pkgs: name: extra:
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index 45c247cbbea6..7fdc3d3d81aa 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -182,6 +182,11 @@ checkConfigOutput '^true$' config.enableAlias ./alias-with-priority.nix
 checkConfigOutput '^false$' config.enable ./alias-with-priority-can-override.nix
 checkConfigOutput '^false$' config.enableAlias ./alias-with-priority-can-override.nix
 
+# Check mkPackageOption
+checkConfigOutput '^"hello"$' config.package.pname ./declare-mkPackageOption.nix
+checkConfigError 'The option .undefinedPackage. is used but not defined' config.undefinedPackage ./declare-mkPackageOption.nix
+checkConfigOutput '^null$' config.nullablePackage ./declare-mkPackageOption.nix
+
 # submoduleWith
 
 ## specialArgs should work
diff --git a/lib/tests/modules/declare-mkPackageOption.nix b/lib/tests/modules/declare-mkPackageOption.nix
new file mode 100644
index 000000000000..640b19a7bf22
--- /dev/null
+++ b/lib/tests/modules/declare-mkPackageOption.nix
@@ -0,0 +1,19 @@
+{ lib, ... }: let
+  pkgs.hello = {
+    type = "derivation";
+    pname = "hello";
+  };
+in {
+  options = {
+    package = lib.mkPackageOption pkgs "hello" { };
+
+    undefinedPackage = lib.mkPackageOption pkgs "hello" {
+      default = null;
+    };
+
+    nullablePackage = lib.mkPackageOption pkgs "hello" {
+      nullable = true;
+      default = null;
+    };
+  };
+}