summary refs log tree commit diff
path: root/lib/options.nix
diff options
context:
space:
mode:
authorAnselm Schüler <mail@anselmschueler.com>2022-01-19 17:09:38 +0100
committerAnselm Schüler <mail@anselmschueler.com>2022-01-23 14:23:03 +0100
commitfdf7ede344318318f82594b0d8a7012667eba1ea (patch)
treeb03aa884a212bfaf42f54b226fb4abfe7f1b4daa /lib/options.nix
parentf2fe7b9b525a4e553bcb1d554e7d235beb1fe133 (diff)
downloadnixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar.gz
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar.bz2
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar.lz
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar.xz
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.tar.zst
nixpkgs-fdf7ede344318318f82594b0d8a7012667eba1ea.zip
lib/options: Add mkPackageOption
This is a squashed commit. These are the original commit messages:

lib/options: Add mkPackageOption

lib/options: Add missing semicolon

lib/options.nix: Make mkPackageOption more complicated

lib/options: Fix indent. & spacing

lib/options.nix: Remove example and align comment

lib/options: ravenous overuse of arguments

lib/options: Format better

lib/options: Add default examplePath decl

lib/options: Make better mkPackageOption function

lib/options: Remove trailing whitespace

lib/options: Improve mkPackageOptions

lib/options: Remove pkgs prefixing
Co-authored-by: pennae <82953136+pennae@users.noreply.github.com>

lib/options: Slim down mkPackageOption further

lib/options: mkPackageOption: Add "pkgs." to example

lib/options: mkPackageOption: Make name & pkgs single arguments

lib/options: mkPackageOption: Swap name & pkgs

lib/options: Remove unnecessary import
Co-authored-by: pennae <82953136+pennae@users.noreply.github.com>
Diffstat (limited to 'lib/options.nix')
-rw-r--r--lib/options.nix19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/options.nix b/lib/options.nix
index 53001a3113f..b3a011168c3 100644
--- a/lib/options.nix
+++ b/lib/options.nix
@@ -26,6 +26,7 @@ let
     take
     ;
   inherit (lib.attrsets)
+    attrByPath
     optionalAttrs
     ;
   inherit (lib.strings)
@@ -99,6 +100,24 @@ rec {
     type = lib.types.bool;
   };
 
+  /* Creaties an Option attribute set for an option that specifies the
+     package a module should use.
+
+     The argument default is an attribute set path in pkgs.
+  */
+  mkPackageOption = pkgs: name:
+    { default ? [ name ], example ? null }:
+    let default' = if !isList default then [ default ] else default;
+    in mkOption {
+      type = lib.types.package;
+      description = "The ${name} package to use.";
+      default = attrByPath default'
+        (throw "${concatStringsSep "." default'} cannot be found in pkgs") pkgs;
+      defaultText = literalExpression ("pkgs." + concatStringsSep "." default');
+      ${if example != null then "example" else null} = literalExpression
+        (if isList example then "pkgs." + concatStringsSep "." example else example);
+    };
+
   /* This option accepts anything, but it does not produce any result.
 
      This is useful for sharing a module across different module sets