2009-06-26 07:53:31 -06:00
|
|
|
|
# Definitions related to run-time type checking. Used in particular
|
|
|
|
|
# to type-check NixOS configurations.
|
|
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
2013-10-27 17:56:22 -06:00
|
|
|
|
with lib.lists;
|
|
|
|
|
with lib.attrsets;
|
|
|
|
|
with lib.options;
|
|
|
|
|
with lib.trivial;
|
|
|
|
|
with lib.modules;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
2013-03-13 08:05:30 -06:00
|
|
|
|
isType = type: x: (x._type or "") == type;
|
2012-12-31 11:59:30 -07:00
|
|
|
|
typeOf = x: x._type or "";
|
2009-06-26 07:53:31 -06:00
|
|
|
|
|
2009-11-19 10:19:39 -07:00
|
|
|
|
setType = typeName: value: value // {
|
|
|
|
|
_type = typeName;
|
|
|
|
|
};
|
|
|
|
|
|
2011-06-13 20:41:13 -06:00
|
|
|
|
|
2013-03-13 08:05:30 -06:00
|
|
|
|
isOptionType = isType "option-type";
|
2009-06-26 07:53:31 -06:00
|
|
|
|
mkOptionType =
|
2013-10-28 12:48:30 -06: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 07:53:31 -06:00
|
|
|
|
}:
|
|
|
|
|
{ _type = "option-type";
|
2013-10-28 12:48:30 -06:00
|
|
|
|
inherit name check merge getSubOptions;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-13 20:41:13 -06:00
|
|
|
|
|
2009-11-06 18:58:56 -07:00
|
|
|
|
types = rec {
|
2009-06-26 07:53:31 -06:00
|
|
|
|
|
2013-10-27 17:56:22 -06:00
|
|
|
|
unspecified = mkOptionType {
|
|
|
|
|
name = "unspecified";
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
bool = mkOptionType {
|
|
|
|
|
name = "boolean";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isBool;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: fold lib.or false;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int = mkOptionType {
|
|
|
|
|
name = "integer";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isInt;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-29 06:45:30 -06:00
|
|
|
|
str = mkOptionType {
|
|
|
|
|
name = "string";
|
|
|
|
|
check = builtins.isString;
|
|
|
|
|
merge = mergeOneOption;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# Deprecated; should not be used because it quietly concatenates
|
|
|
|
|
# strings, which is usually not what you want.
|
2009-06-26 07:53:31 -06:00
|
|
|
|
string = mkOptionType {
|
|
|
|
|
name = "string";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isString;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: lib.concatStrings;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2013-02-11 07:28:41 -07:00
|
|
|
|
# Like ‘string’, but add newlines between every value. Useful for
|
|
|
|
|
# configuration file contents.
|
|
|
|
|
lines = mkOptionType {
|
|
|
|
|
name = "string";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isString;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: lib.concatStringsSep "\n";
|
2013-02-11 07:28:41 -07:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 09:14:15 -06:00
|
|
|
|
commas = mkOptionType {
|
|
|
|
|
name = "string";
|
|
|
|
|
check = builtins.isString;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: lib.concatStringsSep ",";
|
2013-10-28 09:14:15 -06:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 18:58:56 -07:00
|
|
|
|
envVar = mkOptionType {
|
|
|
|
|
name = "environment variable";
|
|
|
|
|
inherit (string) check;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: lib.concatStringsSep ":";
|
2009-11-06 18:58:56 -07:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
attrs = mkOptionType {
|
|
|
|
|
name = "attribute set";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = isAttrs;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: fold lib.mergeAttrs {};
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# derivation is a reserved keyword.
|
|
|
|
|
package = mkOptionType {
|
|
|
|
|
name = "derivation";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = isDerivation;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = mergeOneOption;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2012-05-25 11:19:07 -06:00
|
|
|
|
path = mkOptionType {
|
|
|
|
|
name = "path";
|
|
|
|
|
# Hacky: there is no ‘isPath’ primop.
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = x: builtins.unsafeDiscardStringContext (builtins.substring 0 1 (toString x)) == "/";
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = mergeOneOption;
|
2012-05-25 11:19:07 -06:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-22 00:45:22 -06:00
|
|
|
|
# drop this in the future:
|
2013-10-28 12:48:30 -06:00
|
|
|
|
list = builtins.trace "`types.list' is deprecated; use `types.listOf' instead" types.listOf;
|
2013-08-22 00:45:22 -06:00
|
|
|
|
|
2013-10-27 17:56:22 -06:00
|
|
|
|
listOf = elemType: mkOptionType {
|
2009-06-26 07:53:31 -06:00
|
|
|
|
name = "list of ${elemType.name}s";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = value: isList value && all elemType.check value;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: defs: imap (n: def: elemType.merge (addToPrefix args (toString n)) [def]) (concatLists defs);
|
2013-10-28 07:25:58 -06:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
attrsOf = elemType: mkOptionType {
|
2011-04-27 12:41:27 -06:00
|
|
|
|
name = "attribute set of ${elemType.name}s";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = x: isAttrs x && all elemType.check (lib.attrValues x);
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: lib.zipAttrsWith (name:
|
|
|
|
|
elemType.merge (addToPrefix (args // { inherit name; }) name));
|
2013-10-28 07:25:58 -06:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]);
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-13 20:41:13 -06: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 00:51:46 -06:00
|
|
|
|
{ name = "unnamed-${toString defIdx}.${toString elemIdx}"; value = elem; }))
|
2011-06-13 20:41:13 -06: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-27 17:56:22 -06:00
|
|
|
|
else false;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: defs: attrOnly.merge args (imap convertIfList defs);
|
2013-10-28 07:25:58 -06:00
|
|
|
|
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
|
2013-10-27 17:56:22 -06:00
|
|
|
|
};
|
2011-06-13 20:41:13 -06:00
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
uniq = elemType: mkOptionType {
|
2013-10-28 07:25:58 -06:00
|
|
|
|
inherit (elemType) name check;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = mergeOneOption;
|
2013-10-28 07:25:58 -06:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-05 08:39:45 -07:00
|
|
|
|
none = elemType: mkOptionType {
|
2013-10-28 07:25:58 -06:00
|
|
|
|
inherit (elemType) name check;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: list:
|
|
|
|
|
throw "No definitions are allowed for the option `${showOption args.prefix}'.";
|
2013-10-28 07:25:58 -06:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-11-05 08:39:45 -07:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
nullOr = elemType: mkOptionType {
|
2013-10-27 17:56:22 -06:00
|
|
|
|
name = "null or ${elemType.name}";
|
2009-06-26 07:53:31 -06:00
|
|
|
|
check = x: builtins.isNull x || elemType.check x;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: defs:
|
2013-10-27 17:56:22 -06:00
|
|
|
|
if all isNull defs then null
|
|
|
|
|
else if any isNull defs then
|
2013-10-28 12:48:30 -06: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 07:25:58 -06:00
|
|
|
|
getSubOptions = elemType.getSubOptions;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2012-12-19 20:49:21 -07:00
|
|
|
|
functionTo = elemType: mkOptionType {
|
|
|
|
|
name = "function that evaluates to a(n) ${elemType.name}";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isFunction;
|
2013-10-28 12:48:30 -06:00
|
|
|
|
merge = args: fns:
|
|
|
|
|
fnArgs: elemType.merge args (map (fn: fn fnArgs) fns);
|
2013-10-28 07:25:58 -06: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 12:48:30 -06:00
|
|
|
|
merge = args: defs:
|
2013-10-28 07:25:58 -06:00
|
|
|
|
let
|
|
|
|
|
coerce = def: if builtins.isFunction def then def else { config = def; };
|
|
|
|
|
modules = opts' ++ map coerce defs;
|
2013-10-28 08:48:20 -06: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 07:25:58 -06:00
|
|
|
|
};
|
2013-10-27 17:56:22 -06: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 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 12:48:30 -06:00
|
|
|
|
|
|
|
|
|
/* Helper function. */
|
|
|
|
|
addToPrefix = args: name: args // { prefix = args.prefix ++ [name]; };
|
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
}
|