2009-07-13 16:18:52 +00:00
|
|
|
# NixOS module handling.
|
|
|
|
|
|
|
|
let lib = import ./default.nix; in
|
|
|
|
|
2012-08-13 18:19:31 +00:00
|
|
|
with { inherit (builtins) head; };
|
2009-07-13 16:18:52 +00:00
|
|
|
with import ./trivial.nix;
|
|
|
|
with import ./lists.nix;
|
|
|
|
with import ./misc.nix;
|
|
|
|
with import ./attrsets.nix;
|
2009-09-15 13:36:30 +00:00
|
|
|
with import ./options.nix;
|
2009-07-13 16:18:52 +00: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
|
|
|
|
);
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-07-13 16:18:52 +00:00
|
|
|
importIfPath = path:
|
|
|
|
if isPath path then
|
|
|
|
import path
|
|
|
|
else
|
|
|
|
path;
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-07-13 16:18:52 +00:00
|
|
|
applyIfFunction = f: arg:
|
|
|
|
if builtins.isFunction f then
|
|
|
|
f arg
|
|
|
|
else
|
|
|
|
f;
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
isModule = m:
|
|
|
|
(m ? config && isAttrs m.config && ! isOption m.config)
|
|
|
|
|| (m ? options && isAttrs m.options && ! isOption m.options);
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-09-14 13:19:00 +00:00
|
|
|
# Convert module to a set which has imports / options and config
|
|
|
|
# attributes.
|
|
|
|
unifyModuleSyntax = m:
|
2009-07-13 16:18:52 +00:00
|
|
|
let
|
2012-04-15 23:31:48 +00:00
|
|
|
delayedModule = delayProperties m;
|
2009-07-13 16:18:52 +00:00
|
|
|
|
2013-09-04 09:49:56 +00:00
|
|
|
getImports =
|
|
|
|
toList (rmProperties (delayedModule.require or []));
|
2012-04-15 23:31:48 +00:00
|
|
|
getImportedPaths = filter isPath getImports;
|
|
|
|
getImportedSets = filter (x: !isPath x) getImports;
|
|
|
|
|
|
|
|
getConfig =
|
2013-09-04 09:49:56 +00:00
|
|
|
removeAttrs delayedModule ["require" "key" "imports"];
|
2009-07-13 16:18:52 +00:00
|
|
|
|
2009-09-14 13:19:00 +00:00
|
|
|
in
|
2009-09-15 13:36:30 +00:00
|
|
|
if isModule m then
|
2009-09-18 15:18:23 +00:00
|
|
|
{ key = "<unknown location>"; } // m
|
2009-09-14 13:19:00 +00:00
|
|
|
else
|
2013-09-04 09:49:56 +00:00
|
|
|
{ key = "<unknown location>";
|
|
|
|
imports = (m.imports or []) ++ getImportedPaths;
|
2012-04-15 23:31:48 +00:00
|
|
|
config = getConfig;
|
2009-09-14 13:19:00 +00:00
|
|
|
} // (
|
2012-04-15 23:31:48 +00:00
|
|
|
if getImportedSets != [] then
|
2012-08-13 18:19:31 +00:00
|
|
|
assert length getImportedSets == 1;
|
2012-04-15 23:31:48 +00:00
|
|
|
{ options = head getImportedSets; }
|
2009-09-14 13:19:00 +00:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
2009-10-09 18:11:24 +00:00
|
|
|
|
2013-06-07 07:42:46 +00:00
|
|
|
unifyOptionModule = {key ? "<unknown location>"}: name: index: m: (args:
|
|
|
|
let
|
|
|
|
module = lib.applyIfFunction m args;
|
|
|
|
key_ = rec {
|
|
|
|
file = key;
|
|
|
|
option = name;
|
|
|
|
number = index;
|
2013-06-17 14:05:46 +00:00
|
|
|
outPath = key;
|
2013-06-07 07:42:46 +00:00
|
|
|
};
|
|
|
|
in if lib.isModule module then
|
|
|
|
{ key = key_; } // module
|
2009-10-09 18:11:24 +00:00
|
|
|
else
|
2013-06-07 07:42:46 +00:00
|
|
|
{ key = key_; options = module; }
|
2009-10-09 18:11:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2009-09-14 13:19:00 +00:00
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2011-04-27 18:41:37 +00:00
|
|
|
moduleImport = origin: index: m:
|
2010-01-05 17:04:55 +00:00
|
|
|
let m' = applyIfFunction (importIfPath m) args;
|
|
|
|
in (unifyModuleSyntax m') // {
|
2009-09-14 13:19:00 +00:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2011-04-27 18:41:37 +00:00
|
|
|
key =
|
|
|
|
if isPath m then m
|
2012-12-04 11:28:57 +00:00
|
|
|
else m'.key or (newModuleName origin index);
|
2009-09-15 00:21:39 +00:00
|
|
|
};
|
2009-09-14 13:19:00 +00:00
|
|
|
|
2012-08-13 18:39:45 +00:00
|
|
|
getImports = m: m.imports or [];
|
2009-09-15 00:21:39 +00:00
|
|
|
|
2011-04-27 18:41:37 +00:00
|
|
|
newModuleName = origin: index:
|
|
|
|
"${origin.key}:<import-${toString index}>";
|
|
|
|
|
|
|
|
topLevel = {
|
|
|
|
key = "<top-level>";
|
|
|
|
};
|
|
|
|
|
2009-07-13 16:18:52 +00:00
|
|
|
in
|
2009-09-15 00:21:39 +00:00
|
|
|
(lazyGenericClosure {
|
2011-04-27 18:41:37 +00:00
|
|
|
startSet = imap (moduleImport topLevel) initModules;
|
|
|
|
operator = m: imap (moduleImport m) (getImports m);
|
2010-01-05 17:04:55 +00:00
|
|
|
});
|
2009-07-13 16:18:52 +00:00
|
|
|
|
2009-09-15 13:36:30 +00: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;
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2012-04-15 23:31:48 +00:00
|
|
|
# Handle mkMerge function left behind after a delay property.
|
|
|
|
moduleFlattenMerge = module:
|
|
|
|
if module ? config &&
|
|
|
|
isProperty module.config &&
|
|
|
|
isMerge module.config.property
|
|
|
|
then
|
|
|
|
(map (cfg: { key = module.key; config = cfg; }) module.config.content)
|
|
|
|
++ [ (module // { config = {}; }) ]
|
|
|
|
else
|
|
|
|
[ module ];
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2012-04-15 23:31:48 +00:00
|
|
|
# Handle mkMerge attributes which are left behind by previous delay
|
|
|
|
# properties and convert them into a list of modules. Delay properties
|
|
|
|
# inside the config attribute of a module and create a second module if a
|
|
|
|
# mkMerge attribute was left behind.
|
|
|
|
#
|
|
|
|
# Module -> [ Module ]
|
2009-09-15 13:36:30 +00:00
|
|
|
delayModule = module:
|
2012-04-15 23:31:48 +00:00
|
|
|
map (moduleApply { config = delayProperties; }) (moduleFlattenMerge module);
|
2009-09-15 13:36:30 +00:00
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-10-09 23:03:24 +00:00
|
|
|
evalDefinitions = opt: values:
|
2012-12-04 11:28:57 +00:00
|
|
|
if opt.type.delayOnGlobalEval or false then
|
2009-10-12 13:37:00 +00:00
|
|
|
map (delayPropertiesWithIter opt.type.iter opt.name)
|
|
|
|
(evalLocalProperties values)
|
2009-10-09 23:03:24 +00:00
|
|
|
else
|
|
|
|
evalProperties values;
|
|
|
|
|
|
|
|
|
2009-09-15 13:36:30 +00: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
|
|
|
|
);
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
modulesNames = modules:
|
|
|
|
lib.concatMap (m: []
|
|
|
|
++ optionals (m ? options) (lib.attrNames m.options)
|
|
|
|
++ optionals (m ? config) (lib.attrNames m.config)
|
|
|
|
) modules;
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
moduleZip = funs: modules:
|
|
|
|
lib.mapAttrs (name: fun:
|
2009-10-12 15:50:59 +00:00
|
|
|
fun (catAttrs name modules)
|
2009-09-15 13:36:30 +00:00
|
|
|
) funs;
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-09-15 13:36:30 +00:00
|
|
|
moduleMerge = path: modules:
|
|
|
|
let modules_ = modules; in
|
|
|
|
let
|
|
|
|
addName = name:
|
|
|
|
if path == "" then name else path + "." + name;
|
|
|
|
|
2012-04-15 23:31:48 +00:00
|
|
|
modules = concatLists (map delayModule modules_);
|
2009-09-15 13:36:30 +00:00
|
|
|
|
|
|
|
modulesOf = name: filterModules name modules;
|
|
|
|
declarationsOf = name: filter (m: m ? options) (modulesOf name);
|
|
|
|
definitionsOf = name: filter (m: m ? config ) (modulesOf name);
|
|
|
|
|
2009-10-12 17:51:21 +00:00
|
|
|
recurseInto = name:
|
2009-09-15 13:36:30 +00:00
|
|
|
moduleMerge (addName name) (modulesOf name);
|
|
|
|
|
2013-06-06 17:59:26 +00:00
|
|
|
recurseForOption = name: modules: args:
|
2009-09-15 13:36:30 +00:00
|
|
|
moduleMerge name (
|
2013-06-06 17:59:26 +00:00
|
|
|
moduleClosure modules args
|
2009-09-15 13:36:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
errorSource = modules:
|
2009-09-16 11:37:07 +00:00
|
|
|
"The error may come from the following files:\n" + (
|
2009-09-15 13:36:30 +00:00
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
2009-09-16 11:37:07 +00:00
|
|
|
if m ? key then toString m.key else "<unknown location>"
|
2009-09-15 13:36:30 +00:00
|
|
|
) modules
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
eol = "\n";
|
|
|
|
|
2009-10-12 17:51:21 +00: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 13:36:30 +00:00
|
|
|
|
|
|
|
endRecursion = { options = {}; config = {}; };
|
|
|
|
|
|
|
|
in if modules == [] then endRecursion else
|
2009-10-12 17:51:21 +00: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;
|
|
|
|
|
|
|
|
hasOptions = values != [];
|
|
|
|
isOption = any lib.isOption values;
|
|
|
|
|
|
|
|
decls = # add location to sub-module options.
|
|
|
|
map (m:
|
|
|
|
mapSubOptions
|
2013-06-07 07:42:46 +00:00
|
|
|
(unifyOptionModule {inherit (m) key;} name)
|
2009-10-12 17:51:21 +00:00
|
|
|
m.options
|
|
|
|
) declarations;
|
|
|
|
|
|
|
|
decl =
|
2009-11-05 16:36:03 +00:00
|
|
|
lib.addErrorContext "${eol
|
2010-05-07 15:13:43 +00:00
|
|
|
}while enhancing option `${addName name}':${eol
|
2009-11-05 16:36:03 +00:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}" (
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls decls)
|
|
|
|
);
|
2009-10-12 17:51:21 +00:00
|
|
|
|
|
|
|
value = decl // (with config; {
|
|
|
|
inherit (config) isNotDefined;
|
2009-11-05 15:39:39 +00:00
|
|
|
isDefined = ! isNotDefined;
|
2009-10-12 17:51:21 +00:00
|
|
|
declarations = declarationSources;
|
|
|
|
definitions = definitionSources;
|
|
|
|
config = strictResult;
|
|
|
|
});
|
|
|
|
|
|
|
|
recurse = (recurseInto name).options;
|
|
|
|
|
|
|
|
result =
|
|
|
|
if isOption then value
|
2009-11-07 01:59:50 +00:00
|
|
|
else if !hasOptions then {}
|
2009-10-12 17:51:21 +00:00
|
|
|
else if all isAttrs values then recurse
|
2009-09-15 13:36:30 +00:00
|
|
|
else
|
|
|
|
throw "${eol
|
|
|
|
}Unexpected type where option declarations are expected.${eol
|
2009-10-12 17:51:21 +00:00
|
|
|
}${errorSource declarations}${eol
|
|
|
|
}";
|
2009-09-15 13:36:30 +00:00
|
|
|
|
2009-10-12 17:51:21 +00: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 15:13:43 +00:00
|
|
|
}while evaluating the option `${addName name}':${eol
|
2009-10-12 17:51:21 +00:00
|
|
|
}${errorSource (modulesOf name)}${eol
|
|
|
|
}" (
|
|
|
|
let opt = option.decl; in
|
|
|
|
opt.apply (
|
|
|
|
if isNotDefined then
|
2013-06-28 19:34:33 +00:00
|
|
|
opt.default or (throw "Option `${addName name}' not defined and does not have a default value.")
|
2009-10-12 17:51:21 +00:00
|
|
|
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 13:36:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
fixMergeModules = initModules: {...}@args:
|
|
|
|
lib.fix (result:
|
2012-11-30 11:56:18 +00:00
|
|
|
# This trick avoids an infinite loop because names of attribute
|
|
|
|
# are know and it is not required to evaluate the result of
|
|
|
|
# moduleMerge to know which attributes are present as arguments.
|
2009-09-15 13:36:30 +00:00
|
|
|
let module = { inherit (result) options config; }; in
|
|
|
|
moduleMerge "" (
|
2009-09-28 18:22:24 +00:00
|
|
|
moduleClosure initModules (module // args)
|
2009-09-15 13:36:30 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2012-11-30 11:56:18 +00:00
|
|
|
|
2009-11-07 01:59:50 +00: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 13:55:08 +00:00
|
|
|
}while checking the attribute `${addName name}':${eol
|
2009-11-07 01:59:50 +00: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 20:10:41 +00:00
|
|
|
}
|