Skip to content

Conversation

@QuiNzX
Copy link

@QuiNzX QuiNzX commented Jan 22, 2025

In this PR I've added ruff as lsp.

Breaking change:
What has changed is that before cfg.lsp.server expected a single value but now it expects a list.
So that ruff can work in conjuction with basedpyright(or any other lsp) which is recommended according to the ruff docs.

I might going to add more granular options for the lsp if you like unless you have something else in mind that will do so. ruff lsp
This is my first nix pr so let me know if I made any mistakes!

EDIT: because of the toList function the breaking change isn't the case anymore for cfg.lsp.server .
Let me know if there is a better approach to it!
However the breaking change is still in the case of python.lsp.package which now requires each lsp server specified as an attribute set . On the bright side you can now set arguments for each lsp.
Like so:

package = {
  ruff = [lib.getExe pkgs.ruff "server"];
  basedpyright = [lib.getExe pkgs.basedpyright];
};
  • I have updated the [changelog] as per my changes
  • I have tested, and self-reviewed my code
  • Style and consistency
    • I ran Alejandra to format my code (nix fmt)
    • My code conforms to the [editorconfig] configuration of the project
    • My changes are consistent with the rest of the codebase
  • If new changes are particularly complex:
    • My code includes comments in particularly complex areas
    • I have added a section in the manual
    • (For breaking changes) I have included a migration guide
  • Package(s) built:
    • .#nix (default package)
    • .#maximal
    • .#docs-html (manual, must build)
  • Tested on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin

@QuiNzX QuiNzX requested a review from NotAShelf as a code owner January 22, 2025 00:18
github-actions bot pushed a commit that referenced this pull request Jan 22, 2025
@github-actions
Copy link

github-actions bot commented Jan 22, 2025

🚀 Live preview deployed from 14fb425

View it here:

Debug Information

Triggered by: QuiNzX

HEAD at: feat-ruff

Reruns: 478

github-actions bot pushed a commit that referenced this pull request Jan 23, 2025
Copy link
Owner

@NotAShelf NotAShelf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes to remain consistent with the rest of the codebase, and to conform to our code styles.

To be honest, I don't quite like how this is handled. Not to say I can think of anything better at the moment, but this is entirely different from other language modules and might cause confusion.

I'll probably merge this after the requested changes are implemented, but I think a cleanup is due in the language module.


[QuiNzX](https://github.com/QuiNzX):

[ruff lsp]: (https://github.com/astral-sh/ruff)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reference is not used anywhere. You can remove it if you do not intend to refer to it as [ruff lsp] inline

example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
type = lib.types.attrsOf (either package (listOf str));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't access lib inline

Suggested change
type = lib.types.attrsOf (either package (listOf str));
type = attrsOf (either package (listOf str));

where attrsOf is inherited from types in the top-level let in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!
I've made the necessary changes. However I have yet to find a way to stay consistent with the rest of the codebase. I'm looking forward to the language module cleanup so that my diabolical change will be resolved.

(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.python-lsp = servers.${cfg.lsp.server}.lspConfig;
vim.lsp.lspconfig.sources = lib.genAttrs (lib.toList cfg.lsp.server) (name: servers.${name}.lspConfig);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move toList from an inherit at the top.

github-actions bot pushed a commit that referenced this pull request Jan 25, 2025
@NotAShelf NotAShelf force-pushed the main branch 5 times, most recently from 02ee4cc to bc978c4 Compare March 17, 2025 11:42
github-actions bot pushed a commit that referenced this pull request Mar 19, 2025
github-actions bot pushed a commit that referenced this pull request Mar 23, 2025
@tebuevd
Copy link
Contributor

tebuevd commented Mar 26, 2025

Looking forward to this landing in some form or fashion! Using multiple LSPs (Ruff + something else) is very common in modern Python development and is the preferred workflow for tons of devs.

@machshev
Copy link

machshev commented Oct 7, 2025

Thanks @QuiNzX, I've just managed to get nvf setup for the first time. Really exciting to be able to migrate to a purely Nix based config. However without ruff + pyright (or pyerfly) support it's completely unusable for python development, so unfortunately I'm going to have to go back to my non Nix config for the moment.

Is there an update on this PR? It seems to have stalled for quite a while now. Is there any chance of this getting merged soon, or is there a fundamental issue? Is there something we can do to help? @NotAShelf

@machshev
Copy link

machshev commented Oct 14, 2025

In case anyone else finds this and wonders what the current state is. At the time of writing the language.python.lsp option uses the deprecated lspconfig instead of the new vim.lsp neovim API. It also has a major limitation in only allowing a single LSP.

Support for vim.lsp API has been added in #860 (using the option vim.lsp.servers). Some helpful explanation on how to use this option can be found in #940 as well as the options reference.

I've managed to get Ruff + pyright (as well as pyrefly and ty) working by disabling the python module LSP option and using the vim.lsp.servers option directly. Ideally the language modules are updated to use the new LSP API and support multiple LSPs per language, as others have said this is a must for Python as there is currently no single LSP that does both lint and type checking (astral may provide this with a unified ruff+ty at some point?).

lsp = {
  enable = true;

  inlayHints.enable = true;
  formatOnSave = true;
  lspkind.enable = true;
  lightbulb.enable = true;
  trouble.enable = true;

  servers = {
    "*" = {
      root_markers = [".git"];
      capabilities = {
        textDocument = {
          semanticTokens = {
            multilineTokenSupport = true;
          };
        };
      };
    };
    "typos_lsp" = {};
    "ruff" = {
      root_markers = [".git" "pyproject.toml" "setup.py"];
      filetypes = ["python"];
    };
    "pyright" = {
      root_markers = [".git" "pyproject.toml" "setup.py"];
      filetypes = ["python"];
    };
    #"pyrefly" = {
    #  root_markers = [".git" "pyproject.toml" "setup.py"];
    #  filetypes = ["python"];
    #};
    #"ty" = {
    #  root_markers = [".git" "pyproject.toml" "setup.py"];
    #  filetypes = ["python"];
    #};
  };

  mappings = {
    previousDiagnostic = "[d";
    nextDiagnostic = "]d";
  };
};

# Disable the LSP option here as it conflicts with the above settings.
languages = {
  enableFormat = true;
  enableTreesitter = true;
  enableExtraDiagnostics = true;

  python = {
    enable = true;
    lsp.enable = false; # use `lsp.servers` instead
    format.type = "ruff"; # default is black and conflicts with ruff
  };
};

# Make sure the extra LSPs are installed - this is normally done in the language modules so you don't have to do it explicitly.
extraPackages = [pkgs.pyright pkgs.ruff pkgs.ty];

@NotAShelf
Copy link
Owner

This is a little late, but we're currently working on multiple formatters & multiple LSPs via the language modules in the v0.8 branch, which is set to be released soon. If this PR can be rebased on v0.8, we should be able to merge it with minimal changes to follow the new API imposed in the language modules.

Of course vim.lsp is a feasible option as well.

@machshev
Copy link

machshev commented Jan 7, 2026

I'd really like this, but this PR seems stale. So I've opened up #1320 which uses the new multiple LSP servers option. Which means the manual version described above (#575 (comment)) is no longer required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants