2009-07-13 10:18:52 -06: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 07:36:30 -06:00
|
|
|
with import ./options.nix;
|
2009-07-13 10:18:52 -06: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 07:36:30 -06:00
|
|
|
isModule = m:
|
|
|
|
(m ? config && isAttrs m.config && ! isOption m.config)
|
|
|
|
|| (m ? options && isAttrs m.options && ! isOption m.options);
|
|
|
|
|
2009-09-14 07:19:00 -06:00
|
|
|
# Convert module to a set which has imports / options and config
|
|
|
|
# attributes.
|
|
|
|
unifyModuleSyntax = m:
|
2009-07-13 10:18:52 -06: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 07:19:00 -06:00
|
|
|
getConfig = m:
|
2010-01-05 10:04:55 -07:00
|
|
|
removeAttrs (delayProperties m) ["require" "key"];
|
2009-09-14 07:19:00 -06:00
|
|
|
in
|
2009-09-15 07:36:30 -06:00
|
|
|
if isModule m then
|
2009-09-18 09:18:23 -06:00
|
|
|
{ key = "<unknown location>"; } // m
|
2009-09-14 07:19:00 -06:00
|
|
|
else
|
|
|
|
{
|
2009-09-18 09:18:23 -06:00
|
|
|
key = "<unknown location>";
|
2009-09-14 07:19:00 -06:00
|
|
|
imports = getImportedPaths m;
|
|
|
|
config = getConfig m;
|
|
|
|
} // (
|
|
|
|
if getImportedSets m != [] then
|
|
|
|
assert tail (getImportedSets m) == [];
|
|
|
|
{ options = head (getImportedSets m); }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2009-10-09 12:11:24 -06: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 07:19:00 -06:00
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2009-09-14 18:21:39 -06:00
|
|
|
moduleImport = m:
|
2010-01-05 10:04:55 -07:00
|
|
|
let m' = applyIfFunction (importIfPath m) args;
|
|
|
|
in (unifyModuleSyntax m') // {
|
2009-09-14 07:19:00 -06:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2010-01-05 10:04:55 -07:00
|
|
|
key = if isPath m then m else if m' ? key then m'.key else "<unknown location>";
|
2009-09-14 18:21:39 -06:00
|
|
|
};
|
2009-09-14 07:19:00 -06:00
|
|
|
|
|
|
|
getImports = m: attrByPath ["imports"] [] m;
|
2009-09-14 18:21:39 -06:00
|
|
|
|
2009-07-13 10:18:52 -06:00
|
|
|
in
|
2009-09-14 18:21:39 -06:00
|
|
|
(lazyGenericClosure {
|
2010-01-05 10:04:55 -07:00
|
|
|
startSet = map moduleImport initModules;
|
2009-09-14 07:19:00 -06:00
|
|
|
operator = m: map moduleImport (getImports m);
|
2010-01-05 10:04:55 -07:00
|
|
|
});
|
2009-07-13 10:18:52 -06:00
|
|
|
|
|
|
|
selectDeclsAndDefs = modules:
|
|
|
|
lib.concatMap (m:
|
|
|
|
if m ? config || m ? options then
|
2009-07-14 10:22:42 -06:00
|
|
|
[ (attrByPath ["options"] {} m) ]
|
|
|
|
++ [ (attrByPath ["config"] {} m) ]
|
2009-07-13 10:18:52 -06:00
|
|
|
else
|
|
|
|
[ m ]
|
|
|
|
) modules;
|
|
|
|
|
2009-09-15 07:36:30 -06: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-09 17:03:24 -06:00
|
|
|
|
2009-09-15 07:36:30 -06:00
|
|
|
delayModule = module:
|
|
|
|
moduleApply { config = delayProperties; } module;
|
|
|
|
|
2009-10-09 17:03:24 -06:00
|
|
|
evalDefinitions = opt: values:
|
2009-10-12 09:16:22 -06:00
|
|
|
if opt ? type && opt.type.delayOnGlobalEval then
|
2009-10-12 07:37:00 -06:00
|
|
|
map (delayPropertiesWithIter opt.type.iter opt.name)
|
|
|
|
(evalLocalProperties values)
|
2009-10-09 17:03:24 -06:00
|
|
|
else
|
|
|
|
evalProperties values;
|
|
|
|
|
|
|
|
|
2009-09-15 07:36:30 -06: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 09:50:59 -06:00
|
|
|
fun (catAttrs name modules)
|
2009-09-15 07:36:30 -06: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 11:51:21 -06:00
|
|
|
recurseInto = name:
|
2009-09-15 07:36:30 -06:00
|
|
|
moduleMerge (addName name) (modulesOf name);
|
|
|
|
|
|
|
|
recurseForOption = name: modules:
|
|
|
|
moduleMerge name (
|
|
|
|
map unifyModuleSyntax modules
|
|
|
|
);
|
|
|
|
|
|
|
|
errorSource = modules:
|
2009-09-16 05:37:07 -06:00
|
|
|
"The error may come from the following files:\n" + (
|
2009-09-15 07:36:30 -06:00
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
2009-09-16 05:37:07 -06:00
|
|
|
if m ? key then toString m.key else "<unknown location>"
|
2009-09-15 07:36:30 -06:00
|
|
|
) modules
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
eol = "\n";
|
|
|
|
|
2009-10-12 11:51:21 -06: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 07:36:30 -06:00
|
|
|
|
|
|
|
endRecursion = { options = {}; config = {}; };
|
|
|
|
|
|
|
|
in if modules == [] then endRecursion else
|
2009-10-12 11:51:21 -06: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 07:36:30 -06:00
|
|
|
|
2009-10-12 11:51:21 -06: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 09:36:03 -07:00
|
|
|
lib.addErrorContext "${eol
|
2010-05-07 09:13:43 -06:00
|
|
|
}while enhancing option `${addName name}':${eol
|
2009-11-05 09:36:03 -07:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}" (
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls decls)
|
|
|
|
);
|
2009-10-12 11:51:21 -06:00
|
|
|
|
|
|
|
value = decl // (with config; {
|
|
|
|
inherit (config) isNotDefined;
|
2009-11-05 08:39:39 -07:00
|
|
|
isDefined = ! isNotDefined;
|
2009-10-12 11:51:21 -06:00
|
|
|
declarations = declarationSources;
|
|
|
|
definitions = definitionSources;
|
|
|
|
config = strictResult;
|
|
|
|
});
|
|
|
|
|
|
|
|
recurse = (recurseInto name).options;
|
|
|
|
|
|
|
|
result =
|
|
|
|
if isOption then value
|
2009-11-06 18:59:50 -07:00
|
|
|
else if !hasOptions then {}
|
2009-10-12 11:51:21 -06:00
|
|
|
else if all isAttrs values then recurse
|
2009-09-15 07:36:30 -06:00
|
|
|
else
|
|
|
|
throw "${eol
|
|
|
|
}Unexpected type where option declarations are expected.${eol
|
2009-10-12 11:51:21 -06:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}";
|
2009-09-15 07:36:30 -06:00
|
|
|
|
2009-10-12 11:51:21 -06: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 09:13:43 -06:00
|
|
|
}while evaluating the option `${addName name}':${eol
|
2009-10-12 11:51:21 -06: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 07:36:30 -06: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 12:22:24 -06:00
|
|
|
moduleClosure initModules (module // args)
|
2009-09-15 07:36:30 -06:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-11-06 18:59:50 -07: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 07:55:08 -06:00
|
|
|
}while checking the attribute `${addName name}':${eol
|
2009-11-06 18:59:50 -07:00
|
|
|
}" (checkModule (addName name) (selectModule name m))
|
|
|
|
) (lib.attrNames m.config)
|
|
|
|
else
|
|
|
|
builtins.trace "try to evaluate config ${lib.showVal config}."
|
|
|
|
false;
|
|
|
|
|
2009-09-14 14:10:41 -06:00
|
|
|
}
|