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;
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-30 14:21:41 +01:00
|
|
|
|
mergeDefaultOption = loc: defs:
|
|
|
|
|
let list = getValues defs; in
|
2012-08-13 20:19:31 +02:00
|
|
|
|
if length list == 1 then head list
|
2013-10-30 14:21:41 +01:00
|
|
|
|
else if all builtins.isFunction list then x: mergeDefaultOption loc (map (f: f x) list)
|
2009-05-19 16:54:41 +02:00
|
|
|
|
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
|
2013-10-28 19:48:30 +01:00
|
|
|
|
else if all builtins.isInt list && all (x: x == head list) list then head list
|
2013-10-30 14:21:41 +01:00
|
|
|
|
else throw "Cannot merge definitions of `${showOption loc}' given in ${showFiles (getFiles defs)}.";
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
2013-10-28 16:14:15 +01:00
|
|
|
|
/* Obsolete, will remove soon. Specify an option type or apply
|
|
|
|
|
function instead. */
|
2013-10-30 14:21:41 +01:00
|
|
|
|
mergeTypedOption = typeName: predicate: merge: loc: list:
|
|
|
|
|
let list' = map (x: x.value) list; in
|
|
|
|
|
if all predicate list then merge list'
|
|
|
|
|
else throw "Expected a ${typeName}.";
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-10-30 14:21:41 +01:00
|
|
|
|
mergeStringOption = mergeTypedOption "string" builtins.isString lib.concatStrings;
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
2013-10-30 14:21:41 +01:00
|
|
|
|
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, in ${showFiles (getFiles defs)}."
|
|
|
|
|
else (head defs).value;
|
|
|
|
|
|
|
|
|
|
getValues = map (x: x.value);
|
|
|
|
|
getFiles = map (x: x.file);
|
2009-02-09 17:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
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 19:48:30 +01:00
|
|
|
|
showFiles = files: concatStringsSep " and " (map (f: "`${f}'") files);
|
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
|
|
|
|
}
|