From 851edc8df1347aef556a646c80d469a3137331ba Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sun, 4 Aug 2024 21:54:17 +0100 Subject: [PATCH] lib/modules: add `evalNixvim` Evaluates a nixvim config, by default also checking warnings and assertions. This used internally by the standalone wrapper. --- lib/modules.nix | 34 ++++++++++++++++++++++++++++++++++ wrappers/standalone.nix | 28 ++++++++-------------------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/modules.nix b/lib/modules.nix index eedbdca9..b6403de8 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -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)) + ]; } diff --git a/wrappers/standalone.nix b/wrappers/standalone.nix index 16cc2e80..cc817f22 100644 --- a/wrappers/standalone.nix +++ b/wrappers/standalone.nix @@ -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 {