nvf/modules/default.nix

152 lines
4.7 KiB
Nix
Raw Normal View History

2023-02-10 17:39:31 +00:00
inputs: {
configuration,
pkgs,
lib,
check ? true,
extraSpecialArgs ? {},
extraModules ? [],
}: let
inherit (pkgs) vimPlugins;
inherit (lib.strings) isString toString;
inherit (lib.lists) filter map concatLists;
2024-08-09 07:29:29 +00:00
inherit (lib.attrsets) recursiveUpdate;
2024-08-05 11:16:30 +00:00
inherit (builtins) baseNameOf;
# import modules.nix with `check`, `pkgs` and `lib` as arguments
# check can be disabled while calling this file is called
# to avoid checking in all modules
nvimModules = import ./modules.nix {
inherit pkgs check lib;
};
# evaluate the extended library with the modules
# optionally with any additional modules passed by the user
module = lib.evalModules {
specialArgs = recursiveUpdate {modulesPath = toString ./.;} extraSpecialArgs;
modules = concatLists [[configuration] nvimModules extraModules];
};
# alias to the internal configuration
2023-11-06 09:33:38 +00:00
vimOptions = module.config.vim;
luajit = vimOptions.package.lua;
# build a vim plugin with the given name and arguments
# if the plugin is nvim-treesitter, warn the user to use buildTreesitterPlug
# instead
2024-08-09 07:29:29 +00:00
buildPlug = attrs: let
src = inputs."plugin-${attrs.pname}";
in
pkgs.stdenvNoCC.mkDerivation ({
version = src.shortRev or src.shortDirtyRev or "dirty";
2024-08-05 12:57:02 +00:00
inherit src;
2024-08-09 07:29:41 +00:00
nativeBuildInputs = with pkgs.vimUtils; [
luajit
2024-08-09 07:29:41 +00:00
vimCommandCheckHook
vimGenDocHook
neovimRequireCheckHook
];
passthru.vimPlugin = true;
2024-08-05 12:57:02 +00:00
buildPhase = lib.optionalString vimOptions.byteCompileLua ''
runHook preBuild
find . -type f -name '*.lua' -exec luajit -bd -- {} {} \;
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r . $out
runHook postInstall
'';
}
// attrs);
buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars);
2024-08-09 07:29:29 +00:00
pluginBuilders = {
nvim-treesitter = buildTreesitterPlug vimOptions.treesitter.grammars;
flutter-tools-patched =
buildPlug
{
pname = "flutter-tools";
patches = [../patches/flutter-tools.patch];
};
};
buildConfigPlugins = plugins:
map
2024-08-09 07:29:29 +00:00
(
plug:
if (isString plug)
then pluginBuilders.${plug} or (buildPlug {pname = plug;})
else plug
)
(filter (f: f != null) plugins);
# built (or "normalized") plugins that are modified
builtStartPlugins = buildConfigPlugins vimOptions.startPlugins;
builtOptPlugins = map (package: {
plugin = package;
optional = true;
}) (buildConfigPlugins vimOptions.optPlugins);
# additional Lua and Python3 packages, mapped to their respective functions
# to conform to the format makeNeovimConfig expects. end user should
# only ever need to pass a list of packages, which are modified
# here
extraLuaPackages = ps: map (x: ps.${x}) vimOptions.luaPackages;
extraPython3Packages = ps: map (x: ps.${x}) vimOptions.python3Packages;
2024-08-05 11:16:30 +00:00
luaConfig =
if vimOptions.byteCompileLua
then pkgs.runCommandLocal "init.lua" {text = vimOptions.builtLuaConfigRC;} "${luajit}/bin/luajit -bd -- - $out <<< \"$text\""
2024-08-05 11:16:30 +00:00
else pkgs.writeText "init.lua" vimOptions.builtLuaConfigRC;
extraLuaFiles =
if vimOptions.byteCompileLua
then map (file: pkgs.runCommandLocal (baseNameOf file) {} "${luajit}/bin/luajit -bd -- ${file} $out") vimOptions.extraLuaFiles
2024-08-05 11:16:30 +00:00
else vimOptions.extraLuaFiles;
# Wrap the user's desired (unwrapped) Neovim package with arguments that'll be used to
# generate a wrapped Neovim package.
neovim-wrapped = inputs.mnw.lib.wrap pkgs {
neovim = vimOptions.package;
plugins = concatLists [builtStartPlugins builtOptPlugins];
appName = "nvf";
extraBinPath = vimOptions.extraPackages;
2024-08-05 11:16:30 +00:00
luaFiles = [luaConfig] ++ extraLuaFiles;
inherit (vimOptions) viAlias vimAlias withRuby withNodeJs withPython3;
inherit extraLuaPackages extraPython3Packages;
};
2024-08-10 14:52:14 +00:00
dummyInit = pkgs.writeText "nvf-init.lua" vimOptions.builtLuaConfigRC;
# Additional helper scripts for printing and displaying nvf configuration
# in your commandline.
2024-08-10 14:52:14 +00:00
printConfig = pkgs.writers.writeDashBin "nvf-print-config" "cat ${dummyInit}";
printConfigPath = pkgs.writers.writeDashBin "nvf-print-config-path" "echo -n ${dummyInit}";
in {
inherit (module) options config;
inherit (module._module.args) pkgs;
# Expose wrapped neovim-package for userspace
# or module consumption.
neovim = pkgs.symlinkJoin {
name = "nvf-with-helpers";
paths = [neovim-wrapped printConfig printConfigPath];
2024-08-10 14:52:14 +00:00
postBuild = "echo Helpers added";
meta = {
description = "Wrapped version of Neovim with additional helper scripts";
mainProgram = "nvim";
};
};
}