From 830dd996ea485f4e725b429a009fbced50caddec Mon Sep 17 00:00:00 2001 From: Eman Resu <78693624+quatquatt@users.noreply.github.com> Date: Wed, 3 Jun 2026 13:49:00 -0400 Subject: [PATCH] lib.modules: avoid filtering option decls in most cases --- lib/modules.nix | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index 8b26f2c462e8..a42690b9a0c1 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -881,13 +881,12 @@ let loc = prefix ++ [ name ]; defns = pushedDownDefinitionsByName.${name} or [ ]; defns' = rawDefinitionsByName.${name} or [ ]; - optionDecls = filter ( + isOptionDecl = m: m.options ? _type - && (m.options._type == "option" || throwDeclarationTypeError loc m.options._type m._file) - ) decls; + && (m.options._type == "option" || throwDeclarationTypeError loc m.options._type m._file); in - if length optionDecls == length decls then + if all isOptionDecl decls then let opt = fixupOptionType loc (mergeOptionDecls loc decls); in @@ -895,7 +894,35 @@ let matchedOptions = evalOptionValue loc opt defns'; unmatchedDefns = [ ]; } - else if optionDecls != [ ] then + # this may look like duplicate computations are performed, but testing + # with a minimal NixOS config (defined below): + # + # 1. in 87% of cases, the above `all isOptionDecl decls` passes and we + # short-circuit + # 2. In 12% of cases, there's only one decl and we short-circuit + # 3. in another 1% of cases, we loop again and short-circuit when + # `any isOptionDecl decls` fails + # 4. In the final case, we pass the `any` and have to refilter. This + # only triggers in 1 of the 48000 calls to this function. + # + # The minimal config was defined with this expression: + # + # let nixos = import ./nixos/lib/eval-config.nix { + # modules = [ + # ./nixos/modules/profiles/minimal.nix + # { + # fileSystems."/" = { + # device = "/dev/sda1"; + # fsType = "ext4"; + # }; + # boot.loader.grub.devices = [ "/dev/sda" ]; + # } + # ]; + # }; in nixos.config.system.build.toplevel + else if length decls != 1 && any isOptionDecl decls then + let + optionDecls = filter isOptionDecl decls; + in if all (x: x.options.type.name or null == "submodule") optionDecls # Raw options can only be merged into submodules. Merging into