From 53b281848b98c7c3de0d174be80794582549a79f Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 16 Apr 2024 18:54:21 +0300 Subject: [PATCH] refac: use nixpkgs lib instead --- flake.nix | 2 +- lib.nix | 60 +-------------------------------------- neovim.nix | 16 ++++++++--- plugins/lsp/default.nix | 17 ++++++----- plugins/theme/default.nix | 6 ++-- plugins/ux/default.nix | 4 +-- 6 files changed, 29 insertions(+), 76 deletions(-) diff --git a/flake.nix b/flake.nix index 4ef9530..b4338b8 100644 --- a/flake.nix +++ b/flake.nix @@ -208,7 +208,7 @@ buildPlugin = name: vimUtils.buildVimPlugin { name = name; src = getAttr name inputs; - patches = lib.optional + patches = lib.optionals (pathExists ./patches/${name}) (map (patchName: ./patches/${name}/${patchName}) diff --git a/lib.nix b/lib.nix index fd5b3c1..84c46d9 100644 --- a/lib.nix +++ b/lib.nix @@ -1,65 +1,7 @@ { lib, substituteAll, ... }: let - inherit (builtins) length elemAt isList isString hasAttr getAttr; - - ########################################################################### - # Helpers - - # Source: https://github.com/NixOS/nixpkgs/blob/d61bc96d16ca288c69b798b8e31eca64050098e3/lib/lists.nix - foldr = op: nul: list: - let - len = length list; - fold' = n: - if n == len - then nul - else op (elemAt list n) (fold' (n + 1)); - in - fold' 0; - - concatMap = op: list: - let - concat = (a: b: op a + b); - in - foldr concat "" list; - - ############################################################################ - # Configs - - optional = cond: val: - let - def = - if isList val then [ ] - else if isString val then "" - else null; - in - if cond then val else def; - - getAttrOpt = a: s: - if hasAttr a s then getAttr a s else null; - - ############################################################################# - # Lua - - mkLuaHeredoc = content: '' - lua << EOF - ${content} - EOF - ''; - - mkLuaRc = contents: concatMap mkLuaHeredoc contents; - - ############################################################################ - # Configs - readSubFile = src: params: builtins.readFile (substituteAll (params // { inherit src; })); - in -{ - inherit (lib) importJSON attrByPath; - inherit foldr concatMap; - inherit optional getAttrOpt; - inherit mkLuaHeredoc mkLuaRc; - inherit readSubFile substituteAll; -} +lib // { inherit readSubFile substituteAll; } diff --git a/neovim.nix b/neovim.nix index 5e56e69..f6a516c 100644 --- a/neovim.nix +++ b/neovim.nix @@ -35,7 +35,7 @@ let plugins = mergeAttrs plugins'; inherit (builtins) catAttrs readFile; - myLib = import ./lib.nix { inherit lib substituteAll; } // { + extLib = import ./lib.nix { inherit lib substituteAll; } // { inherit (nix2lua.lib) toLua LuaNil; }; @@ -43,7 +43,7 @@ let pluginParams = neovimPlugins // { inherit tree-sitter plugins enableDevIcons enableTabby enableOrgMode; themeCfg = theme; - lib = myLib; + lib = extLib; }; callPlugin = plugin: callPackage plugin pluginParams; @@ -67,6 +67,14 @@ let basicConfigs = map readFile [ ./config/basic.lua ]; pluginConfigs = catAttrs "luaConfig" pluginsSettings; allConfigs = basicConfigs ++ pluginConfigs ++ [ extraLuaConfig ]; + + mkLuaHeredoc = content: '' + lua << EOF + ${content} + EOF + ''; + + mkLuaRc = lib.concatMapStrings mkLuaHeredoc; in (wrapNeovim neovim-unwrapped { inherit viAlias; @@ -77,13 +85,13 @@ in withRuby = false; configure = { - customRC = extraConfig + myLib.mkLuaRc allConfigs; + customRC = extraConfig + mkLuaRc allConfigs; packages.myVimPackages = { start = allPlugins; }; }; }).overrideAttrs (oldAttrs: { passthru = oldAttrs.passthru // { nix2lua = nix2lua.lib; - inherit plugins; + inherit plugins allConfigs; }; }) diff --git a/plugins/lsp/default.nix b/plugins/lsp/default.nix index eae556d..dcb6562 100644 --- a/plugins/lsp/default.nix +++ b/plugins/lsp/default.nix @@ -18,14 +18,17 @@ let max_lines = 100; }; tabbymlSettings = lib.toLua (lib.attrByPath [ "tabbyml" "settings" ] tabbymlDefaultSettings plugins); - tabbymlLuaConfig = lib.optional tabbymlEnable + tabbymlLuaConfig = lib.optionalString tabbymlEnable (lib.readSubFile ./cmp-tabby.lua { inherit tabbymlSettings; }); - cmpSources = lib.toLua ([ - { name = "nvim_lsp"; } - { name = "luasnip"; } - { name = "orgmode"; } - ] ++ lib.optional tabbymlEnable [{ name = "cmp_tabby"; }]); + cmpSources = lib.toLua ( + [ + { name = "nvim_lsp"; } + { name = "luasnip"; } + { name = "orgmode"; } + ] + ++ lib.optional tabbymlEnable { name = "cmp_tabby"; } + ); cmpLuaConfig = lib.readSubFile ./nvim-cmp.lua { inherit cmpSources; }; lspconfigLuaConfig = lib.readSubFile ./lspconfig.lua @@ -37,7 +40,7 @@ let nvim-cmp # Autocompletion cmp-nvim-lsp # LSP source for nvim-cmp cmp-luasnip # Snippets source for nvim-cmp - ] ++ lib.optional tabbymlEnable [ cmp-tabby ]; + ] ++ lib.optional tabbymlEnable cmp-tabby; lspConfigServers = lib.toLua (lib.attrByPath [ "lspConfig" "servers" ] [ ] plugins); lspSagaSettings = lib.toLua (lib.attrByPath [ "lspSaga" "settings" ] { } plugins); diff --git a/plugins/theme/default.nix b/plugins/theme/default.nix index 54283b7..0c4f85b 100644 --- a/plugins/theme/default.nix +++ b/plugins/theme/default.nix @@ -27,10 +27,10 @@ in { luaConfig = themeConfig + (readFile (lib.substituteAll { src = ./lualine.lua; inherit lualineSettings; })) - + (lib.optional enableTabby (readFile ./tabby-nvim.lua)); + + (lib.optionalString enableTabby (readFile ./tabby-nvim.lua)); plugins = [ theme ] ++ lualinePlugins - ++ (lib.optional enableDevIcons [ nvim-web-devicons ]) - ++ (lib.optional enableTabby [ tabby-nvim ]); + ++ (lib.optional enableDevIcons nvim-web-devicons) + ++ (lib.optional enableTabby tabby-nvim); } diff --git a/plugins/ux/default.nix b/plugins/ux/default.nix index b7d398a..7e590c7 100644 --- a/plugins/ux/default.nix +++ b/plugins/ux/default.nix @@ -15,7 +15,7 @@ let orgmodeEnable = lib.attrByPath [ "orgmode" "enable" ] enableOrgMode plugins; orgmodeSettings = lib.toLua (lib.attrByPath [ "orgmode" "settings" ] { } plugins); - orgmodeLuaConfig = lib.optional orgmodeEnable + orgmodeLuaConfig = lib.optionalString orgmodeEnable (lib.readSubFile ./nvim-orgmode.lua { inherit orgmodeSettings; }); colorizerFiletypes = lib.toLua (lib.attrByPath [ "colorizer" "filetypes" ] lib.LuaNil plugins); @@ -28,5 +28,5 @@ in luaConfig = hopLuaConfig + orgmodeLuaConfig + colorizerLuaConfig; plugins = [ hop-nvim nvim-colorizer ] - ++ lib.optional orgmodeEnable [ nvim-orgmode org-bullets-nvim ]; + ++ lib.optionals orgmodeEnable [ nvim-orgmode org-bullets-nvim ]; }