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;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
hasType = x: isAttrs x && x ? _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
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
# name (name of the type)
|
2013-10-28 00:56:22 +01:00
|
|
|
|
# check (check the config value)
|
2009-06-26 15:53:31 +02:00
|
|
|
|
# merge (default merge function)
|
2009-10-10 01:03:24 +02:00
|
|
|
|
# docPath (path concatenated to the option name contained in the option set)
|
2013-03-13 15:05:30 +01:00
|
|
|
|
isOptionType = isType "option-type";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
mkOptionType =
|
|
|
|
|
{ name
|
|
|
|
|
, check ? (x: true)
|
|
|
|
|
, merge ? mergeDefaultOption
|
2013-10-28 00:56:22 +01:00
|
|
|
|
, merge' ? args: merge
|
2009-06-26 15:53:31 +02:00
|
|
|
|
, docPath ? lib.id
|
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
{ _type = "option-type";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
inherit name check merge merge' docPath;
|
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;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
merge = fold lib.or false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
merge = lib.concatStrings;
|
|
|
|
|
};
|
|
|
|
|
|
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-02-11 15:28:41 +01:00
|
|
|
|
merge = lib.concatStringsSep "\n";
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-07 02:58:56 +01:00
|
|
|
|
envVar = mkOptionType {
|
|
|
|
|
name = "environment variable";
|
|
|
|
|
inherit (string) check;
|
|
|
|
|
merge = lib.concatStringsSep ":";
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
attrs = mkOptionType {
|
|
|
|
|
name = "attribute set";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = isAttrs;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
merge = fold lib.mergeAttrs {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# derivation is a reserved keyword.
|
|
|
|
|
package = mkOptionType {
|
|
|
|
|
name = "derivation";
|
2013-10-28 00:56:22 +01:00
|
|
|
|
check = isDerivation;
|
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)) == "/";
|
2012-05-25 19:19:07 +02:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-22 08:45:22 +02:00
|
|
|
|
# drop this in the future:
|
2013-10-28 00:56:22 +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;
|
|
|
|
|
merge = defs: map (def: elemType.merge [def]) (concatLists defs);
|
2009-06-26 15:53:31 +02:00
|
|
|
|
docPath = path: elemType.docPath (path + ".*");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
merge = lib.zipAttrsWith (name: elemType.merge' { inherit name; });
|
2009-06-26 15:53:31 +02:00
|
|
|
|
docPath = path: elemType.docPath (path + ".<name>");
|
|
|
|
|
};
|
|
|
|
|
|
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:
|
|
|
|
|
nameValuePair "unnamed-${toString defIdx}.${toString elemIdx}" elem))
|
|
|
|
|
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;
|
|
|
|
|
merge = defs: attrOnly.merge (imap convertIfList defs);
|
2011-06-14 04:41:13 +02:00
|
|
|
|
docPath = path: elemType.docPath (path + ".<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 00:56:22 +01:00
|
|
|
|
inherit (elemType) name check docPath;
|
2009-06-26 15:53:31 +02:00
|
|
|
|
merge = list:
|
2012-08-13 20:19:31 +02:00
|
|
|
|
if length list == 1 then
|
2009-06-26 15:53:31 +02:00
|
|
|
|
head list
|
|
|
|
|
else
|
2013-08-22 08:50:25 +02:00
|
|
|
|
throw "Multiple definitions of ${elemType.name}. Only one is allowed for this option.";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2009-11-05 16:39:45 +01:00
|
|
|
|
none = elemType: mkOptionType {
|
2013-10-28 00:56:22 +01:00
|
|
|
|
inherit (elemType) name check docPath;
|
2009-11-05 16:39:45 +01:00
|
|
|
|
merge = list:
|
|
|
|
|
throw "No definitions are allowed for this option.";
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 15:53:31 +02:00
|
|
|
|
nullOr = elemType: mkOptionType {
|
2013-10-28 00:56:22 +01:00
|
|
|
|
inherit (elemType) docPath;
|
|
|
|
|
name = "null or ${elemType.name}";
|
2009-06-26 15:53:31 +02:00
|
|
|
|
check = x: builtins.isNull x || elemType.check x;
|
2013-10-28 00:56:22 +01:00
|
|
|
|
merge = defs:
|
|
|
|
|
if all isNull defs then null
|
|
|
|
|
else if any isNull defs then
|
|
|
|
|
throw "Some but not all values are null."
|
|
|
|
|
else elemType.merge defs;
|
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;
|
2012-12-20 04:49:21 +01:00
|
|
|
|
merge = fns:
|
2012-12-20 06:52:51 +01:00
|
|
|
|
args: elemType.merge (map (fn: fn args) fns);
|
2013-10-28 00:56:22 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
submodule = opts: mkOptionType rec {
|
|
|
|
|
name = "submodule";
|
2009-11-07 13:15:39 +01:00
|
|
|
|
check = x: isAttrs x || builtins.isFunction x;
|
2013-10-28 00:56:22 +01:00
|
|
|
|
# FIXME: make error messages include the parent attrpath.
|
|
|
|
|
merge = merge' {};
|
|
|
|
|
merge' = args: defs:
|
|
|
|
|
let
|
|
|
|
|
coerce = def: if builtins.isFunction def then def else { config = def; };
|
|
|
|
|
modules = (toList opts) ++ map coerce defs;
|
|
|
|
|
in (evalModules modules args).config;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|