Make a LUA formatter work in NeoVim

I added stylua to my configuration using Mason and wired it into Conform (conventional w/o nixvim) like

local mason = require("mason")
local mason_lspconfig = require("mason-lspconfig")
local mason_tool_installer = require("mason-tool-installer")

-- enable mason and configure icons
mason.setup({
  ui = {
    icons = {
      package_installed = "✓",
      package_pending = "➜",
      package_uninstalled = "✗",
    },
  },
})

local servers = {
  "lua_ls",
  "marksman",
  "omnisharp",
  "bashls",
  "pyright",
  "rust_analyzer",
  "tsserver",
}

local stylers = {
  "prettier",
  "stylua",
  "eslint_d",
}

mason_lspconfig.setup({
  -- list of servers for mason to install
  ensure_installed = servers,
  -- auto-install configured servers (with lspconfig)
  automatic_installation = true, -- not the same as ensure_installed
})

mason_tool_installer.setup({
  -- list of formatters & linters for mason to install
  ensure_installed = stylers,
  -- auto-install configured servers (with lspconfig)
  automatic_installation = true,
})


local conform = require("conform")

conform.setup({
  formatters_by_ft = {
    javascript = { "prettier" },
    typescript = { "prettier" },
    javascriptreact = { "prettier" },
    typescriptreact = { "prettier" },
    css = { "prettier" },
    html = { "prettier" },
    json = { "prettier" },
    yaml = { "prettier" },
    markdown = { "prettier" },
    lua = { "stylua" },
    nix = { "nixfmt" },
    python = { "autopep8" },
  },
  format_on_save = {
    lsp_fallback = true,
    async = false,
    timeout_ms = 1000,
  },
})

when conducting an actual formatting of a LUA file, I get:

14:48:16[ERROR] Formatter 'stylua' error: Could not start dynamically linked executable: /home/{username}/.local/share/nvim/mason/bin/stylua
NixOS cannot run dynamically linked executables intended for generic
linux environments out of the box. For more information, see:
https://nix.dev/permalink/stub-ld

when trying the same configuration but with luaformatter instead of stylua Mason already shows an error during installation:

[ERROR Fri 16 Aug 2024 03:02:16 PM CEST] ...kages/start/mason.nvim/lua/mason-core/installer/init.lua:249: Installation failed for Package(name=luaformatter) error=spawn: luarocks failed with exit code - and signal -. luarocks is not executable

I would appreciate to get some hints or even a working sample to achieve getting a LUA formatter work in NeoVim on NixOS + Home Manager.

I would try to dig into some configs of Nix users that you know use nvim and lua…

1 Like

I have some setup with astro-nvim:

nix run ‘github:Mic92/dotfiles#nvim’

I basically disable installing binaries with mason and have it write a json file instead that I than manually translate back to home-manager. However if you really want to use binaries from mason, have a look at GitHub - Mic92/nix-ld: Run unpatched dynamic binaries on NixOS

1 Like

Thank you @Mic92 - I will dig deeper into nix run github:Mic92/dotfiles#nvim which perfectly runs on my machine

Thank you @mightyiam - one angle I currently follow is to check how nixvim got stylua working …

It’s all in here dotfiles/home/.config/nvim at main · Mic92/dotfiles · GitHub
Some crucial bits are about keeping treesitter grammar and other libraries properly in sync: dotfiles/home-manager/modules/neovim/nvim-standalone.nix at dd354d4408dc3a0fdc0c04152e7318dec36e1bb9 · Mic92/dotfiles · GitHub

1 Like