summary refs log tree commit diff
path: root/lib/options.nix
diff options
context:
space:
mode:
authorNaïm Favier <n@monade.li>2023-05-19 12:22:21 +0200
committerNaïm Favier <n@monade.li>2023-05-19 12:22:21 +0200
commit4a56b2655ef3ce7b1513ab6feb0b485edbe548e8 (patch)
treef20afb63ccb883dbf502ee22804f2261c8823b76 /lib/options.nix
parent8d25ab1fc6d9585ea48733fd351820dc6c9d6833 (diff)
downloadnixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar.gz
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar.bz2
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar.lz
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar.xz
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.tar.zst
nixpkgs-4a56b2655ef3ce7b1513ab6feb0b485edbe548e8.zip
lib/options: nullable mkPackageOption
It is sometimes useful to allow setting a package option to `null` to
skip installing the package. See
https://github.com/nix-community/home-manager/pull/3668#issuecomment-1554044171
for example.
Diffstat (limited to 'lib/options.nix')
-rw-r--r--lib/options.nix21
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/options.nix b/lib/options.nix
index d71d9421b7b..af7914bb513 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: