From c1f449137f7a9950d053ca892d9aab04123adc67 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 6 Nov 2023 12:33:38 +0300 Subject: [PATCH] treewide: cleanup --- lib/default.nix | 1 + lib/lua.nix | 51 ++++++- lib/vim.nix | 26 ++++ modules/assertions.nix | 27 ---- modules/basic/config.nix | 8 +- modules/basic/module.nix | 7 +- modules/core/default.nix | 279 ++++++++++++++++++--------------------- modules/default.nix | 6 +- modules/modules.nix | 12 +- 9 files changed, 220 insertions(+), 197 deletions(-) create mode 100644 lib/vim.nix delete mode 100644 modules/assertions.nix diff --git a/lib/default.nix b/lib/default.nix index 2bdaab6..9a1d7f3 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -4,4 +4,5 @@ types = import ./types {inherit lib;}; languages = import ./languages.nix {inherit lib;}; lua = import ./lua.nix {inherit lib;}; + vim = import ./vim.nix {inherit lib;}; } diff --git a/lib/lua.nix b/lib/lua.nix index d888d3f..55e22d3 100644 --- a/lib/lua.nix +++ b/lib/lua.nix @@ -1,11 +1,8 @@ # Helpers for converting values to lua -{lib}: rec { - # yes? no. - yesNo = value: - if value - then "yes" - else "no"; - +{lib}: let + inherit (lib) mapAttrsToList filterAttrs concatStringsSep concatMapStringsSep stringToCharacters boolToString; + inherit (builtins) hasAttr head; +in rec { # Convert a null value to lua's nil nullString = value: if value == null @@ -46,4 +43,44 @@ + " }"; # Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first luaTable = items: ''{${builtins.concatStringsSep "," items}}''; + + toLuaObject = args: + if builtins.isAttrs args + then + if hasAttr "__raw" args + then args.__raw + else if hasAttr "__empty" args + then "{ }" + else + "{" + + (concatStringsSep "," + (mapAttrsToList + (n: v: + if head (stringToCharacters n) == "@" + then toLuaObject v + else "[${toLuaObject n}] = " + (toLuaObject v)) + (filterAttrs + ( + _: v: + (v != null) && (toLuaObject v != "{}") + ) + args))) + + "}" + else if builtins.isList args + then "{" + concatMapStringsSep "," toLuaObject args + "}" + else if builtins.isString args + then + # This should be enough! + builtins.toJSON args + else if builtins.isPath args + then builtins.toJSON (toString args) + else if builtins.isBool args + then "${boolToString args}" + else if builtins.isFloat args + then "${toString args}" + else if builtins.isInt args + then "${toString args}" + else if (args != null) + then "nil" + else ""; } diff --git a/lib/vim.nix b/lib/vim.nix new file mode 100644 index 0000000..b00d7ca --- /dev/null +++ b/lib/vim.nix @@ -0,0 +1,26 @@ +{lib}: let + inherit (builtins) isInt isBool toJSON; +in rec { + # yes? no. + yesNo = value: + if value + then "yes" + else "no"; + + # convert a boolean to a vim compliant boolean string + mkVimBool = val: + if val + then "1" + else "0"; + + # convert a literal value to a vim compliant value + valToVim = val: + if (isInt val) + then (builtins.toString val) + else + ( + if (isBool val) + then (mkVimBool val) + else (toJSON val) + ); +} diff --git a/modules/assertions.nix b/modules/assertions.nix deleted file mode 100644 index 3236efa..0000000 --- a/modules/assertions.nix +++ /dev/null @@ -1,27 +0,0 @@ -{ - config, - lib, - ... -}: -with lib; let - cfg = config.vim; -in { - config = { - assertions = mkMerge [ - { - assertion = cfg.kommentary.enable; - message = "Kommentary has been deprecated in favor of comments-nvim"; - } - { - assertion = cfg.utility.colorizer.enable; - message = "config.utility.colorizer has been renamed to config.utility.ccc"; - } - mkIf - (config.programs.neovim-flake.enable) - { - assertion = !config.programs.neovim.enable; - message = "You cannot use `programs.neovim-flake.enable` with `programs.neovim.enable`"; - } - ]; - }; -} diff --git a/modules/basic/config.nix b/modules/basic/config.nix index 82d8648..e84df7d 100644 --- a/modules/basic/config.nix +++ b/modules/basic/config.nix @@ -2,9 +2,9 @@ lib, config, ... -}: -with lib; -with builtins; let +}: let + inherit (lib) optionalString mkIf nvim; + cfg = config.vim; in { config = { @@ -57,8 +57,8 @@ in { }; vim.configRC.basic = nvim.dag.entryAfter ["globalsScript"] '' - " Debug mode settings ${optionalString cfg.debugMode.enable '' + " Debug mode settings set verbose=${toString cfg.debugMode.level} set verbosefile=${cfg.debugMode.logFile} ''} diff --git a/modules/basic/module.nix b/modules/basic/module.nix index 8bee1a6..cf972c0 100644 --- a/modules/basic/module.nix +++ b/modules/basic/module.nix @@ -2,9 +2,10 @@ pkgs, lib, ... -}: -with lib; -with builtins; { +}: let + inherit (lib) mkEnableOption mkOption; + inherit (lib.types) types; +in { options.vim = { package = mkOption { type = types.package; diff --git a/modules/core/default.nix b/modules/core/default.nix index 2a35262..3b296f0 100644 --- a/modules/core/default.nix +++ b/modules/core/default.nix @@ -3,8 +3,11 @@ lib, ... }: -with lib; with builtins; let + inherit (lib) mkOption types mapAttrsFlatten filterAttrs optionalString getAttrs; + inherit (lib) nvim; + inherit (nvim.lua) toLuaObject; + cfg = config.vim; wrapLuaConfig = luaConfig: '' @@ -20,7 +23,7 @@ with builtins; let mkOption { type = types.bool; default = value; - description = description; + inherit description; }; # Most of the keybindings code is highly inspired by pta2002/nixvim. Thank you! @@ -67,7 +70,7 @@ with builtins; let normalizeAction = action: let # Extract the values of the config options that have been explicitly set by the user config = - filterAttrs (n: v: v != null) + filterAttrs (_: v: v != null) (getAttrs (attrNames mapConfigOptions) action); in { config = @@ -85,8 +88,8 @@ with builtins; let normalizedAction = normalizeAction action; in { inherit (normalizedAction) action config; - key = key; - mode = mode; + inherit key; + inherit mode; }) maps); @@ -117,169 +120,140 @@ with builtins; let default = {}; }; in { - options.vim = { - viAlias = mkOption { - description = "Enable vi alias"; - type = types.bool; - default = true; - }; - - vimAlias = mkOption { - description = "Enable vim alias"; - type = types.bool; - default = true; - }; - - configRC = mkOption { - description = "vimrc contents"; - type = types.oneOf [(nvim.types.dagOf types.lines) types.str]; - default = {}; - }; - - luaConfigRC = mkOption { - description = "vim lua config"; - type = types.oneOf [(nvim.types.dagOf types.lines) types.str]; - default = {}; - }; - - builtConfigRC = mkOption { + options = { + assertions = lib.mkOption { + type = with types; listOf unspecified; internal = true; - type = types.lines; - description = "The built config for neovim after resolving the DAG"; - }; - - startPlugins = nvim.types.pluginsOpt { default = []; - description = "List of plugins to startup."; + example = lib.literalExpression [ + { + assertion = false; + message = "you can't enable this for that reason"; + } + ]; }; - optPlugins = nvim.types.pluginsOpt { + warnings = mkOption { + internal = true; default = []; - description = "List of plugins to optionally load"; - }; - - extraPlugins = mkOption { - type = types.attrsOf nvim.types.extraPluginType; - default = {}; - description = '' - List of plugins and related config. - Note that these are setup after builtin plugins. + type = with types; listOf str; + example = ["The `foo' service is deprecated and will go away soon!"]; + description = lib.mdDoc '' + This option allows modules to show warnings to users during + the evaluation of the system configuration. ''; - example = literalExpression '' - with pkgs.vimPlugins; { - aerial = { - package = aerial-nvim; - setup = "require('aerial').setup {}"; - }; - harpoon = { - package = harpoon; - setup = "require('harpoon').setup {}"; - after = ["aerial"]; - }; - }''; }; - globals = mkOption { - default = {}; - description = "Set containing global variable values"; - type = types.attrs; - }; - - maps = mkOption { - type = types.submodule { - options = { - normal = mapOptions "normal"; - insert = mapOptions "insert"; - select = mapOptions "select"; - visual = mapOptions "visual and select"; - terminal = mapOptions "terminal"; - normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')"; - - visualOnly = mapOptions "visual only"; - operator = mapOptions "operator-pending"; - insertCommand = mapOptions "insert and command-line"; - lang = mapOptions "insert, command-line and lang-arg"; - command = mapOptions "command-line"; - }; + vim = { + viAlias = mkOption { + description = "Enable vi alias"; + type = types.bool; + default = true; }; - default = {}; - description = '' - Custom keybindings for any mode. - For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp. - ''; + vimAlias = mkOption { + description = "Enable vim alias"; + type = types.bool; + default = true; + }; - example = '' - maps = { - normal."m" = { - silent = true; - action = "make"; - }; # Same as nnoremap m make + configRC = mkOption { + description = "vimrc contents"; + type = types.oneOf [(nvim.types.dagOf types.lines) types.str]; + default = {}; + }; + + luaConfigRC = mkOption { + description = "vim lua config"; + type = types.oneOf [(nvim.types.dagOf types.lines) types.str]; + default = {}; + }; + + builtConfigRC = mkOption { + internal = true; + type = types.lines; + description = "The built config for neovim after resolving the DAG"; + }; + + startPlugins = nvim.types.pluginsOpt { + default = []; + description = "List of plugins to startup."; + }; + + optPlugins = nvim.types.pluginsOpt { + default = []; + description = "List of plugins to optionally load"; + }; + + extraPlugins = mkOption { + type = types.attrsOf nvim.types.extraPluginType; + default = {}; + description = '' + List of plugins and related config. + Note that these are setup after builtin plugins. + ''; + example = literalExpression '' + with pkgs.vimPlugins; { + aerial = { + package = aerial-nvim; + setup = "require('aerial').setup {}"; + }; + harpoon = { + package = harpoon; + setup = "require('harpoon').setup {}"; + after = ["aerial"]; + }; + }''; + }; + + globals = mkOption { + default = {}; + description = "Set containing global variable values"; + type = types.attrs; + }; + + maps = mkOption { + type = types.submodule { + options = { + normal = mapOptions "normal"; + insert = mapOptions "insert"; + select = mapOptions "select"; + visual = mapOptions "visual and select"; + terminal = mapOptions "terminal"; + normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')"; + + visualOnly = mapOptions "visual only"; + operator = mapOptions "operator-pending"; + insertCommand = mapOptions "insert and command-line"; + lang = mapOptions "insert, command-line and lang-arg"; + command = mapOptions "command-line"; + }; }; - ''; + default = {}; + description = '' + Custom keybindings for any mode. + + For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp. + ''; + + example = '' + maps = { + normal."m" = { + silent = true; + action = "make"; + }; # Same as nnoremap m make + }; + ''; + }; }; }; config = let - mkVimBool = val: - if val - then "1" - else "0"; - valToVim = val: - if (isInt val) - then (builtins.toString val) - else - ( - if (isBool val) - then (mkVimBool val) - else (toJSON val) - ); - filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings; globalsScript = mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}") (filterNonNull cfg.globals); - toLuaObject = args: - if builtins.isAttrs args - then - if hasAttr "__raw" args - then args.__raw - else if hasAttr "__empty" args - then "{ }" - else - "{" - + (concatStringsSep "," - (mapAttrsToList - (n: v: - if head (stringToCharacters n) == "@" - then toLuaObject v - else "[${toLuaObject n}] = " + (toLuaObject v)) - (filterAttrs - ( - n: v: - !isNull v && (toLuaObject v != "{}") - ) - args))) - + "}" - else if builtins.isList args - then "{" + concatMapStringsSep "," toLuaObject args + "}" - else if builtins.isString args - then - # This should be enough! - builtins.toJSON args - else if builtins.isPath args - then builtins.toJSON (toString args) - else if builtins.isBool args - then "${boolToString args}" - else if builtins.isFloat args - then "${toString args}" - else if builtins.isInt args - then "${toString args}" - else if isNull args - then "nil" - else ""; - toLuaBindings = mode: maps: builtins.map (value: '' vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) @@ -304,7 +278,7 @@ in { mapResult, }: let # When the value is a string, default it to dag.entryAnywhere - finalDag = lib.mapAttrs (name: value: + finalDag = lib.mapAttrs (_: value: if builtins.isString value then nvim.dag.entryAnywhere value else value) @@ -378,6 +352,13 @@ in { }; builtConfigRC = let + failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions); + + baseSystemAssertWarn = + if failedAssertions != [] + then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}" + else lib.showWarnings config.warnings; + mkSection = r: '' " SECTION: ${r.name} ${r.data} @@ -389,7 +370,7 @@ in { inherit mapResult; }; in - vimConfig; + baseSystemAssertWarn vimConfig; }; }; } diff --git a/modules/default.nix b/modules/default.nix index c58f17d..53224c8 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -5,7 +5,7 @@ inputs: { check ? true, extraSpecialArgs ? {}, }: let - inherit (pkgs) neovim-unwrapped wrapNeovim vimPlugins; + inherit (pkgs) wrapNeovim vimPlugins; inherit (builtins) map filter isString toString getAttr; inherit (pkgs.vimUtils) buildVimPlugin; @@ -21,6 +21,8 @@ inputs: { specialArgs = {modulesPath = toString ./.;} // extraSpecialArgs; }; + vimOptions = module.config.vim; + buildPlug = {pname, ...} @ args: assert lib.asserts.assertMsg (pname != "nvim-treesitter") "Use buildTreesitterPlug for building nvim-treesitter."; buildVimPlugin (args @@ -31,8 +33,6 @@ inputs: { buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars); - vimOptions = module.config.vim; - buildConfigPlugins = plugins: map (plug: ( diff --git a/modules/modules.nix b/modules/modules.nix index b3cb152..1bfc069 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -34,10 +34,14 @@ pkgsModule = {config, ...}: { config = { - _module.args.baseModules = modules; - _module.args.pkgsPath = lib.mkDefault pkgs.path; - _module.args.pkgs = lib.mkDefault pkgs; - _module.check = check; + _module = { + inherit check; + args = { + baseModules = modules; + pkgsPath = lib.mkDefault pkgs.path; + pkgs = lib.mkDefault pkgs; + }; + }; }; }; in