2009-02-09 09:51:03 -07:00
|
|
|
|
# Nixpkgs/NixOS option handling.
|
|
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
2009-02-28 11:21:25 -07:00
|
|
|
|
with import ./trivial.nix;
|
2009-02-09 09:51:03 -07:00
|
|
|
|
with import ./lists.nix;
|
2009-02-28 11:21:25 -07:00
|
|
|
|
with import ./misc.nix;
|
2009-02-09 09:51:03 -07:00
|
|
|
|
with import ./attrsets.nix;
|
2013-10-27 18:14:16 -06:00
|
|
|
|
with import ./strings.nix;
|
2009-02-09 09:51:03 -07:00
|
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
2013-10-23 10:20:39 -06:00
|
|
|
|
isOption = lib.isType "option";
|
2009-05-27 14:25:17 -06:00
|
|
|
|
mkOption = attrs: attrs // {
|
|
|
|
|
_type = "option";
|
|
|
|
|
# name (this is the name of the attributem it is automatically generated by the traversal)
|
|
|
|
|
# default (value used when no definition exists)
|
|
|
|
|
# example (documentation)
|
|
|
|
|
# description (documentation)
|
|
|
|
|
# type (option type, provide a default merge function and ensure type correctness)
|
|
|
|
|
# merge (function used to merge definitions into one definition: [ /type/ ] -> /type/)
|
|
|
|
|
# apply (convert the option value to ease the manipulation of the option result)
|
|
|
|
|
# options (set of sub-options declarations & definitions)
|
|
|
|
|
};
|
|
|
|
|
|
2013-07-18 13:13:42 -06:00
|
|
|
|
mkEnableOption = name: mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
example = true;
|
2013-10-17 06:29:51 -06:00
|
|
|
|
description = "Whether to enable ${name}.";
|
2013-07-18 13:13:42 -06:00
|
|
|
|
type = lib.types.bool;
|
|
|
|
|
};
|
|
|
|
|
|
2009-05-27 14:25:17 -06:00
|
|
|
|
# !!! This function will be removed because this can be done with the
|
|
|
|
|
# multiple option declarations.
|
2009-02-09 09:51:03 -07:00
|
|
|
|
addDefaultOptionValues = defs: opts: opts //
|
|
|
|
|
builtins.listToAttrs (map (defName:
|
|
|
|
|
{ name = defName;
|
|
|
|
|
value =
|
|
|
|
|
let
|
|
|
|
|
defValue = builtins.getAttr defName defs;
|
|
|
|
|
optValue = builtins.getAttr defName opts;
|
|
|
|
|
in
|
2013-03-13 08:05:30 -06:00
|
|
|
|
if isOption defValue
|
2009-02-09 09:51:03 -07:00
|
|
|
|
then
|
|
|
|
|
# `defValue' is an option.
|
2009-05-19 08:54:41 -06:00
|
|
|
|
if hasAttr defName opts
|
2009-02-09 09:51:03 -07:00
|
|
|
|
then builtins.getAttr defName opts
|
|
|
|
|
else defValue.default
|
|
|
|
|
else
|
|
|
|
|
# `defValue' is an attribute set containing options.
|
|
|
|
|
# So recurse.
|
2009-05-19 08:54:41 -06:00
|
|
|
|
if hasAttr defName opts && isAttrs optValue
|
2009-02-09 09:51:03 -07:00
|
|
|
|
then addDefaultOptionValues defValue optValue
|
|
|
|
|
else addDefaultOptionValues defValue {};
|
|
|
|
|
}
|
2009-05-19 08:54:41 -06:00
|
|
|
|
) (attrNames defs));
|
2009-02-09 09:51:03 -07:00
|
|
|
|
|
|
|
|
|
mergeDefaultOption = list:
|
2012-08-13 12:19:31 -06:00
|
|
|
|
if length list == 1 then head list
|
2009-05-19 08:54:41 -06:00
|
|
|
|
else if all builtins.isFunction list then x: mergeDefaultOption (map (f: f x) list)
|
|
|
|
|
else if all isList list then concatLists list
|
|
|
|
|
else if all isAttrs list then fold lib.mergeAttrs {} list
|
2010-03-11 15:03:45 -07:00
|
|
|
|
else if all builtins.isBool list then fold lib.or false list
|
|
|
|
|
else if all builtins.isString list then lib.concatStrings list
|
2010-03-11 15:03:49 -07:00
|
|
|
|
else if all builtins.isInt list && all (x: x == head list) list
|
|
|
|
|
then head list
|
2009-02-09 09:51:03 -07:00
|
|
|
|
else throw "Cannot merge values.";
|
|
|
|
|
|
2013-10-28 09:14:15 -06:00
|
|
|
|
|
|
|
|
|
/* Obsolete, will remove soon. Specify an option type or apply
|
|
|
|
|
function instead. */
|
2009-02-09 09:51:03 -07:00
|
|
|
|
mergeTypedOption = typeName: predicate: merge: list:
|
|
|
|
|
if all predicate list then merge list
|
|
|
|
|
else throw "Expect a ${typeName}.";
|
|
|
|
|
|
|
|
|
|
mergeEnableOption = mergeTypedOption "boolean"
|
|
|
|
|
(x: true == x || false == x) (fold lib.or false);
|
|
|
|
|
|
2009-05-19 08:54:41 -06:00
|
|
|
|
mergeListOption = mergeTypedOption "list" isList concatLists;
|
2009-02-09 09:51:03 -07:00
|
|
|
|
|
|
|
|
|
mergeStringOption = mergeTypedOption "string"
|
|
|
|
|
(x: if builtins ? isString then builtins.isString x else x + "")
|
|
|
|
|
lib.concatStrings;
|
|
|
|
|
|
|
|
|
|
mergeOneOption = list:
|
2009-05-19 08:54:41 -06:00
|
|
|
|
if list == [] then abort "This case should never happen."
|
2012-08-13 12:19:31 -06:00
|
|
|
|
else if length list != 1 then throw "Multiple definitions. Only one is allowed for this option."
|
2009-02-09 09:51:03 -07:00
|
|
|
|
else head list;
|
|
|
|
|
|
|
|
|
|
|
2009-06-11 10:03:38 -06:00
|
|
|
|
# Generate documentation template from the list of option declaration like
|
|
|
|
|
# the set generated with filterOptionSets.
|
2013-10-28 07:25:58 -06:00
|
|
|
|
optionAttrSetToDocList = optionAttrSetToDocList' [];
|
|
|
|
|
|
|
|
|
|
optionAttrSetToDocList' = prefix: options:
|
|
|
|
|
fold (opt: rest:
|
|
|
|
|
let
|
|
|
|
|
docOption = rec {
|
|
|
|
|
name = showOption opt.loc;
|
|
|
|
|
description = opt.description or (throw "Option `${name}' has no description.");
|
|
|
|
|
declarations = filter (x: x != unknownModule) opt.declarations;
|
|
|
|
|
internal = opt.internal or false;
|
|
|
|
|
visible = opt.visible or true;
|
|
|
|
|
}
|
|
|
|
|
// optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; }
|
|
|
|
|
// optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; }
|
|
|
|
|
// optionalAttrs (opt ? defaultText) { default = opt.defaultText; };
|
|
|
|
|
|
|
|
|
|
subOptions =
|
|
|
|
|
let ss = opt.type.getSubOptions opt.loc;
|
|
|
|
|
in if ss != {} then optionAttrSetToDocList' opt.loc ss else [];
|
|
|
|
|
in
|
|
|
|
|
# FIXME: expensive, O(n^2)
|
|
|
|
|
[ docOption ] ++ subOptions ++ rest) [] (collect isOption options);
|
2009-06-11 10:03:38 -06:00
|
|
|
|
|
2009-02-09 09:51:03 -07:00
|
|
|
|
|
2010-05-12 05:07:49 -06:00
|
|
|
|
/* This function recursively removes all derivation attributes from
|
|
|
|
|
`x' except for the `name' attribute. This is to make the
|
|
|
|
|
generation of `options.xml' much more efficient: the XML
|
|
|
|
|
representation of derivations is very large (on the order of
|
|
|
|
|
megabytes) and is not actually used by the manual generator. */
|
2013-10-17 06:29:51 -06:00
|
|
|
|
scrubOptionValue = x:
|
2010-05-12 07:24:09 -06:00
|
|
|
|
if isDerivation x then { type = "derivation"; drvPath = x.name; outPath = x.name; name = x.name; }
|
2010-05-12 05:07:49 -06:00
|
|
|
|
else if isList x then map scrubOptionValue x
|
2010-06-01 08:24:16 -06:00
|
|
|
|
else if isAttrs x then mapAttrs (n: v: scrubOptionValue v) (removeAttrs x ["_args"])
|
2010-05-12 05:07:49 -06:00
|
|
|
|
else x;
|
|
|
|
|
|
2011-09-05 04:14:24 -06:00
|
|
|
|
|
|
|
|
|
/* For use in the ‘example’ option attribute. It causes the given
|
|
|
|
|
text to be included verbatim in documentation. This is necessary
|
|
|
|
|
for example values that are not simple values, e.g.,
|
|
|
|
|
functions. */
|
|
|
|
|
literalExample = text: { _type = "literalExample"; inherit text; };
|
|
|
|
|
|
|
|
|
|
|
2013-10-27 17:56:22 -06:00
|
|
|
|
/* Helper functions. */
|
|
|
|
|
showOption = concatStringsSep ".";
|
2013-10-28 07:25:58 -06:00
|
|
|
|
unknownModule = "<unknown-file>";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
|
2009-05-24 04:57:41 -06:00
|
|
|
|
}
|