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 <unknown-file> was imported into homeManager instead of nixos.

(`<unknown-file>` can be improved by also setting `_file`, if known; a
much older feature)

PR #5339
This commit is contained in:
Robert Hensing 2024-04-26 22:02:25 +02:00 committed by Robert Helgesson
parent 0c5704ecee
commit 26e72d85e6
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED
3 changed files with 8 additions and 1 deletions

View file

@ -55,7 +55,11 @@ let
hmPath = toString ./..; hmPath = toString ./..;
buildOptionsDocs = args@{ modules, includeModuleSystemOptions ? true, ... }: 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 ({ in pkgs.buildPackages.nixosOptionsDoc ({
options = if includeModuleSystemOptions then options = if includeModuleSystemOptions then
options options
@ -160,6 +164,7 @@ in {
inherit lib pkgs; inherit lib pkgs;
check = false; check = false;
} ++ [ scrubbedPkgsModule ]; } ++ [ scrubbedPkgsModule ];
class = "homeManager";
}; };
in builtins.toJSON result.config.meta.maintainers); in builtins.toJSON result.config.meta.maintainers);
} }

View file

@ -25,6 +25,7 @@ let
rawModule = extendedLib.evalModules { rawModule = extendedLib.evalModules {
modules = [ configuration ] ++ hmModules; modules = [ configuration ] ++ hmModules;
class = "homeManager";
specialArgs = { modulesPath = builtins.toString ./.; } // extraSpecialArgs; specialArgs = { modulesPath = builtins.toString ./.; } // extraSpecialArgs;
}; };

View file

@ -13,6 +13,7 @@ let
hmModule = types.submoduleWith { hmModule = types.submoduleWith {
description = "Home Manager module"; description = "Home Manager module";
class = "homeManager";
specialArgs = { specialArgs = {
lib = extendedLib; lib = extendedLib;
osConfig = config; osConfig = config;