summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-04-10 17:26:25 +0200
committerRobert Hensing <robert@roberthensing.nl>2023-05-06 18:29:04 +0200
commit84b1b017026bb1d0a37a8d3ef553f073225b4e8d (patch)
tree06dfde57ccd9f656389667f53caffb4c3b1115ac
parent1f4a58ef038184eaf4757e96ec1f09b08a01c8ab (diff)
downloadnixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar.gz
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar.bz2
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar.lz
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar.xz
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.tar.zst
nixpkgs-84b1b017026bb1d0a37a8d3ef553f073225b4e8d.zip
lib/modules: Only interpret class declaration in non-shorthand mode
This is to avoid stealing keys from submodules. `class` might be
common enough that reinterpreting existing `class` attributes in
configurations as a declaration leads to fairly widespread problems.
-rw-r--r--lib/modules.nix2
-rwxr-xr-xlib/tests/modules.sh2
-rw-r--r--lib/tests/modules/class-check.nix1
-rw-r--r--lib/tests/modules/define-freeform-keywords-shorthand.nix15
4 files changed, 19 insertions, 1 deletions
diff --git a/lib/modules.nix b/lib/modules.nix
index 9377c2e1e9d..4533252c60e 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -511,7 +511,7 @@ let
         imports = m.require or [] ++ m.imports or [];
         options = {};
         config = addFreeformType (removeAttrs m ["_file" "key" "disabledModules" "require" "imports" "freeformType"]);
-        class = m.class or null;
+        class = null;
       };
 
   applyModuleArgsIfFunction = key: f: args@{ config, options, lib, ... }:
diff --git a/lib/tests/modules.sh b/lib/tests/modules.sh
index c2a8e566cb8..116a0778aeb 100755
--- a/lib/tests/modules.sh
+++ b/lib/tests/modules.sh
@@ -255,6 +255,8 @@ checkConfigError 'A definition for option .* is not of type .*' \
 ## Freeform modules
 # Assigning without a declared option should work
 checkConfigOutput '^"24"$' config.value ./freeform-attrsOf.nix ./define-value-string.nix
+# Shorthand modules interpret `meta` and `class` as config items
+checkConfigOutput '^true$' options._module.args.value.result ./freeform-attrsOf.nix ./define-freeform-keywords-shorthand.nix
 # No freeform assignments shouldn't make it error
 checkConfigOutput '^{ }$' config ./freeform-attrsOf.nix
 # but only if the type matches
diff --git a/lib/tests/modules/class-check.nix b/lib/tests/modules/class-check.nix
index 6e02f8c3092..f492c844abf 100644
--- a/lib/tests/modules/class-check.nix
+++ b/lib/tests/modules/class-check.nix
@@ -25,6 +25,7 @@
           ./module-class-is-nixos.nix
           { _file = "foo.nix#darwinModules.default";
             class = "darwin";
+            config = {};
             imports = [];
           }
         ];
diff --git a/lib/tests/modules/define-freeform-keywords-shorthand.nix b/lib/tests/modules/define-freeform-keywords-shorthand.nix
new file mode 100644
index 00000000000..8de1ec6a747
--- /dev/null
+++ b/lib/tests/modules/define-freeform-keywords-shorthand.nix
@@ -0,0 +1,15 @@
+{ config, ... }: {
+  class = { "just" = "data"; };
+  a = "one";
+  b = "two";
+  meta = "meta";
+
+  _module.args.result =
+    let r = builtins.removeAttrs config [ "_module" ];
+    in builtins.trace (builtins.deepSeq r r) (r == {
+      a = "one";
+      b = "two";
+      class = { "just" = "data"; };
+      meta = "meta";
+    });
+}