mirror of
https://github.com/nix-community/home-manager
synced 2024-11-09 12:49:44 +01:00
26e72d85e6
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
62 lines
1.6 KiB
Nix
62 lines
1.6 KiB
Nix
{ configuration, pkgs, lib ? pkgs.lib
|
||
|
||
# Whether to check that each option has a matching declaration.
|
||
, check ? true
|
||
# Extra arguments passed to specialArgs.
|
||
, extraSpecialArgs ? { } }:
|
||
|
||
with lib;
|
||
|
||
let
|
||
|
||
collectFailed = cfg:
|
||
map (x: x.message) (filter (x: !x.assertion) cfg.assertions);
|
||
|
||
showWarnings = res:
|
||
let f = w: x: builtins.trace "[1;31mwarning: ${w}[0m" x;
|
||
in fold f res res.config.warnings;
|
||
|
||
extendedLib = import ./lib/stdlib-extended.nix lib;
|
||
|
||
hmModules = import ./modules.nix {
|
||
inherit check pkgs;
|
||
lib = extendedLib;
|
||
};
|
||
|
||
rawModule = extendedLib.evalModules {
|
||
modules = [ configuration ] ++ hmModules;
|
||
class = "homeManager";
|
||
specialArgs = { modulesPath = builtins.toString ./.; } // extraSpecialArgs;
|
||
};
|
||
|
||
moduleChecks = raw:
|
||
showWarnings (let
|
||
failed = collectFailed raw.config;
|
||
failedStr = concatStringsSep "\n" (map (x: "- ${x}") failed);
|
||
in if failed == [ ] then
|
||
raw
|
||
else
|
||
throw ''
|
||
|
||
Failed assertions:
|
||
${failedStr}'');
|
||
|
||
withExtraAttrs = rawModule:
|
||
let module = moduleChecks rawModule;
|
||
in {
|
||
inherit (module) options config;
|
||
|
||
activationPackage = module.config.home.activationPackage;
|
||
|
||
# For backwards compatibility. Please use activationPackage instead.
|
||
activation-script = module.config.home.activationPackage;
|
||
|
||
newsDisplay = rawModule.config.news.display;
|
||
newsEntries = sort (a: b: a.time > b.time)
|
||
(filter (a: a.condition) rawModule.config.news.entries);
|
||
|
||
inherit (module._module.args) pkgs;
|
||
|
||
extendModules = args: withExtraAttrs (rawModule.extendModules args);
|
||
};
|
||
in withExtraAttrs rawModule
|