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