@@ -1,8 +1,6 @@
11# Definitions related to run-time type checking. Used in particular
22# to type-check NixOS configurations.
33
4−let lib = import ./default.nix; in
5−
64with import ./lists.nix;
75with import ./attrsets.nix;
86with import ./options.nix;
@@ -23,8 +21,6 @@ rec {
2321 mkOptionType =
2422 { # Human-readable representation of the type.
2523 name
26− , # Parseable representation of the type.
27− typerep
2824 , # Function applied to each definition that should return true if
2925 # its type-correct, false otherwise.
3026 check ? (x: true)
@@ -35,59 +31,40 @@ rec {
3531 # definition values and locations (e.g. [ { file = "/foo.nix";
3632 # value = 1; } { file = "/bar.nix"; value = 2 } ]).
3733 merge ? mergeDefaultOption
38− , # Return list of sub-options.
39− getSubOptions ? {}
40− , # Same as 'getSubOptions', but with extra information about the
41− # location of the option which is used to generate documentation.
42− getSubOptionsPrefixed ? null
34+ , # Return a flat list of sub-options. Used to generate
35+ # documentation.
36+ getSubOptions ? prefix: {}
4337 , # List of modules if any, or null if none.
4438 getSubModules ? null
4539 , # Function for building the same option type with a different list of
4640 # modules.
4741 substSubModules ? m: null
48− , # List of type representations (typerep) of all the elementary types
49− # that are nested within the type. For an elementary type the list is
50− # a singleton of the typerep of itself.
51− # NOTE: Must be specified for every container type!
52− nestedTypes ? null
53− , # List of all default values, and an empty list if no default value exists.
54− defaultValues ? []
5542 }:
5643 { _type = "option-type";
57− inherit name typerep check merge getSubOptions getSubModules substSubModules defaultValues;
58− nestedTypes = if (isNull nestedTypes) then (singleton typerep) else nestedTypes;
59− getSubOptionsPrefixed = if (isNull getSubOptionsPrefixed) then (prefix: getSubOptions) else getSubOptionsPrefixed;
44+ inherit name check merge getSubOptions getSubModules substSubModules;
6045 };
6146
6247
6348 types = rec {
6449
65− #
66− # Elementary types
67− #
68−
6950 unspecified = mkOptionType {
7051 name = "unspecified";
71− typerep = "(unspecified)";
7252 };
7353
7454 bool = mkOptionType {
7555 name = "boolean";
76− typerep = "(boolean)";
7756 check = isBool;
7857 merge = mergeEqualOption;
7958 };
8059
8160 int = mkOptionType {
8261 name = "integer";
83− typerep = "(integer)";
8462 check = isInt;
8563 merge = mergeOneOption;
8664 };
8765
8866 str = mkOptionType {
8967 name = "string";
90− typerep = "(string)";
9168 check = isString;
9269 merge = mergeOneOption;
9370 };
@@ -95,111 +72,73 @@ rec {
9572 # Merge multiple definitions by concatenating them (with the given
9673 # separator between the values).
9774 separatedString = sep: mkOptionType {
98− name = "string" + (optionalString (sep != "") " separated by ${sep}");
99− typerep = "(separatedString(${escape ["(" ")"] sep}))";
75+ name = "string";
10076 check = isString;
101− merge = _module: loc: defs: concatStringsSep sep (getValues defs);
77+ merge = loc: defs: concatStringsSep sep (getValues defs);
10278 };
10379
104− lines = separatedString "\n" // { typerep = "(lines)"; };
105− commas = separatedString "," // { typerep = "(commas)"; };
106− envVar = separatedString ":" // { typerep = "(envVar)"; };
80+ lines = separatedString "\n";
81+ commas = separatedString ",";
82+ envVar = separatedString ":";
10783
10884 # Deprecated; should not be used because it quietly concatenates
10985 # strings, which is usually not what you want.
110− string = separatedString "" // { typerep = "(string)"; };
86+ string = separatedString "";
11187
11288 attrs = mkOptionType {
11389 name = "attribute set";
114− typerep = "(attrs)";
11590 check = isAttrs;
116− merge = _module: loc: foldl' (res: def: mergeAttrs res def.value) {};
91+ merge = loc: foldl' (res: def: mergeAttrs res def.value) {};
11792 };
11893
11994 # derivation is a reserved keyword.
12095 package = mkOptionType {
12196 name = "package";
122− typerep = "(package)";
12397 check = x: isDerivation x || isStorePath x;
124− merge = _module: loc: defs:
125− let res = mergeOneOption _module loc defs;
98+ merge = loc: defs:
99+ let res = mergeOneOption loc defs;
126100 in if isDerivation res then res else toDerivation res;
127101 };
128102
129− # The correct type of packageSet would be:
130− # packageSet = attrsOf (either package packageSet)
131− # (Not sure if nix would allow to define a recursive type.)
132− # However, currently it is not possible to check that a packageSet actually
133− # contains packages (that is derivations). The check 'isDerivation' is too
134− # eager for the current implementation of the assertion mechanism and of the
135− # licenses control mechanism. That means it is not generally possible to go
136− # into the attribute set of packages to check that every attribute would
137− # evaluate to a derivation if the package would actually be evaluated. Maybe
138− # that restriction can be lifted in the future, but for now the content of
139− # the packageSet is not checked.
140− # TODO: The 'merge' function is copied from 'mergeDefaultOption' to keep
141− # backwards compatibility with the 'unspecified' type that was used for
142− # package sets previously. Maybe review if the merge function has to change.
143− packageSet = mkOptionType {
144− name = "derivation set";
145− typerep = "(packageSet)";
146− check = isAttrs;
147− merge = _module: loc: defs: foldl' mergeAttrs {} (map (x: x.value) defs);
148− };
149−
150103 path = mkOptionType {
151104 name = "path";
152− typerep = "(path)";
153105 # Hacky: there is no ‘isPath’ primop.
154106 check = x: builtins.substring 0 1 (toString x) == "/";
155107 merge = mergeOneOption;
156108 };
157109
158−
159− #
160− # Container types
161− #
162−
163110 # drop this in the future:
164111 list = builtins.trace "`types.list' is deprecated; use `types.listOf' instead" types.listOf;
165112
166113 listOf = elemType: mkOptionType {
167114 name = "list of ${elemType.name}s";
168− typerep = "(listOf${elemType.typerep})";
169115 check = isList;
170− merge = _module: loc: defs:
116+ merge = loc: defs:
171117 map (x: x.value) (filter (x: x ? value) (concatLists (imap (n: def: imap (m: def':
172− (mergeDefinitions _module
118+ (mergeDefinitions
173119 (loc ++ ["[definition ${toString n}-entry ${toString m}]"])
174120 elemType
175121 [{ inherit (def) file; value = def'; }]
176122 ).optionalValue
177123 ) def.value) defs)));
178− getSubOptions = elemType.getSubOptions;
179− getSubOptionsPrefixed = prefix: elemType.getSubOptionsPrefixed (prefix ++ ["*"]);
124+ getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
180125 getSubModules = elemType.getSubModules;
181126 substSubModules = m: listOf (elemType.substSubModules m);
182− nestedTypes = elemType.nestedTypes;
183− defaultValues = [[]];
184127 };
185128
186129 attrsOf = elemType: mkOptionType {
187130 name = "attribute set of ${elemType.name}s";
188− typerep = "(attrsOf${elemType.typerep})";
189131 check = isAttrs;
190− merge = _module: loc: defs:
132+ merge = loc: defs:
191133 mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
192− (mergeDefinitions _module (loc ++ [name]) elemType defs).optionalValue
134+ (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue
193135 )
194136 # Push down position info.
195137 (map (def: listToAttrs (mapAttrsToList (n: def':
196138 { name = n; value = { inherit (def) file; value = def'; }; }) def.value)) defs)));
197− getSubOptions = elemType.getSubOptions;
198− getSubOptionsPrefixed = prefix: elemType.getSubOptionsPrefixed (prefix ++ ["<name>"]);
139+ getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]);
199140 getSubModules = elemType.getSubModules;
200141 substSubModules = m: attrsOf (elemType.substSubModules m);
201− nestedTypes = elemType.nestedTypes;
202− defaultValues = [{}];
203142 };
204143
205144 # List or attribute set of ...
@@ -220,23 +159,18 @@ rec {
220159 attrOnly = attrsOf elemType;
221160 in mkOptionType {
222161 name = "list or attribute set of ${elemType.name}s";
223− typerep = "(loaOf${elemType.typerep})";
224162 check = x: isList x || isAttrs x;
225− merge = _module: loc: defs: attrOnly.merge _module loc (imap convertIfList defs);
226− getSubOptions = elemType.getSubOptions;
227− getSubOptionsPrefixed = prefix: elemType.getSubOptionsPrefixed (prefix ++ ["<name?>"]);
163+ merge = loc: defs: attrOnly.merge loc (imap convertIfList defs);
164+ getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name?>"]);
228165 getSubModules = elemType.getSubModules;
229166 substSubModules = m: loaOf (elemType.substSubModules m);
230− nestedTypes = elemType.nestedTypes;
231− defaultValues = [{} []];
232167 };
233168
234169 # List or element of ...
235170 loeOf = elemType: mkOptionType {
236171 name = "element or list of ${elemType.name}s";
237− typerep = "(loeOf${elemType.typerep})";
238172 check = x: isList x || elemType.check x;
239− merge = _module: loc: defs:
173+ merge = loc: defs:
240174 let
241175 defs' = filterOverrides defs;
242176 res = (head defs').value;
@@ -247,136 +181,81 @@ rec {
247181 else if !isString res then
248182 throw "The option `${showOption loc}' does not have a string value, in ${showFiles (getFiles defs)}."
249183 else res;
250− nestedTypes = elemType.nestedTypes;
251− defaultValues = [[]] ++ elemType.defaultValues;
252184 };
253185
254186 uniq = elemType: mkOptionType {
255− inherit (elemType) check;
256− name = "unique ${elemType.name}";
257− typerep = "(uniq${elemType.typerep})";
187+ inherit (elemType) name check;
258188 merge = mergeOneOption;
259189 getSubOptions = elemType.getSubOptions;
260− getSubOptionsPrefixed = prefix: elemType.getSubOptionsPrefixed prefix;
261190 getSubModules = elemType.getSubModules;
262191 substSubModules = m: uniq (elemType.substSubModules m);
263− nestedTypes = elemType.nestedTypes;
264− defaultValues = elemType.defaultValues;
265192 };
266193
267194 nullOr = elemType: mkOptionType {
268195 name = "null or ${elemType.name}";
269− typerep = "(nullOr${elemType.typerep})";
270196 check = x: x == null || elemType.check x;
271− merge = _module: loc: defs:
197+ merge = loc: defs:
272198 let nrNulls = count (def: def.value == null) defs; in
273199 if nrNulls == length defs then null
274200 else if nrNulls != 0 then
275201 throw "The option `${showOption loc}' is defined both null and not null, in ${showFiles (getFiles defs)}."
276− else elemType.merge _module loc defs;
202+ else elemType.merge loc defs;
277203 getSubOptions = elemType.getSubOptions;
278− getSubOptionsPrefixed = prefix: elemType.getSubOptionsPrefixed prefix;
279204 getSubModules = elemType.getSubModules;
280205 substSubModules = m: nullOr (elemType.substSubModules m);
281− nestedTypes = elemType.nestedTypes;
282− defaultValues = [null] ++ elemType.defaultValues;
283− };
284−
285− enum = values:
286− let
287− show = v:
288− if builtins.isString v then ''"${v}"''
289− else if builtins.isInt v then builtins.toString v
290− else ''<${builtins.typeOf v}>'';
291− in
292− mkOptionType {
293− name = "one of ${concatMapStringsSep ", " show values}";
294− typerep = "(enum${concatMapStrings (x: "(${escape ["(" ")"] (builtins.toString x)})") values})";
295− check = flip elem values;
296− merge = mergeOneOption;
297− nestedTypes = [];
298− };
299−
300− either = t1: t2: mkOptionType {
301− name = "${t1.name} or ${t2.name}";
302− typerep = "(either${t1.typerep}${t2.typerep})";
303− check = x: t1.check x || t2.check x;
304− merge = mergeOneOption;
305− nestedTypes = t1.nestedTypes ++ t2.nestedTypes;
306− defaultValues = t1.defaultValues ++ t2.defaultValues;
307206 };
308207
309−
310− #
311− # Complex types
312− #
313−
314208 submodule = opts:
315209 let
316210 opts' = toList opts;
317211 inherit (import ./modules.nix) evalModules;
318− filterVisible = filter (opt: (if opt ? visible then opt.visible else true) && (if opt ? internal then !opt.internal else true));
319212 in
320213 mkOptionType rec {
321214 name = "submodule";
322− typerep = "(submodule)";
323215 check = x: isAttrs x || isFunction x;
324− merge = _module: loc: defs:
216+ merge = loc: defs:
325217 let
326− internalModule = [ { inherit _module; } { _module.args.name = lib.mkForce (last loc); } ];
327218 coerce = def: if isFunction def then def else { config = def; };
328− modules = opts' ++ internalModule ++ map (def: { _file = def.file; imports = [(coerce def.value)]; }) defs;
219+ modules = opts' ++ map (def: { _file = def.file; imports = [(coerce def.value)]; }) defs;
329220 in (evalModules {
330221 inherit modules;
222+ args.name = last loc;
331223 prefix = loc;
332224 }).config;
333− getSubOptions = getSubOptionsPrefixed [];
334− getSubOptionsPrefixed = prefix: _module:
335− let
225+ getSubOptions = prefix: (evalModules
226+ { modules = opts'; inherit prefix;
336227 # FIXME: hack to get shit to evaluate.
337− internalModule = [ { inherit _module; } { _module.args.name = lib.mkForce ""; } ];
338− in (evalModules {
339− modules = opts' ++ internalModule;
340− inherit prefix;
341− }).options;
228+ args = { name = ""; }; }).options;
342229 getSubModules = opts';
343230 substSubModules = m: submodule m;
344− nestedTypes = concatMap (opt: opt.type.nestedTypes) (collect (lib.isType "option") (getSubOptions {}));
345− defaultValues = [{}];
346231 };
347232
233+ enum = values:
234+ let
235+ show = v:
236+ if builtins.isString v then ''"${v}"''
237+ else if builtins.isInt v then builtins.toString v
238+ else ''<${builtins.typeOf v}>'';
239+ in
240+ mkOptionType {
241+ name = "one of ${concatMapStringsSep ", " show values}";
242+ check = flip elem values;
243+ merge = mergeOneOption;
244+ };
348245
349−
350− #
351− # Legacy types
352− #
246+ either = t1: t2: mkOptionType {
247+ name = "${t1.name} or ${t2.name}";
248+ check = x: t1.check x || t2.check x;
249+ merge = mergeOneOption;
250+ };
353251
354252 # Obsolete alternative to configOf. It takes its option
355253 # declarations from the ‘options’ attribute of containing option
356254 # declaration.
357255 optionSet = mkOptionType {
358256 name = /* builtins.trace "types.optionSet is deprecated; use types.submodule instead" */ "option set";
359− typerep = "(optionSet)";
360257 };
361258
362−
363− # Try to remove module options of that type wherever possible.
364− # A module option taking a function can not be introspected and documented properly.
365− functionTo = resultType:
366− mkOptionType {
367− name = "function to ${resultType.name}";
368− typerep = "(function${resultType.typerep})";
369− check = builtins.isFunction;
370− merge = mergeOneOption;
371− nestedTypes = resultType.nestedTypes;
372− defaultValues = map (x: {...}:x) resultType.nestedTypes; # INFO: It seems as nix can't compare functions, yet.
373− };
374−
375−
376− #
377− # misc
378− #
379−
380259 # Augment the given type with an additional type check function.
381260 addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; };
382261