coc-sumneko-lua icon indicating copy to clipboard operation
coc-sumneko-lua copied to clipboard

Support NixOS

Open lf- opened this issue 3 years ago • 2 comments

Currently the interpreter on the binary builds of sumneko-lua is not present on NixOS. rust-analyzer has the same problem and they fix it by doing this in their code: https://github.com/rust-lang/rust-analyzer/blob/76030ebdeaf759a5d9948ece4ba16092d0039c95/editors/code/src/main.ts#L216

lf- avatar May 19 '22 16:05 lf-

I think this should be lua-language-server's issue

xiyaowong avatar May 19 '22 16:05 xiyaowong

I don't think so; it has to be done on the local system before running the executable, which lands it in the language server client extension.

lf- avatar May 19 '22 18:05 lf-

Here is an configuration working for NixOS (${pkgs.sumneko-lua-language-server} needs to be replaced by real path of sumneko-language-server):

{
  "sumneko-lua.serverDir": "${pkgs.sumneko-lua-language-server}/share/lua-language-server",
  "Lua.misc.parameters": [
    "--metapath",
    "~/.cache/sumneko_lua/meta",
    "--logpath",
    "~/.cache/sumneko_lua/log"
  ]
}

This configuration can generate lua meta data to <metapath> and properly output logs. I find these configuration in nvim-lspconfig. Maybe you can add this example to wiki. @xiyaowong

wrvsrx avatar Sep 20 '22 12:09 wrvsrx

Unfortunately that doesn't work if you're not generating your configuration with nix. If there was a way to provide a plain old executable path, a solution would be a wrapper script like so (completely untested):

#!/usr/bin/env sh
nix run nixpkgs#sumneko-lua-language-server -- "$@"

However you could put it into the nix profile to install it in your user directory so it would be in ~/.nix-profile/share/..., which would work assuming that string is home-expanded.

lf- avatar Sep 20 '22 13:09 lf-

Unfortunately that doesn't work if you're not generating your configuration with nix. If there was a way to provide a plain old executable path, a solution would be a wrapper script like so (completely untested):

#!/usr/bin/env sh
nix run nixpkgs#sumneko-lua-language-server -- "$@"

However you could put it into the nix profile to install it in your user directory so it would be in ~/.nix-profile/share/..., which would work assuming that string is home-expanded.

If you don't want to generate coc configuration file by nix, maybe you can generate a soft link to lua-language-server at a specific position, then set serverDir to the path. That's not a big problem.

wrvsrx avatar Sep 20 '22 18:09 wrvsrx

thanks for the tips, with home-manager you can do it like this: home.nix:

  home.file.sumneko-lua-language-server = {
    source = pkgs.sumneko-lua-language-server;
    target = ".local/share/sumneko-lua-language-server";
  };

coc-settings.json:

    "sumneko-lua.serverDir": "/home/<your-user>/.local/share/sumneko-lua-language-server/share/lua-language-server",

afreakk avatar Jan 14 '23 10:01 afreakk

thanks for the tips, with home-manager you can do it like this: home.nix:

  home.file.sumneko-lua-language-server = {
    source = pkgs.sumneko-lua-language-server;
    target = ".local/share/sumneko-lua-language-server";
  };

coc-settings.json:

    "sumneko-lua.serverDir": "/home/<your-user>/.local/share/sumneko-lua-language-server/share/lua-language-server",

Does it still work for you now? I tried this approach, but I found it didn't work for me.

I tried to fork this repository(this forked repository) and modified some function content for NixOS. And then, I can use this extensions correctly.

PHSix avatar May 11 '23 12:05 PHSix

@PHSix Here's a working example without need of patching

  • home.nix
{ pkgs, ... }:
{
  xdg.dataFile.lua-language-server.source = "${pkgs.sumneko-lua-language-server}/share/lua-language-server";
}
  • coc-settings.json
{
  "Lua.misc.parameters": [
    "--metapath",
    "~/.cache/sumneko_lua/meta",
    "--logpath",
    "~/.cache/sumneko_lua/log"
  ],
  "sumneko-lua.serverDir": "/home/<your user>/.local/share/lua-language-server"
}

wrvsrx avatar May 12 '23 01:05 wrvsrx

This is a suboptimal hack since it does not work across both NixOS and non NixOS systems with the same configuration file (and i don't think (?) you can dynamically configure coc from VimL/lua to get around this).

lf- avatar May 12 '23 05:05 lf-

It works. You just need to link lua-language-server to the same location in both NixOS and non-NixOS systems, and then use the same coc-settings.json I mentioned above.

wrvsrx avatar May 12 '23 05:05 wrvsrx

The result here is that you have to do extra setup that's not automated on non NixOS systems, which isn't ideal.

lf- avatar May 12 '23 05:05 lf-

@PHSix Here's a working example without need of patching

  • home.nix
{ pkgs, ... }:
{
  xdg.dataFile.lua-language-server.source = "${pkgs.sumneko-lua-language-server}/share/lua-language-server";
}
  • coc-settings.json
{
  "Lua.misc.parameters": [
    "--metapath",
    "~/.cache/sumneko_lua/meta",
    "--logpath",
    "~/.cache/sumneko_lua/log"
  ],
  "sumneko-lua.serverDir": "/home/<your user>/.local/share/lua-language-server"
}

I tried to use this solution to solve the problem that coc-sumneko-lua cannot run correctly under NixOS, but using it will prevent me from using the same set of nvim configurations on other non-NixOS platforms. I think this problem can be solved by using vim/lua to dynamically configure coc-settings. It can run normally on my system:

local isNix = (function()
	local version = uv.os_uname().version;
	return string.find(version, "NixOS") ~= nil
end)()

if isNix then
	vim.g.coc_user_config = {
		Lua = {
			misc = {
				parameters = {
					"--metapath",
					"~/.cache/sumneko_lua/meta",
					"--logpath",
					"~/.cache/sumneko_lua/log"
				}
			}
		},
		["sumneko-lua"] = {
			serverDir = string.format("%s/.nix-profile/share/lua-language-server", vim.env.HOME)
		}
	}
end

PHSix avatar May 12 '23 06:05 PHSix

Maybe the issue title should be renamed to "support automatically setup in NixOS out of box" if you don't want extra config on NixOS and non-NixOS. @lf-

In my opinion, there's no need to make every coc.nvim plugin author with a built-in download feature adapt to NixOS using patchelf. Besides, supporting automatically download feature in NixOS also requires dealing with many NixOS-specific boundary cases, such as whether to follow channel/flake updates.


@PHSix Thanks for your sharing! By the way, setting "Lua.misc.parameters" in non-NixOS doesn't influence the usage, it only influences the cache path. Setting "sumneko-lua.serverDir" differently is enough.

wrvsrx avatar May 12 '23 06:05 wrvsrx