about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorJonathan Ringer <jonringer117@gmail.com>2022-01-24 23:49:19 -0800
committerJonathan Ringer <jonringer117@gmail.com>2022-01-24 23:49:19 -0800
commite379e3d4bbaf7cbd8a44914ba4e200597a2505f1 (patch)
treeb8e0d7b8b4849df8d77c93183b245c63655172a3 /lib
parent2cd72d57a3e2329a61b91e12e2e101b0d88935c5 (diff)
parentcfef27f7cfa05a59b768ae3c4aa0c54bb9572677 (diff)
Merge remote-tracking branch 'origin/staging-next' into staging
Conflicts:
	pkgs/development/python-modules/googleapis-common-protos/default.nix
Diffstat (limited to 'lib')
-rw-r--r--lib/meta.nix2
-rw-r--r--lib/options.nix44
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/meta.nix b/lib/meta.nix
index bc3387646f264..5b1f7ee5ff2d1 100644
--- a/lib/meta.nix
+++ b/lib/meta.nix
@@ -78,7 +78,7 @@ rec {
 
        2. (modern) a pattern for the platform `parsed` field.
 
-     We can inject these into a patten for the whole of a structured platform,
+     We can inject these into a pattern for the whole of a structured platform,
      and then match that.
   */
   platformMatch = platform: elem: let
diff --git a/lib/options.nix b/lib/options.nix
index 53001a3113f93..794ca5e339425 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,49 @@ rec {
     type = lib.types.bool;
   };
 
+  /* Creates an Option attribute set for an option that specifies the
+     package a module should use for some purpose.
+
+     Type: mkPackageOption :: pkgs -> string -> { default :: [string], example :: null | string | [string] } -> option
+
+     The package is specified as a list of strings representing its attribute path in nixpkgs.
+
+     Because of this, you need to pass nixpkgs itself as the first argument.
+
+     The second argument is the name of the option, used in the description "The <name> package to use.".
+
+     You can also pass an example value, either a literal string or a package's attribute path.
+
+     You can omit the default path if the name of the option is also attribute path in nixpkgs.
+
+     Example:
+       mkPackageOption pkgs "hello" { }
+       => { _type = "option"; default = «derivation /nix/store/3r2vg51hlxj3cx5vscp0vkv60bqxkaq0-hello-2.10.drv»; defaultText = { ... }; description = "The hello package to use."; type = { ... }; }
+
+     Example:
+       mkPackageOption pkgs "GHC" {
+         default = [ "ghc" ];
+         example = "pkgs.haskell.package.ghc921.ghc.withPackages (hkgs: [ hkgs.primes ])";
+       }
+       => { _type = "option"; default = «derivation /nix/store/jxx55cxsjrf8kyh3fp2ya17q99w7541r-ghc-8.10.7.drv»; defaultText = { ... }; description = "The GHC package to use."; example = { ... }; type = { ... }; }
+  */
+  mkPackageOption =
+    # Package set (a specific version of nixpkgs)
+    pkgs:
+      # Name for the package, shown in option description
+      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