From b696a983bcd0086792d2d5d3e64704b89af3ff9e Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 8 Dec 2025 01:23:36 +0100 Subject: [PATCH] flakeModules.modules: Make values valid definitions for plain submodule --- dev/tests/eval-tests.nix | 50 +++++++++++++++++++++++++++++----------- extras/modules.nix | 14 +++++++---- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/dev/tests/eval-tests.nix b/dev/tests/eval-tests.nix index f0de2fb..e719632 100644 --- a/dev/tests/eval-tests.nix +++ b/dev/tests/eval-tests.nix @@ -95,23 +95,42 @@ rec { }; }; - modulesFlake = mkFlake - { - inputs.self = { }; - moduleLocation = "modulesFlake"; - } - { - imports = [ flake-parts.flakeModules.modules ]; - systems = [ ]; - flake = { - modules.generic.example = { lib, ... }: { - options.generic.example = lib.mkOption { default = "works in any module system application"; }; + modulesFlake = + mkFlake + { + inputs.self = { }; + moduleLocation = "modulesFlake"; + } + { + imports = [ flake-parts.flakeModules.modules ]; + options = { + # Test option that uses plain types.submodule + flake.fooConfiguration = lib.mkOption { + type = lib.types.submoduleWith { + # Just Like types.submodule; + shorthandOnlyDefinesConfig = true; + class = "foo"; + modules = [ ]; + }; + }; }; - modules.foo.example = { lib, ... }: { - options.foo.example = lib.mkOption { default = "works in foo application"; }; + config = { + systems = [ ]; + flake = { + modules.generic.example = + { lib, ... }: + { + options.generic.example = lib.mkOption { default = "works in any module system application"; }; + }; + modules.foo.example = + { lib, ... }: + { + options.foo.example = lib.mkOption { default = "works in foo application"; }; + }; + fooConfiguration = modulesFlake.modules.foo.example; + }; }; }; - }; flakeModulesDeclare = mkFlake { inputs.self = { outPath = ./.; }; } @@ -315,6 +334,9 @@ rec { ]; }).config.foo.example == "works in foo application"; + # Test that modules can be loaded into plain submodules with shorthandOnlyDefinesConfig = true + assert modulesFlake.fooConfiguration.foo.example == "works in foo application"; + assert specialArgFlake.foo; assert builtins.isAttrs partitionWithoutExtraInputsFlake.devShells.x86_64-linux; diff --git a/extras/modules.nix b/extras/modules.nix index 98e9a96..7902a57 100644 --- a/extras/modules.nix +++ b/extras/modules.nix @@ -9,13 +9,19 @@ let escapeNixIdentifier ; - addInfo = class: moduleName: - if class == "generic" - then module: module + addInfo = + class: moduleName: + if class == "generic" then + module: module else module: - # TODO: set key? + # By returning a function, it will be accepted as a full-on module despite + # a submodule having shorthandOnlyDefinesConfig = true, which is the + # `types.submodule` default. + # https://github.com/hercules-ci/flake-parts/issues/326 + { ... }: { + # TODO: set key? _class = class; _file = "${toString moduleLocation}#modules.${escapeNixIdentifier class}.${escapeNixIdentifier moduleName}"; imports = [ module ];