1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-20 01:09:37 +01:00

Add poor man's module system

This commit is contained in:
Eelco Dolstra 2020-08-26 09:44:12 +02:00
parent 38b339d447
commit 13735a63b4
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
3 changed files with 52 additions and 1 deletions

View file

@ -1,7 +1,8 @@
corepkgs_FILES = \ corepkgs_FILES = \
unpack-channel.nix \ unpack-channel.nix \
derivation.nix \ derivation.nix \
fetchurl.nix fetchurl.nix \
module.nix
$(foreach file,config.nix $(corepkgs_FILES),$(eval $(call install-data-in,$(d)/$(file),$(datadir)/nix/corepkgs))) $(foreach file,config.nix $(corepkgs_FILES),$(eval $(call install-data-in,$(d)/$(file),$(datadir)/nix/corepkgs)))

46
corepkgs/module.nix Normal file
View file

@ -0,0 +1,46 @@
let
showPos = pos:
if pos == null
then "<unknown location>"
else "${pos.file}:${toString pos.line}:${toString pos.column}";
getAnyPos = attrs:
builtins.foldl' (prev: name: if prev == null then builtins.unsafeGetAttrPos name attrs else prev) null (builtins.attrNames attrs);
in
{ description ? null, extends ? [], options ? {}, config ? ({ config }: {}) } @ inArgs:
let thisModule = rec {
type = "module";
_module = {
inherit description extends options config;
};
_allModules = [thisModule] ++ builtins.concatLists (map (mod: assert mod.type or "<untyped>" == "module"; mod._allModules) extends);
_allOptions = builtins.foldl' (xs: mod: xs // mod._module.options) {} _allModules;
_allConfigs = map (mod: mod._module.config { config = final; }) _allModules;
_allDefinitions = builtins.mapAttrs (name: value: map (x: x) (builtins.catAttrs name _allConfigs)) _allOptions;
final = builtins.mapAttrs
(name: defs:
if defs == []
then
_allOptions.${name}.default
or (throw "Option '${name}' is not defined by module at ${showPos (getAnyPos inArgs)} and has no default value.")
else
# FIXME: support merge functions.
if builtins.isList (builtins.head defs)
then builtins.concatLists defs
else
if builtins.isAttrs (builtins.head defs)
then builtins.foldl' (xs: ys: xs // ys) {} defs
else builtins.head defs)
_allDefinitions;
}; in thisModule

View file

@ -2416,6 +2416,10 @@ void EvalState::createBaseEnv()
evalFile(path, v); evalFile(path, v);
addConstant("derivation", v); addConstant("derivation", v);
path = canonPath(settings.nixDataDir + "/nix/corepkgs/module.nix", true);
evalFile(path, v);
addConstant("module", v);
/* Add a value containing the current Nix expression search path. */ /* Add a value containing the current Nix expression search path. */
mkList(v, searchPath.size()); mkList(v, searchPath.size());
int n = 0; int n = 0;