From 5b950d50103f879b4bb5ff6ec2e58a86da6fa818 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 26 Apr 2024 22:02:25 +0200 Subject: [PATCH] home-manager: Set module class to "homeManager" This enables a module system feature documented here: https://nixos.org/manual/nixpkgs/stable/index.html#module-system-lib-evalModules-param-class For example, it allows a mistake to be caught, which is loading a NixOS module into home-manager. This only works when the offending module declares what it's for with a `_class` attribute. It is not expected that users declare the `_type`, because the payoff is small. It is only expected to be set by generic code, such as functions or libraries that help with the "publishing" of modules (e.g. flake-parts, flake-utils). The class feature has been available in the module system since https://github.com/NixOS/nixpkgs/pull/197547, merged May 6, 2023. It has been part of all releases since 23.05-beta. The last NixOS release that did _not_ support it has been end-of-life for close to a year now. Example: (lib.homeManagerConfiguration { pkgs = nixpkgs.legacyPackages.x86_64-linux; modules = [{ _class = "nixos"; imports = [ ./foo.nix ]; }]; }).activation-script Corresponding error: error: The module was imported into homeManager instead of nixos. (`` can be improved by also setting `_file`, if known; a much older feature) --- docs/default.nix | 3 ++- modules/default.nix | 1 + nixos/common.nix | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/default.nix b/docs/default.nix index 422e5ef80bce..5965e1ab4f3f 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -55,7 +55,7 @@ let hmPath = toString ./..; buildOptionsDocs = args@{ modules, includeModuleSystemOptions ? true, ... }: - let options = (lib.evalModules { inherit modules; }).options; + let options = (lib.evalModules { inherit modules; class = "homeManager"; }).options; in pkgs.buildPackages.nixosOptionsDoc ({ options = if includeModuleSystemOptions then options @@ -160,6 +160,7 @@ in { inherit lib pkgs; check = false; } ++ [ scrubbedPkgsModule ]; + class = "homeManager"; }; in builtins.toJSON result.config.meta.maintainers); } diff --git a/modules/default.nix b/modules/default.nix index ad0e67646688..b4b5be0d93b7 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -26,6 +26,7 @@ let rawModule = extendedLib.evalModules { modules = [ configuration ] ++ hmModules; specialArgs = { modulesPath = builtins.toString ./.; } // extraSpecialArgs; + class = "homeManager"; }; moduleChecks = raw: diff --git a/nixos/common.nix b/nixos/common.nix index 5da3240b97b2..08491be9b1ef 100644 --- a/nixos/common.nix +++ b/nixos/common.nix @@ -13,6 +13,7 @@ let hmModule = types.submoduleWith { description = "Home Manager module"; + class = "homeManager"; specialArgs = { lib = extendedLib; osConfig = config;