Merge pull request #54637 from danbst/small-eval-optimization

module system: small eval optimization
This commit is contained in:
Danylo Hlynskyi 2019-01-31 00:42:24 +02:00 committed by GitHub
commit 30c312341f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 60 additions and 69 deletions

View File

@ -214,23 +214,25 @@ rec {
qux = [ "module.hidden=baz,value=bar" "module.hidden=fli,value=gne" ]; qux = [ "module.hidden=baz,value=bar" "module.hidden=fli,value=gne" ];
} }
*/ */
byName = attr: f: modules: foldl' (acc: module: byName = attr: f: modules:
foldl' (inner: name: foldl' (acc: module:
inner // { ${name} = (acc.${name} or []) ++ (f module module.${attr}.${name}); } acc // (mapAttrs (n: v:
) acc (attrNames module.${attr}) (acc.${n} or []) ++ f module v
) {} modules; ) module.${attr}
)
) {} modules;
# an attrset 'name' => list of submodules that declare name. # an attrset 'name' => list of submodules that declare name.
declsByName = byName "options" declsByName = byName "options" (module: option:
(module: option: [{ inherit (module) file; options = option; }]) [{ inherit (module) file; options = option; }]
options; ) options;
# an attrset 'name' => list of submodules that define name. # an attrset 'name' => list of submodules that define name.
defnsByName = byName "config" (module: value: defnsByName = byName "config" (module: value:
map (config: { inherit (module) file; inherit config; }) (pushDownProperties value) map (config: { inherit (module) file; inherit config; }) (pushDownProperties value)
) configs; ) configs;
# extract the definitions for each loc # extract the definitions for each loc
defnsByName' = byName "config" defnsByName' = byName "config" (module: value:
(module: value: [{ inherit (module) file; inherit value; }]) [{ inherit (module) file; inherit value; }]
configs; ) configs;
in in
(flip mapAttrs declsByName (name: decls: (flip mapAttrs declsByName (name: decls:
# We're descending into attribute name. # We're descending into attribute name.
@ -362,7 +364,6 @@ rec {
values = defs'''; values = defs''';
inherit (defs'') highestPrio; inherit (defs'') highestPrio;
}; };
defsFinal = defsFinal'.values; defsFinal = defsFinal'.values;
# Type-check the remaining definitions, and merge them. # Type-check the remaining definitions, and merge them.
@ -475,22 +476,8 @@ rec {
optionSet to options of type submodule. FIXME: remove optionSet to options of type submodule. FIXME: remove
eventually. */ eventually. */
fixupOptionType = loc: opt: fixupOptionType = loc: opt:
let if opt.type.getSubModules or null == null
options = opt.options or then opt // { type = opt.type or types.unspecified; }
(throw "Option `${showOption loc'}' has type optionSet but has no option attribute, in ${showFiles opt.declarations}.");
f = tp:
let optionSetIn = type: (tp.name == type) && (tp.functor.wrapped.name == "optionSet");
in
if tp.name == "option set" || tp.name == "submodule" then
throw "The option ${showOption loc} uses submodules without a wrapping type, in ${showFiles opt.declarations}."
else if optionSetIn "attrsOf" then types.attrsOf (types.submodule options)
else if optionSetIn "loaOf" then types.loaOf (types.submodule options)
else if optionSetIn "listOf" then types.listOf (types.submodule options)
else if optionSetIn "nullOr" then types.nullOr (types.submodule options)
else tp;
in
if opt.type.getSubModules or null == null
then opt // { type = f (opt.type or types.unspecified); }
else opt // { type = opt.type.substSubModules opt.options; options = []; }; else opt // { type = opt.type.substSubModules opt.options; options = []; };

View File

@ -48,8 +48,6 @@ rec {
visible ? null, visible ? null,
# Whether the option can be set only once # Whether the option can be set only once
readOnly ? null, readOnly ? null,
# Obsolete, used by types.optionSet.
options ? null
} @ attrs: } @ attrs:
attrs // { _type = "option"; }; attrs // { _type = "option"; };

View File

@ -284,8 +284,7 @@ rec {
(mergeDefinitions (loc ++ [name]) elemType defs).optionalValue (mergeDefinitions (loc ++ [name]) elemType defs).optionalValue
) )
# Push down position info. # Push down position info.
(map (def: listToAttrs (mapAttrsToList (n: def': (map (def: mapAttrs (n: v: { inherit (def) file; value = v; }) def.value) defs)));
{ name = n; value = { inherit (def) file; value = def'; }; }) def.value)) defs)));
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]); getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]);
getSubModules = elemType.getSubModules; getSubModules = elemType.getSubModules;
substSubModules = m: attrsOf (elemType.substSubModules m); substSubModules = m: attrsOf (elemType.substSubModules m);
@ -470,10 +469,7 @@ rec {
# Obsolete alternative to configOf. It takes its option # Obsolete alternative to configOf. It takes its option
# declarations from the options attribute of containing option # declarations from the options attribute of containing option
# declaration. # declaration.
optionSet = mkOptionType { optionSet = builtins.throw "types.optionSet is deprecated; use types.submodule instead" "optionSet";
name = builtins.trace "types.optionSet is deprecated; use types.submodule instead" "optionSet";
description = "option set";
};
# Augment the given type with an additional type check function. # Augment the given type with an additional type check function.
addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; }; addCheck = elemType: check: elemType // { check = x: elemType.check x && check x; };

View File

@ -370,6 +370,14 @@
for details. for details.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
Support for NixOS module system type <literal>types.optionSet</literal> and
<literal>lib.mkOption</literal> argument <literal>options</literal> is removed.
Use <literal>types.submodule</literal> instead.
(<link xlink:href="https://github.com/NixOS/nixpkgs/pull/54637">#54637</link>)
</para>
</listitem>
</itemizedlist> </itemizedlist>
</section> </section>

View File

@ -13,5 +13,5 @@ with lib;
documentation.enable = mkDefault false; documentation.enable = mkDefault false;
services.nixosManual.enable = mkDefault false; documentation.nixos.enable = mkDefault false;
} }

View File

@ -142,7 +142,6 @@ in
description = "Collection of named nylon instances"; description = "Collection of named nylon instances";
type = with types; loaOf (submodule nylonOpts); type = with types; loaOf (submodule nylonOpts);
internal = true; internal = true;
options = [ nylonOpts ];
}; };
}; };

