2009-06-26 15:53:31 +02:00
|
|
|
|
# Definitions related to run-time type checking. Used in particular
|
|
|
|
|
# to type-check NixOS configurations.
|
|
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
2013-10-28 00:56:22 +01:00
|
|
|
|
with lib.lists;
|
|
|
|
|
with lib.attrsets;
|
|
|
|
|
with lib.options;
|
|
|
|
|
with lib.trivial;
|
|
|
|
|
with lib.modules;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
2013-03-13 15:05:30 +01:00
|
|
|
|
isType = type: x: (x._type or "") == type;
|
2012-12-31 19:59:30 +01:00
|
|
|
|
typeOf = x: x._type or "";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
|
2009-11-19 18:19:39 +01:00
|
|
|
|
setType = typeName: value: value // {
|
|
|
|
|
_type = typeName;
|
|
|
|
|
};
|
|
|
|
|
|
2011-06-14 04:41:13 +02:00
|
|
|
|
|
2013-03-13 15:05:30 +01:00
|
|
|
|
isOptionType = isType "option-type";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
mkOptionType =
|
2013-10-28 19:48:30 +01:00
|
|
|
|
{ # Human-readable representation of the type.
|
|
|
|
|
name
|
|
|
|
|
, # Function applied to each definition that should return true if
|
|
|
|
|
# its type-correct, false otherwise.
|
|
|
|
|
check ? (x: true)
|
|
|
|
|
, # Merge a list of definitions together into a single value.
|
|
|
|
|
merge ? mergeDefaultOption
|
|
|
|
|
, # Return a flat list of sub-options. Used to generate
|
|
|
|
|
# documentation.
|
|
|
|
|
getSubOptions ? prefix: {}
|
2009-06-26 15:53:31 +02:00
|
|
|
|
}:
|
|
|
|
|
{ _type = "option-type";
|
2013-10-28 19:48:30 +01:00
|
|
|
|
inherit name check merge getSubOptions;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-14 04:41:13 +02:00
|
|
|
|
|
2009-11-07 02:58:56 +01:00
|
|
|
|
types = rec {
|
2009-06-26 15:53:31 +02:00
|
|
|
|
|
2013-10-28 00:56:22 +01:00
|
|
|
|
unspecified = mkOptionType {
|
|
|
|
|
name = "unspecified";
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
bool = mkOptionType {
|
|
|
|
|
name = "boolean";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = builtins.isBool;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: fold lib.or false;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int = mkOptionType {
|
|
|
|
|
name = "integer";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = builtins.isInt;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string = mkOptionType {
|
|
|
|
|
name = "string";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = builtins.isString;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: lib.concatStrings;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2013-02-11 15:28:41 +01:00
|
|
|
|
# Like ‘string’, but add newlines between every value. Useful for
|
|
|
|
|
# configuration file contents.
|
|
|
|
|
lines = mkOptionType {
|
|
|
|
|
name = "string";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = builtins.isString;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: lib.concatStringsSep "\n";
|
2013-02-11 15:28:41 +01:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 16:14:15 +01:00
|
|
|
|
commas = mkOptionType {
|
|
|
|
|
name = "string";
|
|
|
|
|
check = builtins.isString;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: lib.concatStringsSep ",";
|
2013-10-28 16:14:15 +01:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-07 02:58:56 +01:00
|
|
|
|
envVar = mkOptionType {
|
|
|
|
|
name = "environment variable";
|
|
|
|
|
inherit (string) check;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: lib.concatStringsSep ":";
|
2009-11-07 02:58:56 +01:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
attrs = mkOptionType {
|
|
|
|
|
name = "attribute set";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = isAttrs;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: fold lib.mergeAttrs {};
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# derivation is a reserved keyword.
|
|
|
|
|
package = mkOptionType {
|
|
|
|
|
name = "derivation";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = isDerivation;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = mergeOneOption;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2012-05-25 19:19:07 +02:00
|
|
|
|
path = mkOptionType {
|
|
|
|
|
name = "path";
|
|
|
|
|
# Hacky: there is no ‘isPath’ primop.
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = x: builtins.unsafeDiscardStringContext (builtins.substring 0 1 (toString x)) == "/";
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = mergeOneOption;
|
2012-05-25 19:19:07 +02:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-22 08:45:22 +02:00
|
|
|
|
# drop this in the future:
|
2013-10-28 19:48:30 +01:00
|
|
|
|
list = builtins.trace "`types.list' is deprecated; use `types.listOf' instead" types.listOf;
|
2013-08-22 08:45:22 +02:00
|
|
|
|
|
2013-10-28 00:56:22 +01:00
|
|
|
|
listOf = elemType: mkOptionType {
|
2009-06-26 15:53:31 +02:00
|
|
|
|
name = "list of ${elemType.name}s";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = value: isList value && all elemType.check value;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: defs: imap (n: def: elemType.merge (addToPrefix args (toString n)) [def]) (concatLists defs);
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
attrsOf = elemType: mkOptionType {
|
2011-04-27 20:41:27 +02:00
|
|
|
|
name = "attribute set of ${elemType.name}s";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = x: isAttrs x && all elemType.check (lib.attrValues x);
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: lib.zipAttrsWith (name:
|
|
|
|
|
elemType.merge (addToPrefix (args // { inherit name; }) name));
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]);
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-14 04:41:13 +02:00
|
|
|
|
# List or attribute set of ...
|
|
|
|
|
loaOf = elemType:
|
|
|
|
|
let
|
|
|
|
|
convertIfList = defIdx: def:
|
|
|
|
|
if isList def then
|
|
|
|
|
listToAttrs (
|
|
|
|
|
flip imap def (elemIdx: elem:
|
2013-10-28 07:51:46 +01:00
|
|
|
|
{ name = "unnamed-${toString defIdx}.${toString elemIdx}"; value = elem; }))
|
2011-06-14 04:41:13 +02:00
|
|
|
|
else
|
|
|
|
|
def;
|
|
|
|
|
listOnly = listOf elemType;
|
|
|
|
|
attrOnly = attrsOf elemType;
|
|
|
|
|
|
|
|
|
|
in mkOptionType {
|
|
|
|
|
name = "list or attribute set of ${elemType.name}s";
|
|
|
|
|
check = x:
|
|
|
|
|
if isList x then listOnly.check x
|
|
|
|
|
else if isAttrs x then attrOnly.check x
|
2013-10-28 00:56:22 +01:00
|
|
|
|
else false;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: defs: attrOnly.merge args (imap convertIfList defs);
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
|
2013-10-28 00:56:22 +01:00
|
|
|
|
};
|
2011-06-14 04:41:13 +02:00
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
uniq = elemType: mkOptionType {
|
2013-10-28 14:25:58 +01:00
|
|
|
|
inherit (elemType) name check;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = mergeOneOption;
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-05 16:39:45 +01:00
|
|
|
|
none = elemType: mkOptionType {
|
2013-10-28 14:25:58 +01:00
|
|
|
|
inherit (elemType) name check;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: list:
|
|
|
|
|
throw "No definitions are allowed for the option `${showOption args.prefix}'.";
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-11-05 16:39:45 +01:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
nullOr = elemType: mkOptionType {
|
2013-10-28 00:56:22 +01:00
|
|
|
|
name = "null or ${elemType.name}";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
check = x: builtins.isNull x || elemType.check x;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: defs:
|
2013-10-28 00:56:22 +01:00
|
|
|
|
if all isNull defs then null
|
|
|
|
|
else if any isNull defs then
|
2013-10-28 19:48:30 +01:00
|
|
|
|
throw "The option `${showOption args.prefix}' is defined both null and not null, in ${showFiles args.files}."
|
|
|
|
|
else elemType.merge args defs;
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2012-12-20 04:49:21 +01:00
|
|
|
|
functionTo = elemType: mkOptionType {
|
|
|
|
|
name = "function that evaluates to a(n) ${elemType.name}";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = builtins.isFunction;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: fns:
|
|
|
|
|
fnArgs: elemType.merge args (map (fn: fn fnArgs) fns);
|
2013-10-28 14:25:58 +01:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submodule = opts:
|
|
|
|
|
let opts' = toList opts; in
|
|
|
|
|
mkOptionType rec {
|
|
|
|
|
name = "submodule";
|
|
|
|
|
check = x: isAttrs x || builtins.isFunction x;
|
2013-10-28 19:48:30 +01:00
|
|
|
|
merge = args: defs:
|
2013-10-28 14:25:58 +01:00
|
|
|
|
let
|
|
|
|
|
coerce = def: if builtins.isFunction def then def else { config = def; };
|
|
|
|
|
modules = opts' ++ map coerce defs;
|
2013-10-28 15:48:20 +01:00
|
|
|
|
in (evalModules { inherit modules args; prefix = args.prefix; }).config;
|
|
|
|
|
getSubOptions = prefix: (evalModules
|
|
|
|
|
{ modules = opts'; inherit prefix;
|
|
|
|
|
# FIXME: hack to get shit to evaluate.
|
|
|
|
|
args = { name = ""; }; }).options;
|
2013-10-28 14:25:58 +01:00
|
|
|
|
};
|
2013-10-28 00:56:22 +01:00
|
|
|
|
|
|
|
|
|
# Obsolete alternative to configOf. It takes its option
|
|
|
|
|
# declarations from the ‘options’ attribute of containing option
|
|
|
|
|
# declaration.
|
|
|
|
|
optionSet = mkOptionType {
|
|
|
|
|
name = /* builtins.trace "types.optionSet is deprecated; use types.submodule instead" */ "option set";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 19:48:30 +01:00
|
|
|
|
|
|
|
|
|
/* Helper function. */
|
|
|
|
|
addToPrefix = args: name: args // { prefix = args.prefix ++ [name]; };
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
}
|