nixpkgs-suyu/lib/types.nix

202 lines
5.8 KiB
Nix
Raw Normal View History

# Definitions related to run-time type checking. Used in particular
# to type-check NixOS configurations.
let lib = import ./default.nix; in
with lib.lists;
with lib.attrsets;
with lib.options;
with lib.trivial;
with lib.modules;
rec {
isType = type: x: (x._type or "") == type;
2012-12-31 19:59:30 +01:00
typeOf = x: x._type or "";
setType = typeName: value: value // {
_type = typeName;
};
# name (name of the type)
# check (check the config value)
# merge (default merge function)
2013-10-28 14:25:58 +01:00
# getSubOptions (returns sub-options for manual generation)
isOptionType = isType "option-type";
mkOptionType =
{ name
, check ? (x: true)
, merge ? mergeDefaultOption
, merge' ? args: merge
2013-10-28 14:25:58 +01:00
, getSubOptions ? prefix: {}
}:
{ _type = "option-type";
2013-10-28 14:25:58 +01:00
inherit name check merge merge' getSubOptions;
};
2013-10-28 15:13:51 +01:00
addToPrefix = args: name: args // { prefix = args.prefix ++ [name]; };
types = rec {
unspecified = mkOptionType {
name = "unspecified";
};
bool = mkOptionType {
name = "boolean";
check = builtins.isBool;
merge = fold lib.or false;
};
int = mkOptionType {
name = "integer";
check = builtins.isInt;
};
string = mkOptionType {
name = "string";
check = builtins.isString;
merge = lib.concatStrings;
};
# Like string, but add newlines between every value. Useful for
# configuration file contents.
lines = mkOptionType {
name = "string";
check = builtins.isString;
merge = lib.concatStringsSep "\n";
};
envVar = mkOptionType {
name = "environment variable";
inherit (string) check;
merge = lib.concatStringsSep ":";
};
attrs = mkOptionType {
name = "attribute set";
check = isAttrs;
merge = fold lib.mergeAttrs {};
};
# derivation is a reserved keyword.
package = mkOptionType {
name = "derivation";
check = isDerivation;
};
path = mkOptionType {
name = "path";
# Hacky: there is no isPath primop.
check = x: builtins.unsafeDiscardStringContext (builtins.substring 0 1 (toString x)) == "/";
};
# drop this in the future:
list = builtins.trace "types.list is deprecated; use types.listOf instead" types.listOf;
listOf = elemType: mkOptionType {
name = "list of ${elemType.name}s";
check = value: isList value && all elemType.check value;
2013-10-28 15:13:51 +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 ++ ["*"]);
};
attrsOf = elemType: mkOptionType {
name = "attribute set of ${elemType.name}s";
check = x: isAttrs x && all elemType.check (lib.attrValues x);
2013-10-28 15:13:51 +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>"]);
};
# List or attribute set of ...
loaOf = elemType:
let
convertIfList = defIdx: def:
if isList def then
listToAttrs (
flip imap def (elemIdx: elem:
{ name = "unnamed-${toString defIdx}.${toString elemIdx}"; value = 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
else false;
2013-10-28 15:13:51 +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?>"]);
};
uniq = elemType: mkOptionType {
2013-10-28 14:25:58 +01:00
inherit (elemType) name check;
merge = list:
2012-08-13 20:19:31 +02:00
if length list == 1 then
head list
else
throw "Multiple definitions of ${elemType.name}. Only one is allowed for this option.";
2013-10-28 14:25:58 +01:00
getSubOptions = elemType.getSubOptions;
};
none = elemType: mkOptionType {
2013-10-28 14:25:58 +01:00
inherit (elemType) name check;
merge = list:
throw "No definitions are allowed for this option.";
2013-10-28 14:25:58 +01:00
getSubOptions = elemType.getSubOptions;
};
nullOr = elemType: mkOptionType {
name = "null or ${elemType.name}";
check = x: builtins.isNull x || elemType.check x;
2013-10-28 15:13:51 +01:00
merge' = args: defs:
if all isNull defs then null
else if any isNull defs then
throw "Some but not all values are null."
2013-10-28 15:13:51 +01:00
else elemType.merge' args defs;
2013-10-28 14:25:58 +01:00
getSubOptions = elemType.getSubOptions;
};
functionTo = elemType: mkOptionType {
name = "function that evaluates to a(n) ${elemType.name}";
check = builtins.isFunction;
2013-10-28 15:13:51 +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;
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 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
};
# 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";
};
};
}