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
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
# name (name of the type)
|
2013-10-27 17:56:22 -06:00
|
|
|
|
# check (check the config value)
|
2009-06-26 07:53:31 -06:00
|
|
|
|
# merge (default merge function)
|
2013-10-28 07:25:58 -06:00
|
|
|
|
# getSubOptions (returns sub-options for manual generation)
|
2013-03-13 08:05:30 -06:00
|
|
|
|
isOptionType = isType "option-type";
|
2009-06-26 07:53:31 -06:00
|
|
|
|
mkOptionType =
|
|
|
|
|
{ name
|
|
|
|
|
, check ? (x: true)
|
|
|
|
|
, merge ? mergeDefaultOption
|
2013-10-27 17:56:22 -06:00
|
|
|
|
, merge' ? args: merge
|
2013-10-28 07:25:58 -06:00
|
|
|
|
, getSubOptions ? prefix: {}
|
2009-06-26 07:53:31 -06:00
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
{ _type = "option-type";
|
2013-10-28 07:25:58 -06:00
|
|
|
|
inherit name check merge merge' getSubOptions;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
2011-06-13 20:41:13 -06:00
|
|
|
|
|
2013-10-28 08:13:51 -06:00
|
|
|
|
addToPrefix = args: name: args // { prefix = args.prefix ++ [name]; };
|
|
|
|
|
|
|
|
|
|
|
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;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
merge = fold lib.or false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int = mkOptionType {
|
|
|
|
|
name = "integer";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isInt;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string = mkOptionType {
|
|
|
|
|
name = "string";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = builtins.isString;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
merge = lib.concatStrings;
|
|
|
|
|
};
|
|
|
|
|
|
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-02-11 07:28:41 -07:00
|
|
|
|
merge = lib.concatStringsSep "\n";
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-28 09:14:15 -06:00
|
|
|
|
commas = mkOptionType {
|
|
|
|
|
name = "string";
|
|
|
|
|
check = builtins.isString;
|
|
|
|
|
merge = lib.concatStringsSep ",";
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-06 18:58:56 -07:00
|
|
|
|
envVar = mkOptionType {
|
|
|
|
|
name = "environment variable";
|
|
|
|
|
inherit (string) check;
|
|
|
|
|
merge = lib.concatStringsSep ":";
|
|
|
|
|
};
|
|
|
|
|
|
2009-06-26 07:53:31 -06:00
|
|
|
|
attrs = mkOptionType {
|
|
|
|
|
name = "attribute set";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = isAttrs;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
merge = fold lib.mergeAttrs {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
# derivation is a reserved keyword.
|
|
|
|
|
package = mkOptionType {
|
|
|
|
|
name = "derivation";
|
2013-10-27 17:56:22 -06:00
|
|
|
|
check = isDerivation;
|
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)) == "/";
|
2012-05-25 11:19:07 -06:00
|
|
|
|
};
|
|
|
|
|
|
2013-08-22 00:45:22 -06:00
|
|
|
|
# drop this in the future:
|
2013-10-27 17:56:22 -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 08:13:51 -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 08:13:51 -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 08:13:51 -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;
|
2009-06-26 07:53:31 -06:00
|
|
|
|
merge = list:
|
2012-08-13 12:19:31 -06:00
|
|
|
|
if length list == 1 then
|
2009-06-26 07:53:31 -06:00
|
|
|
|
head list
|
|
|
|
|
else
|
2013-08-22 00:50:25 -06:00
|
|
|
|
throw "Multiple definitions of ${elemType.name}. Only one is allowed for this option.";
|
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;
|
2009-11-05 08:39:45 -07:00
|
|
|
|
merge = list:
|
|
|
|
|
throw "No definitions are allowed for this option.";
|
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 08:13:51 -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
|
|
|
|
|
throw "Some but not all values are null."
|
2013-10-28 08:13:51 -06:00
|
|
|
|
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 08:13:51 -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;
|
|
|
|
|
merge = merge' {};
|
|
|
|
|
merge' = args: defs:
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|