2009-07-13 20:18:52 +04:00
|
|
|
# NixOS module handling.
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
|
|
|
with { inherit (builtins) head tail; };
|
|
|
|
with import ./trivial.nix;
|
|
|
|
with import ./lists.nix;
|
|
|
|
with import ./misc.nix;
|
|
|
|
with import ./attrsets.nix;
|
2009-09-15 17:36:30 +04:00
|
|
|
with import ./options.nix;
|
2009-07-13 20:18:52 +04:00
|
|
|
with import ./properties.nix;
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
|
|
|
# Unfortunately this can also be a string.
|
|
|
|
isPath = x: !(
|
|
|
|
builtins.isFunction x
|
|
|
|
|| builtins.isAttrs x
|
|
|
|
|| builtins.isInt x
|
|
|
|
|| builtins.isBool x
|
|
|
|
|| builtins.isList x
|
|
|
|
);
|
|
|
|
|
|
|
|
importIfPath = path:
|
|
|
|
if isPath path then
|
|
|
|
import path
|
|
|
|
else
|
|
|
|
path;
|
|
|
|
|
|
|
|
applyIfFunction = f: arg:
|
|
|
|
if builtins.isFunction f then
|
|
|
|
f arg
|
|
|
|
else
|
|
|
|
f;
|
|
|
|
|
2009-09-15 17:36:30 +04:00
|
|
|
isModule = m:
|
|
|
|
(m ? config && isAttrs m.config && ! isOption m.config)
|
|
|
|
|| (m ? options && isAttrs m.options && ! isOption m.options);
|
|
|
|
|
2009-09-14 17:19:00 +04:00
|
|
|
# Convert module to a set which has imports / options and config
|
|
|
|
# attributes.
|
|
|
|
unifyModuleSyntax = m:
|
2009-07-13 20:18:52 +04:00
|
|
|
let
|
|
|
|
getImports = m:
|
|
|
|
if m ? config || m ? options then
|
|
|
|
attrByPath ["imports"] [] m
|
|
|
|
else
|
|
|
|
toList (rmProperties (attrByPath ["require"] [] (delayProperties m)));
|
|
|
|
|
|
|
|
getImportedPaths = m: filter isPath (getImports m);
|
|
|
|
getImportedSets = m: filter (x: !isPath x) (getImports m);
|
|
|
|
|
2009-09-14 17:19:00 +04:00
|
|
|
getConfig = m:
|
2010-01-05 20:04:55 +03:00
|
|
|
removeAttrs (delayProperties m) ["require" "key"];
|
2009-09-14 17:19:00 +04:00
|
|
|
in
|
2009-09-15 17:36:30 +04:00
|
|
|
if isModule m then
|
2009-09-18 19:18:23 +04:00
|
|
|
{ key = "<unknown location>"; } // m
|
2009-09-14 17:19:00 +04:00
|
|
|
else
|
|
|
|
{
|
2009-09-18 19:18:23 +04:00
|
|
|
key = "<unknown location>";
|
2009-09-14 17:19:00 +04:00
|
|
|
imports = getImportedPaths m;
|
|
|
|
config = getConfig m;
|
|
|
|
} // (
|
|
|
|
if getImportedSets m != [] then
|
|
|
|
assert tail (getImportedSets m) == [];
|
|
|
|
{ options = head (getImportedSets m); }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2009-10-09 22:11:24 +04:00
|
|
|
|
|
|
|
unifyOptionModule = {key ? "<unknown location>"}: m: (args:
|
|
|
|
let module = lib.applyIfFunction m args; in
|
|
|
|
if lib.isModule module then
|
|
|
|
{ inherit key; } // module
|
|
|
|
else
|
|
|
|
{ inherit key; options = module; }
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2009-09-14 17:19:00 +04:00
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2011-04-27 22:41:37 +04:00
|
|
|
moduleImport = origin: index: m:
|
2010-01-05 20:04:55 +03:00
|
|
|
let m' = applyIfFunction (importIfPath m) args;
|
|
|
|
in (unifyModuleSyntax m') // {
|
2009-09-14 17:19:00 +04:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2011-04-27 22:41:37 +04:00
|
|
|
key =
|
|
|
|
if isPath m then m
|
|
|
|
else if m' ? key then m'.key
|
|
|
|
else newModuleName origin index;
|
2009-09-15 04:21:39 +04:00
|
|
|
};
|
2009-09-14 17:19:00 +04:00
|
|
|
|
|
|
|
getImports = m: attrByPath ["imports"] [] m;
|
2009-09-15 04:21:39 +04:00
|
|
|
|
2011-04-27 22:41:37 +04:00
|
|
|
newModuleName = origin: index:
|
|
|
|
"${origin.key}:<import-${toString index}>";
|
|
|
|
|
|
|
|
topLevel = {
|
|
|
|
key = "<top-level>";
|
|
|
|
};
|
|
|
|
|
2009-07-13 20:18:52 +04:00
|
|
|
in
|
2009-09-15 04:21:39 +04:00
|
|
|
(lazyGenericClosure {
|
2011-04-27 22:41:37 +04:00
|
|
|
startSet = imap (moduleImport topLevel) initModules;
|
|
|
|
operator = m: imap (moduleImport m) (getImports m);
|
2010-01-05 20:04:55 +03:00
|
|
|
});
|
2009-07-13 20:18:52 +04:00
|
|
|
|
|
|
|
selectDeclsAndDefs = modules:
|
|
|
|
lib.concatMap (m:
|
|
|
|
if m ? config || m ? options then
|
2009-07-14 20:22:42 +04:00
|
|
|
[ (attrByPath ["options"] {} m) ]
|
|
|
|
++ [ (attrByPath ["config"] {} m) ]
|
2009-07-13 20:18:52 +04:00
|
|
|
else
|
|
|
|
[ m ]
|
|
|
|
) modules;
|
|
|
|
|
2009-09-15 17:36:30 +04:00
|
|
|
|
|
|
|
moduleApply = funs: module:
|
|
|
|
lib.mapAttrs (name: value:
|
|
|
|
if builtins.hasAttr name funs then
|
|
|
|
let fun = lib.getAttr name funs; in
|
|
|
|
fun value
|
|
|
|
else
|
|
|
|
value
|
|
|
|
) module;
|
|
|
|
|
2009-10-10 03:03:24 +04:00
|
|
|
|
2009-09-15 17:36:30 +04:00
|
|
|
delayModule = module:
|
|
|
|
moduleApply { config = delayProperties; } module;
|
|
|
|
|
2009-10-10 03:03:24 +04:00
|
|
|
evalDefinitions = opt: values:
|
2009-10-12 19:16:22 +04:00
|
|
|
if opt ? type && opt.type.delayOnGlobalEval then
|
2009-10-12 17:37:00 +04:00
|
|
|
map (delayPropertiesWithIter opt.type.iter opt.name)
|
|
|
|
(evalLocalProperties values)
|
2009-10-10 03:03:24 +04:00
|
|
|
else
|
|
|
|
evalProperties values;
|
|
|
|
|
|
|
|
|
2009-09-15 17:36:30 +04:00
|
|
|
selectModule = name: m:
|
|
|
|
{ inherit (m) key;
|
|
|
|
} // (
|
|
|
|
if m ? options && builtins.hasAttr name m.options then
|
|
|
|
{ options = lib.getAttr name m.options; }
|
|
|
|
else {}
|
|
|
|
) // (
|
|
|
|
if m ? config && builtins.hasAttr name m.config then
|
|
|
|
{ config = lib.getAttr name m.config; }
|
|
|
|
else {}
|
|
|
|
);
|
|
|
|
|
|
|
|
filterModules = name: modules:
|
|
|
|
filter (m: m ? config || m ? options) (
|
|
|
|
map (selectModule name) modules
|
|
|
|
);
|
|
|
|
|
|
|
|
modulesNames = modules:
|
|
|
|
lib.concatMap (m: []
|
|
|
|
++ optionals (m ? options) (lib.attrNames m.options)
|
|
|
|
++ optionals (m ? config) (lib.attrNames m.config)
|
|
|
|
) modules;
|
|
|
|
|
|
|
|
moduleZip = funs: modules:
|
|
|
|
lib.mapAttrs (name: fun:
|
2009-10-12 19:50:59 +04:00
|
|
|
fun (catAttrs name modules)
|
2009-09-15 17:36:30 +04:00
|
|
|
) funs;
|
|
|
|
|
|
|
|
moduleMerge = path: modules:
|
|
|
|
let modules_ = modules; in
|
|
|
|
let
|
|
|
|
addName = name:
|
|
|
|
if path == "" then name else path + "." + name;
|
|
|
|
|
|
|
|
modules = map delayModule modules_;
|
|
|
|
|
|
|
|
modulesOf = name: filterModules name modules;
|
|
|
|
declarationsOf = name: filter (m: m ? options) (modulesOf name);
|
|
|
|
definitionsOf = name: filter (m: m ? config ) (modulesOf name);
|
|
|
|
|
2009-10-12 21:51:21 +04:00
|
|
|
recurseInto = name:
|
2009-09-15 17:36:30 +04:00
|
|
|
moduleMerge (addName name) (modulesOf name);
|
|
|
|
|
|
|
|
recurseForOption = name: modules:
|
|
|
|
moduleMerge name (
|
|
|
|
map unifyModuleSyntax modules
|
|
|
|
);
|
|
|
|
|
|
|
|
errorSource = modules:
|
2009-09-16 15:37:07 +04:00
|
|
|
"The error may come from the following files:\n" + (
|
2009-09-15 17:36:30 +04:00
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
2009-09-16 15:37:07 +04:00
|
|
|
if m ? key then toString m.key else "<unknown location>"
|
2009-09-15 17:36:30 +04:00
|
|
|
) modules
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
eol = "\n";
|
|
|
|
|
2009-10-12 21:51:21 +04:00
|
|
|
allNames = modulesNames modules;
|
|
|
|
|
|
|
|
getResults = m:
|
|
|
|
let fetchResult = s: mapAttrs (n: v: v.result) s; in {
|
|
|
|
options = fetchResult m.options;
|
|
|
|
config = fetchResult m.config;
|
|
|
|
};
|
2009-09-15 17:36:30 +04:00
|
|
|
|
|
|
|
endRecursion = { options = {}; config = {}; };
|
|
|
|
|
|
|
|
in if modules == [] then endRecursion else
|
2009-10-12 21:51:21 +04:00
|
|
|
getResults (fix (crossResults: moduleZip {
|
|
|
|
options = lib.zipWithNames allNames (name: values: rec {
|
|
|
|
config = lib.getAttr name crossResults.config;
|
|
|
|
|
|
|
|
declarations = declarationsOf name;
|
|
|
|
declarationSources =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
|
|
|
}) declarations;
|
|
|
|
|
2009-09-15 17:36:30 +04:00
|
|
|
|
2009-10-12 21:51:21 +04:00
|
|
|
hasOptions = values != [];
|
|
|
|
isOption = any lib.isOption values;
|
|
|
|
|
|
|
|
decls = # add location to sub-module options.
|
|
|
|
map (m:
|
|
|
|
mapSubOptions
|
|
|
|
(unifyOptionModule {inherit (m) key;})
|
|
|
|
m.options
|
|
|
|
) declarations;
|
|
|
|
|
|
|
|
decl =
|
2009-11-05 19:36:03 +03:00
|
|
|
lib.addErrorContext "${eol
|
2010-05-07 19:13:43 +04:00
|
|
|
}while enhancing option `${addName name}':${eol
|
2009-11-05 19:36:03 +03:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}" (
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls decls)
|
|
|
|
);
|
2009-10-12 21:51:21 +04:00
|
|
|
|
|
|
|
value = decl // (with config; {
|
|
|
|
inherit (config) isNotDefined;
|
2009-11-05 18:39:39 +03:00
|
|
|
isDefined = ! isNotDefined;
|
2009-10-12 21:51:21 +04:00
|
|
|
declarations = declarationSources;
|
|
|
|
definitions = definitionSources;
|
|
|
|
config = strictResult;
|
|
|
|
});
|
|
|
|
|
|
|
|
recurse = (recurseInto name).options;
|
|
|
|
|
|
|
|
result =
|
|
|
|
if isOption then value
|
2009-11-07 04:59:50 +03:00
|
|
|
else if !hasOptions then {}
|
2009-10-12 21:51:21 +04:00
|
|
|
else if all isAttrs values then recurse
|
2009-09-15 17:36:30 +04:00
|
|
|
else
|
|
|
|
throw "${eol
|
|
|
|
}Unexpected type where option declarations are expected.${eol
|
2009-10-12 21:51:21 +04:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}";
|
2009-09-15 17:36:30 +04:00
|
|
|
|
2009-10-12 21:51:21 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
config = lib.zipWithNames allNames (name: values_: rec {
|
|
|
|
option = lib.getAttr name crossResults.options;
|
|
|
|
|
|
|
|
definitions = definitionsOf name;
|
|
|
|
definitionSources =
|
|
|
|
map (m: {
|
|
|
|
source = m.key;
|
|
|
|
value = m.config;
|
|
|
|
}) definitions;
|
|
|
|
|
|
|
|
values = values_ ++
|
|
|
|
optionals (option.isOption && option.decl ? extraConfigs)
|
|
|
|
option.decl.extraConfigs;
|
|
|
|
|
|
|
|
defs = evalDefinitions option.decl values;
|
|
|
|
|
|
|
|
isNotDefined = defs == [];
|
|
|
|
|
|
|
|
value =
|
|
|
|
lib.addErrorContext "${eol
|
2010-05-07 19:13:43 +04:00
|
|
|
}while evaluating the option `${addName name}':${eol
|
2009-10-12 21:51:21 +04:00
|
|
|
}${errorSource (modulesOf name)}${eol
|
|
|
|
}" (
|
|
|
|
let opt = option.decl; in
|
|
|
|
opt.apply (
|
|
|
|
if isNotDefined then
|
|
|
|
if opt ? default then opt.default
|
|
|
|
else throw "Not defined."
|
|
|
|
else opt.merge defs
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
strictResult = builtins.tryEval (builtins.toXML value);
|
|
|
|
|
|
|
|
recurse = (recurseInto name).config;
|
|
|
|
|
|
|
|
configIsAnOption = v: isOption (rmProperties v);
|
|
|
|
errConfigIsAnOption =
|
|
|
|
let badModules = filter (m: configIsAnOption m.config) definitions; in
|
|
|
|
"${eol
|
|
|
|
}Option ${addName name} is defined in the configuration section.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}";
|
|
|
|
|
|
|
|
errDefinedWithoutDeclaration =
|
|
|
|
let badModules = definitions; in
|
|
|
|
"${eol
|
|
|
|
}Option '${addName name}' defined without option declaration.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}";
|
|
|
|
|
|
|
|
result =
|
|
|
|
if option.isOption then value
|
|
|
|
else if !option.hasOptions then throw errDefinedWithoutDeclaration
|
|
|
|
else if any configIsAnOption values then throw errConfigIsAnOption
|
|
|
|
else if all isAttrs values then recurse
|
|
|
|
# plain value during the traversal
|
|
|
|
else throw errDefinedWithoutDeclaration;
|
|
|
|
|
|
|
|
});
|
|
|
|
} modules));
|
2009-09-15 17:36:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
fixMergeModules = initModules: {...}@args:
|
|
|
|
lib.fix (result:
|
|
|
|
# This trick avoid an infinite loop because names of attribute are
|
|
|
|
# know and it is not require to evaluate the result of moduleMerge to
|
|
|
|
# know which attribute are present as argument.
|
|
|
|
let module = { inherit (result) options config; }; in
|
|
|
|
|
|
|
|
moduleMerge "" (
|
2009-09-28 22:22:24 +04:00
|
|
|
moduleClosure initModules (module // args)
|
2009-09-15 17:36:30 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-11-07 04:59:50 +03:00
|
|
|
# Visit all definitions to raise errors related to undeclared options.
|
|
|
|
checkModule = path: {config, options, ...}@m:
|
|
|
|
let
|
|
|
|
eol = "\n";
|
|
|
|
addName = name:
|
|
|
|
if path == "" then name else path + "." + name;
|
|
|
|
in
|
|
|
|
if lib.isOption options then
|
|
|
|
if options ? options then
|
|
|
|
options.type.fold
|
|
|
|
(cfg: res: res && checkModule (options.type.docPath path) cfg._args)
|
|
|
|
true config
|
|
|
|
else
|
|
|
|
true
|
|
|
|
else if isAttrs options && lib.attrNames m.options != [] then
|
|
|
|
all (name:
|
|
|
|
lib.addErrorContext "${eol
|
2010-10-14 17:55:08 +04:00
|
|
|
}while checking the attribute `${addName name}':${eol
|
2009-11-07 04:59:50 +03:00
|
|
|
}" (checkModule (addName name) (selectModule name m))
|
|
|
|
) (lib.attrNames m.config)
|
|
|
|
else
|
|
|
|
builtins.trace "try to evaluate config ${lib.showVal config}."
|
|
|
|
false;
|
|
|
|
|
2009-09-15 00:10:41 +04:00
|
|
|
}
|