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>
This commit is contained in:
Anselm Schüler 2022-01-19 17:09:38 +01:00
parent f2fe7b9b52
commit fdf7ede344

View file

@ -26,6 +26,7 @@ let
take take
; ;
inherit (lib.attrsets) inherit (lib.attrsets)
attrByPath
optionalAttrs optionalAttrs
; ;
inherit (lib.strings) inherit (lib.strings)
@ -99,6 +100,24 @@ rec {
type = lib.types.bool; 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 option accepts anything, but it does not produce any result.
This is useful for sharing a module across different module sets This is useful for sharing a module across different module sets