Skip to content

Commit

Permalink
Set module class to "homeManager"
Browse files Browse the repository at this point in the history
This enables a module system feature documented here:
https://nixos.org/manual/nixpkgs/stable/index.html#module-system-lib-evalModules-param-class

This allows the mistake of loading a NixOS module into home-manager
to be caught, provided that a module declares what it's for with
a `_class` attribute.

The class feature has been available in the module system since
NixOS/nixpkgs#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.

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).

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)
  • Loading branch information
roberth committed Apr 26, 2024
1 parent 0c5704e commit ed11151
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -160,6 +160,7 @@ in {
inherit lib pkgs;
check = false;
} ++ [ scrubbedPkgsModule ];
class = "homeManager";
};
in builtins.toJSON result.config.meta.maintainers);
}
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let
rawModule = extendedLib.evalModules {
modules = [ configuration ] ++ hmModules;
specialArgs = { modulesPath = builtins.toString ./.; } // extraSpecialArgs;
class = "homeManager";
};

moduleChecks = raw:
Expand Down
1 change: 1 addition & 0 deletions nixos/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let

hmModule = types.submoduleWith {
description = "Home Manager module";
class = "homeManager";
specialArgs = {
lib = extendedLib;
osConfig = config;
Expand Down

0 comments on commit ed11151

Please sign in to comment.