diff options
Diffstat (limited to 'lib/options.nix')
-rw-r--r-- | lib/options.nix | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/lib/options.nix b/lib/options.nix index 53001a3113f93..627aac24d2fb2 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 @@ -128,11 +172,13 @@ rec { else if all isInt list && all (x: x == head list) list then head list else throw "Cannot merge definitions of `${showOption loc}'. Definition values:${showDefs defs}"; - mergeOneOption = loc: defs: - if defs == [] then abort "This case should never happen." - else if length defs != 1 then - throw "The unique option `${showOption loc}' is defined multiple times. Definition values:${showDefs defs}" - else (head defs).value; + mergeOneOption = mergeUniqueOption { message = ""; }; + + mergeUniqueOption = { message }: loc: defs: + if length defs == 1 + then (head defs).value + else assert length defs > 1; + throw "The option `${showOption loc}' is defined multiple times.\n${message}\nDefinition values:${showDefs defs}"; /* "Merge" option definitions by checking that they all have the same value. */ mergeEqualOption = loc: defs: |