Skip to content

Commit

Permalink
vimPlugins.plenary: init from lua package
Browse files Browse the repository at this point in the history
This commit shows how to convert luarocks packages into (neo)vim ones.
The advantage for neovim lua plugins to register their rockspec (aka
package definition) is that the plugin can express its dependencies and
a few metadata through it.
  • Loading branch information
teto committed Aug 9, 2021
1 parent be0b03e commit 0668479
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion maintainers/scripts/luarocks-packages.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nix name, luarocks name, server, version,luaversion,maintainers
# nix name,luarocks name,server,version,luaversion,maintainers
alt-getopt,,,,,arobyn
ansicolors,,,,,
argparse,,,,,
Expand All @@ -16,6 +16,7 @@ cyrussasl,,,,,
digestif,,,,lua5_3,
dkjson,,,,,
fifo,,,,,
gitsigns.nvim,,,,lua5_1,
http,,,,,vcunat
inspect,,,,,
ldbus,,http://luarocks.org/dev,,,
Expand Down
14 changes: 11 additions & 3 deletions pkgs/misc/vim-plugins/default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# TODO check that no license information gets lost
{ callPackage, config, lib, vimUtils, vim, darwin, llvmPackages }:
{ callPackage, config, lib, vimUtils, vim, darwin, llvmPackages, luaPackages }:

let

inherit (vimUtils.override {inherit vim;}) buildVimPluginFrom2Nix;

inherit (lib) extends;

initialPackages = self: {};
initialPackages = self: {
# Convert derivation to a vim plugin.
toVimPlugin = drv:
drv.overrideAttrs(oldAttrs: {
passthru = (oldAttrs.passthru or {}) // {
vimPlugin = true;
};
});
};

plugins = callPackage ./generated.nix {
inherit buildVimPluginFrom2Nix;
Expand All @@ -22,7 +30,7 @@ let
overrides = callPackage ./overrides.nix {
inherit (darwin.apple_sdk.frameworks) Cocoa CoreFoundation CoreServices;
inherit buildVimPluginFrom2Nix;
inherit llvmPackages;
inherit llvmPackages luaPackages;
};

aliases = if (config.allowAliases or true) then (import ./aliases.nix lib) else final: prev: {};
Expand Down
4 changes: 4 additions & 0 deletions pkgs/misc/vim-plugins/overrides.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
, iferr
, impl
, reftools
# must be lua51Packages
, luaPackages
}:

self: super: {
Expand Down Expand Up @@ -282,6 +284,8 @@ self: super: {
dependencies = with self; [ plenary-nvim ];
});

plenary-nvim = super.toVimPlugin(luaPackages.plenary-nvim);

gruvbox-nvim = super.gruvbox-nvim.overrideAttrs (old: {
dependencies = with self; [ lush-nvim ];
});
Expand Down
33 changes: 30 additions & 3 deletions pkgs/misc/vim-plugins/vim-utils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{ lib, stdenv, vim, vimPlugins, vim_configurable, buildEnv, writeText, writeScriptBin
, nix-prefetch-hg, nix-prefetch-git
, fetchFromGitHub, runtimeShell
, hasLuaModule
}:

/*
Expand Down Expand Up @@ -186,7 +187,21 @@ let

nativeImpl = packages:
(let
link = (packageName: dir: pluginPath: "ln -sf ${pluginPath}/share/vim-plugins/* $out/pack/${packageName}/${dir}");
# dir is "start" or "opt"
linkLuaPlugin = plugin: packageName: dir: ''
mkdir -p $out/pack/${packageName}/${dir}/${plugin.pname}/lua
ln -sf ${plugin}/share/lua/5.1/* $out/pack/${packageName}/${dir}/${plugin.pname}/lua
ln -sf ${plugin}/${plugin.pname}-${plugin.version}-rocks/${plugin.pname}/${plugin.version}/* $out/pack/${packageName}/${dir}/${plugin.pname}/
'';

linkVimlPlugin = pluginPath: packageName: dir:
"ln -sf ${pluginPath}/${rtpPath}/* $out/pack/${packageName}/${dir}";

# (builtins.trace pluginPath )
link = pluginPath: if hasLuaModule pluginPath
then linkLuaPlugin pluginPath
else linkVimlPlugin pluginPath;

packageLinks = (packageName: {start ? [], opt ? []}:
let
# `nativeImpl` expects packages to be derivations, not strings (as
Expand All @@ -199,9 +214,9 @@ let
[ "mkdir -p $out/pack/${packageName}/start" ]
# To avoid confusion, even dependencies of optional plugins are added
# to `start` (except if they are explicitly listed as optional plugins).
++ (builtins.map (link packageName "start") (lib.unique (startWithDeps ++ depsOfOptionalPlugins)))
++ (builtins.map (x: link x packageName "start") (lib.unique (startWithDeps ++ depsOfOptionalPlugins)))
++ ["mkdir -p $out/pack/${packageName}/opt"]
++ (builtins.map (link packageName "opt") opt)
++ (builtins.map (x: link x packageName "opt") opt)
);
packDir = (packages:
stdenv.mkDerivation {
Expand All @@ -217,6 +232,18 @@ let
set runtimepath^=${packDir packages}
'');

/* Generates a vimrc string
packages is an attrset with {name: { start = [ vim derivations ]; opt = [ vim derivations ]; }
Example:
vimrcContent {
packages = { home-manager = { start = [vimPlugins.vim-fugitive]; opt = [];};
beforePlugins = '';
customRc = ''let mapleader = " "'';
};
*/
vimrcContent = {
packages ? null,
vam ? null,
Expand Down
6 changes: 5 additions & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32195,10 +32195,14 @@ with pkgs;

viewnior = callPackage ../applications/graphics/viewnior { };

vimUtils = callPackage ../misc/vim-plugins/vim-utils.nix { };

vimUtils = callPackage ../misc/vim-plugins/vim-utils.nix {
inherit (lua51Packages) hasLuaModule;
};

vimPlugins = recurseIntoAttrs (callPackage ../misc/vim-plugins {
llvmPackages = llvmPackages_6;
luaPackages = lua51Packages;
});

vimb-unwrapped = callPackage ../applications/networking/browsers/vimb { };
Expand Down
2 changes: 1 addition & 1 deletion pkgs/top-level/lua-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ with self; {
};


inherit toLuaModule lua-setup-hook;
inherit toLuaModule hasLuaModule lua-setup-hook;
inherit buildLuarocksPackage buildLuaApplication;
inherit requiredLuaModules luaOlder luaAtLeast
isLua51 isLua52 isLua53 isLuaJIT lua callPackage;
Expand Down

0 comments on commit 0668479

Please sign in to comment.