2009-02-09 16:51:03 +00:00
|
|
|
|
# Nixpkgs/NixOS option handling.
|
|
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
2009-02-28 18:21:25 +00:00
|
|
|
|
with import ./trivial.nix;
|
2009-02-09 16:51:03 +00:00
|
|
|
|
with import ./lists.nix;
|
2009-02-28 18:21:25 +00:00
|
|
|
|
with import ./misc.nix;
|
2009-02-09 16:51:03 +00:00
|
|
|
|
with import ./attrsets.nix;
|
2013-10-28 00:14:16 +00:00
|
|
|
|
with import ./strings.nix;
|
2009-02-09 16:51:03 +00:00
|
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
2013-10-23 17:20:39 +01:00
|
|
|
|
isOption = lib.isType "option";
|
2009-05-27 21:25:17 +01: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 20:13:42 +01:00
|
|
|
|
mkEnableOption = name: mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
example = true;
|
2013-10-17 13:29:51 +01:00
|
|
|
|
description = "Whether to enable ${name}.";
|
2013-07-18 20:13:42 +01:00
|
|
|
|
type = lib.types.bool;
|
|
|
|
|
};
|
|
|
|
|
|
2009-05-27 21:25:17 +01:00
|
|
|
|
# !!! This function will be removed because this can be done with the
|
|
|
|
|
# multiple option declarations.
|
2009-02-09 16:51:03 +00: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 14:05:30 +00:00
|
|
|
|
if isOption defValue
|
2009-02-09 16:51:03 +00:00
|
|
|
|
then
|
|
|
|
|
# `defValue' is an option.
|
2009-05-19 15:54:41 +01:00
|
|
|
|
if hasAttr defName opts
|
2009-02-09 16:51:03 +00:00
|
|
|
|
then builtins.getAttr defName opts
|
|
|
|
|
else defValue.default
|
|
|
|
|
else
|
|
|
|
|
# `defValue' is an attribute set containing options.
|
|
|
|
|
# So recurse.
|
2009-05-19 15:54:41 +01:00
|
|
|
|
if hasAttr defName opts && isAttrs optValue
|
2009-02-09 16:51:03 +00:00
|
|
|
|
then addDefaultOptionValues defValue optValue
|
|
|
|
|
else addDefaultOptionValues defValue {};
|
|
|
|
|
}
|
2009-05-19 15:54:41 +01:00
|
|
|
|
) (attrNames defs));
|
2009-02-09 16:51:03 +00:00
|
|
|
|
|
|
|
|
|
mergeDefaultOption = list:
|
2012-08-13 19:19:31 +01:00
|
|
|
|
if length list == 1 then head list
|
2009-05-19 15:54:41 +01: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 22:03:45 +00: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 22:03:49 +00:00
|
|
|
|
else if all builtins.isInt list && all (x: x == head list) list
|
|
|
|
|
then head list
|
2009-02-09 16:51:03 +00:00
|
|
|
|
else throw "Cannot merge values.";
|
|
|
|
|
|
|
|
|
|
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 15:54:41 +01:00
|
|
|
|
mergeListOption = mergeTypedOption "list" isList concatLists;
|
2009-02-09 16:51:03 +00:00
|
|
|
|
|
|
|
|
|
mergeStringOption = mergeTypedOption "string"
|
|
|
|
|
(x: if builtins ? isString then builtins.isString x else x + "")
|
|
|
|
|
lib.concatStrings;
|
|
|
|
|
|
|
|
|
|
mergeOneOption = list:
|
2009-05-19 15:54:41 +01:00
|
|
|
|
if list == [] then abort "This case should never happen."
|
2012-08-13 19:19:31 +01:00
|
|
|
|
else if length list != 1 then throw "Multiple definitions. Only one is allowed for this option."
|
2009-02-09 16:51:03 +00:00
|
|
|
|
else head list;
|
|
|
|
|
|
|
|
|
|
|
2009-06-11 17:03:38 +01:00
|
|
|
|
# Generate documentation template from the list of option declaration like
|
|
|
|
|
# the set generated with filterOptionSets.
|
2013-10-17 13:09:05 +01:00
|
|
|
|
optionAttrSetToDocList = attrs:
|
2009-06-11 17:03:38 +01:00
|
|
|
|
let options = collect isOption attrs; in
|
|
|
|
|
fold (opt: rest:
|
|
|
|
|
let
|
|
|
|
|
docOption = {
|
|
|
|
|
inherit (opt) name;
|
2013-10-17 13:29:51 +01:00
|
|
|
|
description = opt.description or (throw "Option ${opt.name}: No description.");
|
2009-09-18 16:10:11 +01:00
|
|
|
|
declarations = map (x: toString x.source) opt.declarations;
|
2010-08-11 12:46:09 +01:00
|
|
|
|
#definitions = map (x: toString x.source) opt.definitions;
|
2013-10-23 15:59:33 +01:00
|
|
|
|
internal = opt.internal or false;
|
2013-10-23 16:03:46 +01:00
|
|
|
|
visible = opt.visible or true;
|
2009-06-11 17:03:38 +01:00
|
|
|
|
}
|
2010-05-12 12:07:49 +01:00
|
|
|
|
// optionalAttrs (opt ? example) { example = scrubOptionValue opt.example; }
|
2010-05-28 12:20:45 +01:00
|
|
|
|
// optionalAttrs (opt ? default) { default = scrubOptionValue opt.default; }
|
|
|
|
|
// optionalAttrs (opt ? defaultText) { default = opt.defaultText; };
|
2009-06-11 17:03:38 +01:00
|
|
|
|
|
|
|
|
|
subOptions =
|
|
|
|
|
if opt ? options then
|
2013-10-17 13:09:05 +01:00
|
|
|
|
optionAttrSetToDocList opt.options
|
2009-06-11 17:03:38 +01:00
|
|
|
|
else
|
|
|
|
|
[];
|
|
|
|
|
in
|
2013-10-27 23:56:22 +00:00
|
|
|
|
# FIXME: expensive (O(n^2)
|
2009-06-11 17:03:38 +01:00
|
|
|
|
[ docOption ] ++ subOptions ++ rest
|
|
|
|
|
) [] options;
|
|
|
|
|
|
2009-02-09 16:51:03 +00:00
|
|
|
|
|
2010-05-12 12:07:49 +01: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 13:29:51 +01:00
|
|
|
|
scrubOptionValue = x:
|
2010-05-12 14:24:09 +01:00
|
|
|
|
if isDerivation x then { type = "derivation"; drvPath = x.name; outPath = x.name; name = x.name; }
|
2010-05-12 12:07:49 +01:00
|
|
|
|
else if isList x then map scrubOptionValue x
|
2010-06-01 15:24:16 +01:00
|
|
|
|
else if isAttrs x then mapAttrs (n: v: scrubOptionValue v) (removeAttrs x ["_args"])
|
2010-05-12 12:07:49 +01:00
|
|
|
|
else x;
|
|
|
|
|
|
2011-09-05 11:14:24 +01: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 23:56:22 +00:00
|
|
|
|
/* Helper functions. */
|
|
|
|
|
showOption = concatStringsSep ".";
|
|
|
|
|
|
2009-05-24 11:57:41 +01:00
|
|
|
|
}
|