vimUtils: derive pname from the plugin string (#53121)

For backwards compatibility. The user may specify their own plugins
without the pname attribute. In that case the attrname of the plugin
(given as a string) is the pname.

Fixes #53112
This commit is contained in:
Timo Kaufmann 2018-12-31 16:26:06 +01:00 committed by GitHub
parent ced0be5c06
commit a0dc57e27b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
{stdenv, vim, vimPlugins, vim_configurable, neovim, buildEnv, writeText, writeScriptBin { stdenv, vim, vimPlugins, vim_configurable, neovim, buildEnv, writeText, writeScriptBin
, nix-prefetch-hg, nix-prefetch-git }: , nix-prefetch-hg, nix-prefetch-git
, fetchFromGitHub
}:
/* /*
@ -150,20 +152,23 @@ vim_with_plugins can be installed like any other application within Nix.
let let
inherit (stdenv) lib; inherit (stdenv) lib;
# transitive closure of plugin dependencies # make sure a plugin is a derivation. If plugin already is a derivation, this
transitiveClosure = knownPlugins: plugin: # is a no-op. If it is a string, it is looked up in knownPlugins.
let pluginToDrv = knownPlugins: plugin:
# vam puts out a list of strings as the dependency list, we need to be able to deal with that. if builtins.isString plugin then
# Because of that, "plugin" may be a string or a derivation. If it is a string, it is resolved # make sure `pname` is set to that we are able to convert the derivation
# using `knownPlugins`. Otherwise `knownPlugins` can be null. # back to a string.
knownPlugins' = if knownPlugins == null then vimPlugins else knownPlugins; ( knownPlugins.${plugin} // { pname = plugin; })
pluginDrv = if builtins.isString plugin then knownPlugins'.${plugin} else plugin; else
in plugin;
[ pluginDrv ] ++ (
lib.unique (builtins.concatLists (map (transitiveClosure knownPlugins) pluginDrv.dependencies or [])) # transitive closure of plugin dependencies (plugin needs to be a derivation)
transitiveClosure = plugin:
[ plugin ] ++ (
lib.unique (builtins.concatLists (map transitiveClosure plugin.dependencies or []))
); );
findDependenciesRecursively = knownPlugins: plugins: lib.concatMap (transitiveClosure knownPlugins) plugins; findDependenciesRecursively = plugins: lib.concatMap transitiveClosure plugins;
attrnamesToPlugins = { knownPlugins, names }: attrnamesToPlugins = { knownPlugins, names }:
map (name: if builtins.isString name then knownPlugins.${name} else name) knownPlugins; map (name: if builtins.isString name then knownPlugins.${name} else name) knownPlugins;
@ -195,7 +200,7 @@ let
(let (let
knownPlugins = pathogen.knownPlugins or vimPlugins; knownPlugins = pathogen.knownPlugins or vimPlugins;
plugins = findDependenciesRecursively knownPlugins pathogen.pluginNames; plugins = findDependenciesRecursively (map (pluginToDrv knownPlugins) pathogen.pluginNames);
pluginsEnv = buildEnv { pluginsEnv = buildEnv {
name = "pathogen-plugin-env"; name = "pathogen-plugin-env";
@ -240,7 +245,10 @@ let
(let (let
knownPlugins = vam.knownPlugins or vimPlugins; knownPlugins = vam.knownPlugins or vimPlugins;
plugins = findDependenciesRecursively knownPlugins (lib.concatMap vamDictToNames vam.pluginDictionaries); # plugins specified by the user
specifiedPlugins = map (pluginToDrv knownPlugins) (lib.concatMap vamDictToNames vam.pluginDictionaries);
# plugins with dependencies
plugins = findDependenciesRecursively specifiedPlugins;
# Vim almost reads JSON, so eventually JSON support should be added to Nix # Vim almost reads JSON, so eventually JSON support should be added to Nix
# TODO: proper quoting # TODO: proper quoting
@ -298,8 +306,8 @@ let
# opposed to older implementations that have to maintain backwards # opposed to older implementations that have to maintain backwards
# compatibility). Therefore we don't need to deal with "knownPlugins" # compatibility). Therefore we don't need to deal with "knownPlugins"
# and can simply pass `null`. # and can simply pass `null`.
depsOfOptionalPlugins = lib.subtractLists opt (findDependenciesRecursively null opt); depsOfOptionalPlugins = lib.subtractLists opt (findDependenciesRecursively opt);
startWithDeps = findDependenciesRecursively null start; startWithDeps = findDependenciesRecursively start;
in in
["mkdir -p $out/pack/${packageName}/start"] ["mkdir -p $out/pack/${packageName}/start"]
# To avoid confusion, even dependencies of optional plugins are added # To avoid confusion, even dependencies of optional plugins are added
@ -421,8 +429,8 @@ rec {
if vam != null && vam ? knownPlugins then vam.knownPlugins else if vam != null && vam ? knownPlugins then vam.knownPlugins else
if pathogen != null && pathogen ? knownPlugins then pathogen.knownPlugins else if pathogen != null && pathogen ? knownPlugins then pathogen.knownPlugins else
vimPlugins; vimPlugins;
pathogenPlugins = findDependenciesRecursively knownPlugins pathogen.pluginNames; pathogenPlugins = findDependenciesRecursively ((map pluginToDrv knownPlugins) pathogen.pluginNames);
vamPlugins = findDependenciesRecursively knownPlugins (lib.concatMap vamDictToNames vam.pluginDictionaries); vamPlugins = findDependenciesRecursively (map (pluginToDrv knownPlugins) (lib.concatMap vamDictToNames vam.pluginDictionaries));
nonNativePlugins = (lib.optionals (pathogen != null) pathogenPlugins) nonNativePlugins = (lib.optionals (pathogen != null) pathogenPlugins)
++ (lib.optionals (vam != null) vamPlugins) ++ (lib.optionals (vam != null) vamPlugins)
++ (lib.optionals (plug != null) plug.plugins); ++ (lib.optionals (plug != null) plug.plugins);
@ -457,4 +465,26 @@ rec {
test_nvim_with_vim_nix_using_pathogen = neovim.override { test_nvim_with_vim_nix_using_pathogen = neovim.override {
configure.pathogen.pluginNames = [ "vim-nix" ]; configure.pathogen.pluginNames = [ "vim-nix" ];
}; };
# regression test for https://github.com/NixOS/nixpkgs/issues/53112
# The user may have specified their own plugins which may not be formatted
# exactly as the generated ones. In particular, they may not have the `pname`
# attribute.
test_vim_with_custom_plugin = vim_configurable.customize {
name = "vim_with_custom_plugin";
vimrcConfig.vam.knownPlugins =
vimPlugins // ({
"vim-trailing-whitespace" = buildVimPluginFrom2Nix {
name = "vim-trailing-whitespace";
src = fetchFromGitHub {
owner = "bronson";
repo = "vim-trailing-whitespace";
rev = "4c596548216b7c19971f8fc94e38ef1a2b55fee6";
sha256 = "0f1cpnp1nxb4i5hgymjn2yn3k1jwkqmlgw1g02sq270lavp2dzs9";
};
dependencies = [];
};
});
vimrcConfig.vam.pluginDictionaries = [ { names = [ "vim-trailing-whitespace" ]; } ];
};
} }