From 4a014ed92e468cce5ba375d086f61fa31512682b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 3 Jul 2024 10:41:01 +0200 Subject: [PATCH] lib/modules: Use fixed lib instead of args.lib The practical use for this should be very limited because I don't think anyone should change `lib`, let alone change `lib.functionArgs`, but, but it would be even stranger to rely on `args.lib` (or really `specialArgs.lib` for what's clearly a behavior of the current `evalModules`, which uses its own ambient lib for basically everything. The shadowing of `lib` by `args.lib` here seems to be a small mistake, which is easy to make. --- lib/modules.nix | 4 ++-- lib/tests/modules.sh | 2 ++ lib/tests/modules/specialArgs-lib.nix | 28 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 lib/tests/modules/specialArgs-lib.nix diff --git a/lib/modules.nix b/lib/modules.nix index 79892f50c4fe..44de50769633 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -485,10 +485,10 @@ let config = addFreeformType (removeAttrs m ["_class" "_file" "key" "disabledModules" "require" "imports" "freeformType"]); }; - applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }: + applyModuleArgsIfFunction = key: f: args@{ config, ... }: if isFunction f then applyModuleArgs key f args else f; - applyModuleArgs = key: f: args@{ config, options, lib, ... }: + applyModuleArgs = key: f: args@{ config, ... }: let # Module arguments are resolved in a strict manner when attribute set # deconstruction is used. As the arguments are now defined with the diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh index 750b1d025e02..280d0b47d574 100755 --- a/lib/tests/modules.sh +++ b/lib/tests/modules.sh @@ -94,6 +94,8 @@ checkConfigOutput '^true$' config.result ./module-argument-default.nix # gvariant checkConfigOutput '^true$' config.assertion ./gvariant.nix +checkConfigOutput '"ok"' config.result ./specialArgs-lib.nix + # https://github.com/NixOS/nixpkgs/pull/131205 # We currently throw this error already in `config`, but throwing in `config.wrong1` would be acceptable. checkConfigError 'It seems as if you.re trying to declare an option by placing it into .config. rather than .options.' config.wrong1 ./error-mkOption-in-config.nix diff --git a/lib/tests/modules/specialArgs-lib.nix b/lib/tests/modules/specialArgs-lib.nix new file mode 100644 index 000000000000..8c9d2103862a --- /dev/null +++ b/lib/tests/modules/specialArgs-lib.nix @@ -0,0 +1,28 @@ +{ config, lib, ... }: + +{ + options = { + result = lib.mkOption { }; + weird = lib.mkOption { + type = lib.types.submoduleWith { + # I generally recommend against overriding lib, because that leads to + # slightly incompatible dialects of the module system. + # Nonetheless, it's worth guarding the property that the module system + # evaluates with a completely custom lib, as a matter of separation of + # concerns. + specialArgs.lib = { }; + modules = [ ]; + }; + }; + }; + config.weird = args@{ ... /* note the lack of a `lib` argument */ }: + assert args.lib == { }; + assert args.specialArgs == { lib = { }; }; + { + options.foo = lib.mkOption { }; + config.foo = lib.mkIf true "alright"; + }; + config.result = + assert config.weird.foo == "alright"; + "ok"; +}