Commit c17904b9 authored by Matthieu Coudron's avatar Matthieu Coudron Committed by Matthieu Coudron
Browse files

neovim: more flexibility in startup commands

the neovim mechanisms generate provider rc configuration such as:
vim.g.<LANG>_host_prog=$out/bin/nvim-<LANG>

In home-manager, we want more control over it, e.g., to pass it via
command line or in init.lua.

We also want to retrieve the generated packpathDirs so that we can link
it in the expected $XDG_DATA_HOME folder so that we then dont have to
tell neovim where look for it anymore.
parent d4250d5d
Loading
Loading
Loading
Loading
+35 −32
Original line number Diff line number Diff line
@@ -77,18 +77,6 @@ let

      luaEnv = neovim-unwrapped.lua.withPackages(extraLuaPackages);

      # Mapping a boolean argument to a key that tells us whether to add or not to
      # add to nvim's 'embedded rc' this:
      #    let g:<key>_host_prog=$out/bin/nvim-<key>
      # Or this:
      #    let g:loaded_${prog}_provider=0
      # While the latter tells nvim that this provider is not available
      hostprog_check_table = {
        node = withNodeJs;
        python = false;
        python3 = withPython3;
        ruby = withRuby;
      };
      # as expected by packdir
      packpathDirs.myNeovimPackages = myVimPackage;
      ## Here we calculate all of the arguments to the 1st call of `makeWrapper`
@@ -98,22 +86,9 @@ let
      makeWrapperArgs =
        let
          binPath = lib.makeBinPath (lib.optionals withRuby [ rubyEnv ] ++ lib.optionals withNodeJs [ nodejs ]);

          hostProviderViml = lib.mapAttrsToList genProviderSettings hostprog_check_table;

          # as expected by packdir
          packDirArgs.myNeovimPackages = myVimPackage;

          # vim accepts a limited number of commands so we join them all
          flags = [
            "--cmd" (lib.intersperse "|" hostProviderViml)
          ] ++ lib.optionals (myVimPackage.start != [] || myVimPackage.opt != []) [
            "--cmd" "set packpath^=${vimUtils.packDir packDirArgs}"
            "--cmd" "set rtp^=${vimUtils.packDir packDirArgs}"
          ];
        in
        [
          "--inherit-argv0" "--add-flags" (lib.escapeShellArgs flags)
          "--inherit-argv0"
        ] ++ lib.optionals withRuby [
          "--set" "GEM_HOME" "${rubyEnv}/${rubyEnv.ruby.gemPath}"
        ] ++ lib.optionals (binPath != "") [
@@ -144,12 +119,6 @@ let
      inherit rubyEnv;
    };

    genProviderSettings = prog: withProg:
      if withProg then
        "let g:${prog}_host_prog='${placeholder "out"}/bin/nvim-${prog}'"
      else
        "let g:loaded_${prog}_provider=0"
    ;

  # to keep backwards compatibility for people using neovim.override
  legacyWrapper = neovim: {
@@ -191,9 +160,43 @@ let
      wrapperArgs = lib.escapeShellArgs res.wrapperArgs + " " + extraMakeWrapperArgs;
      wrapRc = (configure != {});
  });

  /* Generate vim.g.<LANG>_host_prog lua rc to setup host providers

  Mapping a boolean argument to a key that tells us whether to add
      vim.g.<LANG>_host_prog=$out/bin/nvim-<LANG>
  Or this:
      let g:loaded_${prog}_provider=0
  While the latter tells nvim that this provider is not available */
  generateProviderRc = {
      withPython3 ? true
    , withNodeJs ? false
    , withRuby ? true

    # so that we can pass the full neovim config while ignoring it
    , ...
    }: let
      hostprog_check_table = {
        node = withNodeJs;
        python = false;
        python3 = withPython3;
        ruby = withRuby;
      };

      genProviderCommand = prog: withProg:
        if withProg then
          "vim.g.${prog}_host_prog='${placeholder "out"}/bin/nvim-${prog}'"
        else
          "vim.g.loaded_${prog}_provider=0";

      hostProviderLua = lib.mapAttrsToList genProviderCommand hostprog_check_table;
    in
        lib.concatStringsSep ";" hostProviderLua;

