Skip to content

Commit

Permalink
lib/modules: add evalNixvim
Browse files Browse the repository at this point in the history
Evaluates a nixvim config, by default also checking warnings and assertions.

This used internally by the standalone wrapper.
  • Loading branch information
MattSturgeon committed Aug 20, 2024
1 parent bc7a7ad commit 851edc8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
34 changes: 34 additions & 0 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,38 @@ rec {
defaultPkgs = pkgs;
}
// extraSpecialArgs;

# Evaluate nixvim modules, checking warnings and assertions
evalNixvim =
{
modules ? [ ],
extraSpecialArgs ? { },
# Set to false to disable warnings and assertions
# Intended for use with tests, where we may not want to check assertions
# WARNING: This argument may be removed without notice:
check ? true,
}:
let
result = lib.evalModules {
modules = [ ../modules/top-level ] ++ modules;
specialArgs = specialArgsWith extraSpecialArgs;
};

failedAssertions = getAssertionMessages result.config.assertions;

checked =
if failedAssertions != [ ] then
throw "\nFailed assertions:\n${lib.concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
else
lib.showWarnings result.config.warnings result;
in
if check then checked else result;

# Return the messages for all assertions that failed
getAssertionMessages =
assertions:
lib.pipe assertions [
(lib.filter (x: !x.assertion))
(lib.map (x: x.message))
];
}
28 changes: 8 additions & 20 deletions wrappers/standalone.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,30 @@ default_pkgs: self:
let
helpers = import ../lib/helpers.nix { inherit pkgs lib _nixvimTests; };

inherit (helpers.modules) specialArgsWith;

handleAssertions =
config:
let
failedAssertions = map (x: x.message) (lib.filter (x: !x.assertion) config.assertions);
in
if failedAssertions != [ ] then
throw "\nFailed assertions:\n${builtins.concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
else
lib.showWarnings config.warnings config;
inherit (helpers.modules) evalNixvim;

mkNvim =
mod:
let
evaledModule = lib.evalModules {
evaledModule = evalNixvim {
modules = [
mod
./modules/standalone.nix
../modules/top-level
];
specialArgs = specialArgsWith extraSpecialArgs;
inherit extraSpecialArgs;
};
config = handleAssertions evaledModule.config;
inherit (evaledModule.config) enableMan finalPackage printInitPackage;
in
(pkgs.symlinkJoin {
name = "nixvim";
paths = [
config.finalPackage
config.printInitPackage
] ++ pkgs.lib.optional config.enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
finalPackage
printInitPackage
] ++ pkgs.lib.optional enableMan self.packages.${pkgs.stdenv.hostPlatform.system}.man-docs;
meta.mainProgram = "nvim";
})
// rec {
inherit config;
inherit (evaledModule) options;
inherit (evaledModule) config options;
extend =
extension:
mkNvim {
Expand Down

0 comments on commit 851edc8

Please sign in to comment.