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:
|
|
|
|
removeAttrs (delayProperties m) ["require"];
|
|
|
|
in
|
2009-09-15 07:36:30 -06:00
|
|
|
if isModule m then
|
2009-09-14 07:19:00 -06:00
|
|
|
m
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imports = getImportedPaths m;
|
|
|
|
config = getConfig m;
|
|
|
|
} // (
|
|
|
|
if getImportedSets m != [] then
|
|
|
|
assert tail (getImportedSets m) == [];
|
|
|
|
{ options = head (getImportedSets m); }
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
moduleClosure = initModules: args:
|
|
|
|
let
|
2009-09-14 18:21:39 -06:00
|
|
|
moduleImport = m:
|
|
|
|
(unifyModuleSyntax (applyIfFunction (importIfPath m) args)) // {
|
2009-09-14 07:19:00 -06:00
|
|
|
# used by generic closure to avoid duplicated imports.
|
2009-09-14 18:21:39 -06:00
|
|
|
key = if isPath m then m else
|
|
|
|
/bad/developer/implies/bad/error/messages;
|
|
|
|
};
|
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 {
|
|
|
|
startSet = map moduleImport (filter isPath initModules);
|
2009-09-14 07:19:00 -06:00
|
|
|
operator = m: map moduleImport (getImports m);
|
2009-09-14 18:21:39 -06:00
|
|
|
}) ++ (map moduleImport (filter (m: ! isPath m) initModules));
|
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;
|
|
|
|
|
|
|
|
delayModule = module:
|
|
|
|
moduleApply { config = delayProperties; } module;
|
|
|
|
|
|
|
|
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:
|
|
|
|
fun (
|
|
|
|
map (lib.getAttr name) (
|
|
|
|
filter (builtins.hasAttr name) modules
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) 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);
|
|
|
|
|
|
|
|
recurseInto = name: modules:
|
|
|
|
moduleMerge (addName name) (modulesOf name);
|
|
|
|
|
|
|
|
recurseForOption = name: modules:
|
|
|
|
moduleMerge name (
|
|
|
|
map unifyModuleSyntax modules
|
|
|
|
);
|
|
|
|
|
|
|
|
errorSource = modules:
|
|
|
|
"The error may comes from the following files:\n" + (
|
|
|
|
lib.concatStringsSep "\n" (
|
|
|
|
map (m:
|
|
|
|
if m ? key then toString m.key else "<unknow location>"
|
|
|
|
) modules
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
eol = "\n";
|
|
|
|
|
|
|
|
errDefinedWithoutDeclaration = name:
|
|
|
|
let
|
|
|
|
badModules =
|
|
|
|
filter (m: ! isAttrs m.config)
|
|
|
|
(definitionsOf name);
|
|
|
|
in
|
|
|
|
"${eol
|
|
|
|
}Option '${addName name}' defined without option declaration.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}";
|
|
|
|
|
|
|
|
endRecursion = { options = {}; config = {}; };
|
|
|
|
|
|
|
|
in if modules == [] then endRecursion else
|
|
|
|
|
|
|
|
lib.fix (result:
|
|
|
|
moduleZip {
|
|
|
|
options = lib.zip (name: values:
|
|
|
|
if any isOption values then
|
|
|
|
addOptionMakeUp
|
|
|
|
{ name = addName name; recurseInto = recurseForOption; }
|
|
|
|
(mergeOptionDecls values)
|
|
|
|
else if all isAttrs values then
|
|
|
|
(recurseInto name modules).options
|
|
|
|
else
|
|
|
|
throw "${eol
|
|
|
|
}Unexpected type where option declarations are expected.${eol
|
|
|
|
}${errorSource (declarationsOf name)}${eol
|
|
|
|
}"
|
|
|
|
);
|
|
|
|
|
|
|
|
config = lib.zipWithNames (modulesNames modules) (name: values:
|
|
|
|
let
|
|
|
|
hasOpt = builtins.hasAttr name result.options;
|
|
|
|
opt = lib.getAttr name result.options;
|
|
|
|
|
|
|
|
in if hasOpt && isOption opt then
|
|
|
|
let defs = evalProperties values; in
|
|
|
|
lib.addErrorContext "${eol
|
|
|
|
}while evaluating the option '${addName name}'.${eol
|
|
|
|
}${errorSource (modulesOf name)}${eol
|
|
|
|
}" (
|
|
|
|
opt.apply (
|
|
|
|
if defs == [] then
|
|
|
|
if opt ? default then opt.default
|
|
|
|
else throw "Not defined."
|
|
|
|
else opt.merge defs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
else if hasOpt && lib.attrNames opt == [] then
|
|
|
|
throw (errDefinedWithoutDeclaration name)
|
|
|
|
|
|
|
|
else if any (v: isOption (rmProperties v)) values then
|
|
|
|
let
|
|
|
|
badModules =
|
|
|
|
filter (m: isOption m.config)
|
|
|
|
(definitionsOf name);
|
|
|
|
in
|
|
|
|
throw "${eol
|
|
|
|
}Option ${addName name} is defined in the configuration section.${eol
|
|
|
|
}${errorSource badModules}${eol
|
|
|
|
}"
|
|
|
|
|
|
|
|
else if all isAttrs values then
|
|
|
|
(recurseInto name modules).config
|
|
|
|
else
|
|
|
|
throw (errDefinedWithoutDeclaration name)
|
|
|
|
);
|
|
|
|
|
|
|
|
} modules
|
|
|
|
);
|
|
|
|
|
|
|
|
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 "" (
|
|
|
|
moduleClosure initModules (args // module)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-09-14 14:10:41 -06:00
|
|
|
}
|