View File

@ -11,7 +11,7 @@ let
userOptions = { userOptions = {
openssh.authorizedKeys = { options.openssh.authorizedKeys = {
keys = mkOption { keys = mkOption {
type = types.listOf types.str; type = types.listOf types.str;
default = []; default = [];
@ -320,7 +320,7 @@ in
}; };
users.users = mkOption { users.users = mkOption {
options = [ userOptions ]; type = with types; loaOf (submodule userOptions);
}; };
}; };

View File

@ -525,16 +525,18 @@ in
}; };
fileSystems = mkOption { fileSystems = mkOption {
options.neededForBoot = mkOption { type = with lib.types; loaOf (submodule {
default = false; options.neededForBoot = mkOption {
type = types.bool; default = false;
description = '' type = types.bool;
If set, this file system will be mounted in the initial description = ''
ramdisk. By default, this applies to the root file system If set, this file system will be mounted in the initial
and to the file system containing ramdisk. By default, this applies to the root file system
<filename>/nix/store</filename>. and to the file system containing
''; <filename>/nix/store</filename>.
}; '';
};
});
}; };
}; };

View File

@ -12,28 +12,28 @@ let
encryptedFSOptions = { encryptedFSOptions = {
encrypted = { options.encrypted = {
enable = mkOption { enable = mkOption {
default = false; default = false;
type = types.bool; type = types.bool;
description = "The block device is backed by an encrypted one, adds this device as a initrd luks entry."; description = "The block device is backed by an encrypted one, adds this device as a initrd luks entry.";
}; };
blkDev = mkOption { options.blkDev = mkOption {
default = null; default = null;
example = "/dev/sda1"; example = "/dev/sda1";
type = types.nullOr types.str; type = types.nullOr types.str;
description = "Location of the backing encrypted device."; description = "Location of the backing encrypted device.";
}; };
label = mkOption { options.label = mkOption {
default = null; default = null;
example = "rootfs"; example = "rootfs";
type = types.nullOr types.str; type = types.nullOr types.str;
description = "Label of the unlocked encrypted device. Set <literal>fileSystems.&lt;name?&gt;.device</literal> to <literal>/dev/mapper/&lt;label&gt;</literal> to mount the unlocked device."; description = "Label of the unlocked encrypted device. Set <literal>fileSystems.&lt;name?&gt;.device</literal> to <literal>/dev/mapper/&lt;label&gt;</literal> to mount the unlocked device.";
}; };
keyFile = mkOption { options.keyFile = mkOption {
default = null; default = null;
example = "/mnt-root/root/.swapkey"; example = "/mnt-root/root/.swapkey";
type = types.nullOr types.str; type = types.nullOr types.str;
@ -47,10 +47,10 @@ in
options = { options = {
fileSystems = mkOption { fileSystems = mkOption {
options = [encryptedFSOptions]; type = with lib.types; loaOf (submodule encryptedFSOptions);
}; };
swapDevices = mkOption { swapDevices = mkOption {
options = [encryptedFSOptions]; type = with lib.types; listOf (submodule encryptedFSOptions);
}; };
}; };

View File

@ -92,23 +92,24 @@ let
exit($mainRes & 127 ? 255 : $mainRes << 8); exit($mainRes & 127 ? 255 : $mainRes << 8);
''; '';
opts = { config, name, ... }: {
options.runner = mkOption {
internal = true;
description = ''
A script that runs the service outside of systemd,
useful for testing or for using NixOS services outside
of NixOS.
'';
};
config.runner = makeScript name config;
};
in in
{ {
options = { options = {
systemd.services = mkOption { systemd.services = mkOption {
options = type = with types; attrsOf (submodule opts);
{ config, name, ... }:
{ options.runner = mkOption {
internal = true;
description = ''
A script that runs the service outside of systemd,
useful for testing or for using NixOS services outside
of NixOS.
'';
};
config.runner = makeScript name config;
};
}; };
}; };
} }