in
{
  inherit makeNeovimConfig;
  inherit generateProviderRc;
  inherit legacyWrapper;

  buildNeovimPluginFrom2Nix = callPackage ./build-neovim-plugin.nix {
+24 −1
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@
, python3
, python3Packages
, callPackage
, neovimUtils
, vimUtils
}:
neovim:

@@ -12,6 +14,7 @@ let
      extraName ? ""
    # should contain all args but the binary. Can be either a string or list
    , wrapperArgs ? []
    # a limited RC script used only to generate the manifest for remote plugins
    , manifestRc ? null
    , withPython2 ? false
    , withPython3 ? true,  python3Env ? python3
@@ -26,12 +29,30 @@ let
    # (e.g., in ~/.config/init.vim or project/.nvimrc)
    , wrapRc ? true
    , neovimRcContent ? ""
    # entry to load in packpath
    , packpathDirs
    , ...
  }@args:
  let

    wrapperArgsStr = if lib.isString wrapperArgs then wrapperArgs else lib.escapeShellArgs wrapperArgs;

    # "--add-flags" (lib.escapeShellArgs flags)
    # wrapper args used both when generating the manifest and in the final neovim executable
    commonWrapperArgs = (lib.optionals (lib.isList wrapperArgs) wrapperArgs)
      # vim accepts a limited number of commands so we join them all
          ++ [
            "--add-flags" ''--cmd "lua ${providerLuaRc}"''
            # (lib.intersperse "|" hostProviderViml)
          ] ++ lib.optionals (packpathDirs.myNeovimPackages.start != [] || packpathDirs.myNeovimPackages.opt != []) [
            "--add-flags" ''--cmd "set packpath^=${vimUtils.packDir packpathDirs}"''
            "--add-flags" ''--cmd "set rtp^=${vimUtils.packDir packpathDirs}"''
          ]
          ;

    providerLuaRc = neovimUtils.generateProviderRc args;
    # providerLuaRc = "toto";

    # If configure != {}, we can't generate the rplugin.vim file with e.g
    # NVIM_SYSTEM_RPLUGIN_MANIFEST *and* NVIM_RPLUGIN_MANIFEST env vars set in
    # the wrapper. That's why only when configure != {} (tested both here and
@@ -42,6 +63,7 @@ let
      [ "${neovim}/bin/nvim" "${placeholder "out"}/bin/nvim" ]
      ++ [ "--set" "NVIM_SYSTEM_RPLUGIN_MANIFEST" "${placeholder "out"}/rplugin.vim" ]
      ++ lib.optionals wrapRc [ "--add-flags" "-u ${writeText "init.vim" neovimRcContent}" ]
      ++ commonWrapperArgs
      ;
  in
  assert withPython2 -> throw "Python2 support has been removed from the neovim wrapper, please remove withPython2 and python2Env.";
@@ -72,7 +94,7 @@ let
      ''
      + lib.optionalString (manifestRc != null) (let
        manifestWrapperArgs =
          [ "${neovim}/bin/nvim" "${placeholder "out"}/bin/nvim-wrapper" ];
          [ "${neovim}/bin/nvim" "${placeholder "out"}/bin/nvim-wrapper" ] ++ commonWrapperArgs;
      in ''
        echo "Generating remote plugin manifest"
        export NVIM_RPLUGIN_MANIFEST=$out/rplugin.vim
@@ -116,6 +138,7 @@ let

    nativeBuildInputs = [ makeWrapper ];
    passthru = {
      inherit providerLuaRc packpathDirs;
      unwrapped = neovim;
      initRc = neovimRcContent;