From b7456195bd1379f45d908f6d0a3563ad7afe123e Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sun, 2 Jun 2024 01:01:38 -0700 Subject: [PATCH] feat(lib): withDynamicArgs, add basic module system tests --- lib/src/fp/default.nix | 12 ++++++++ lib/src/modules/default.nix | 2 +- lib/src/modules/default.test.nix | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/src/fp/default.nix b/lib/src/fp/default.nix index 1b3bc55..57fd5a2 100644 --- a/lib/src/fp/default.nix +++ b/lib/src/fp/default.nix @@ -42,5 +42,17 @@ lib: { if f ? __functor then f.__args__ or lib.fp.args (f.__functor f) else builtins.functionArgs f; + + # TODO: Document this. + withDynamicArgs = f: args: let + fArgs = lib.fp.args f; + common = builtins.intersectAttrs fArgs args; + in + if builtins.isAttrs args + then + if fArgs == {} + then f args + else f common + else f args; }; } diff --git a/lib/src/modules/default.nix b/lib/src/modules/default.nix index e37e0c3..b956596 100644 --- a/lib/src/modules/default.nix +++ b/lib/src/modules/default.nix @@ -172,7 +172,7 @@ lib: { (lib.fp.args module); in if builtins.isFunction module - then module (args // dynamicArgs) + then lib.fp.withDynamicArgs module (args // dynamicArgs) else module; # TODO: Document this. diff --git a/lib/src/modules/default.test.nix b/lib/src/modules/default.test.nix index d379070..6ce3c10 100644 --- a/lib/src/modules/default.test.nix +++ b/lib/src/modules/default.test.nix @@ -3,6 +3,25 @@ let in { examples = { "empty" = let + evaluated = lib.modules.run { + modules = [ + { + options.aux = { + message = lib.options.create { + type = lib.types.string; + }; + }; + + config = { + aux.message = "Hello, World!"; + }; + } + ]; + }; + in + evaluated ? config; + + "hello world" = let expected = "Hello, World!"; evaluated = lib.modules.run { @@ -21,6 +40,36 @@ in { ]; }; + actual = evaluated.config.aux.message; + in + actual == expected; + + "recursive" = let + expected = "Hello, World!"; + + evaluated = lib.modules.run { + modules = [ + ({config}: { + options.aux = { + message = lib.options.create { + type = lib.types.string; + }; + + proxy = lib.options.create { + type = lib.types.string; + }; + }; + + config = { + aux = { + proxy = "Hello, World!"; + message = config.aux.proxy; + }; + }; + }) + ]; + }; + actual = evaluated.config.aux.message; in actual == expected;