nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
6.32k stars 1.69k forks source link

bug: Neovim sources package configs before extraLuaConfig #4609

Open twiclo opened 8 months ago

twiclo commented 8 months ago

Are you following the right branch?

Is there an existing issue for this?

Issue description

Here is a segment of my home.nix

programs = {
  neovim = let
    toLua = str: "lua << EOF\n${str}\nEOF\n";
    toLuaFile = file: "lua << EOF\n${builtins.readFile file}\nEOF\n";
  in {
    enable = true;
    viAlias = true;
    vimAlias = true;
    vimdiffAlias = true;
    extraLuaConfig = ''
      ${builtins.readFile ./nvim/lua/set.lua}
      ${builtins.readFile ./nvim/lua/remap.lua}
    '';
    extraPackages = with pkgs; [
      ripgrep
      xclip
      rnix-lsp
    ];
    plugins = with pkgs.vimPlugins; [
      {
        plugin = (nvim-treesitter.withPlugins (p: [
          p.tree-sitter-bash
          ...
        ]));
        config = toLuaFile ./nvim/plugin/treesitter.lua;
      }
      {
        plugin = indent-blankline-nvim;
        config = toLua ''
          require('ibl').setup {
            indent = { char = "│" },
            scope = { enabled = false }
          }
        '';
      }
      { plugin = telescope-nvim; config = toLuaFile ./nvim/plugin/telescope.lua; }
      #{ plugin = melange-nvim; config = "colorscheme melange"; }
      { plugin = gruvbox; config = "colorscheme gruvbox"; }
      nvim-web-devicons
      vim-commentary
      nvim-ts-autotag
    ];
  };

The first line of set.lua is vim.g.mapleader = " " Here is my telescope.lua

local builtin = require('telescope.builtin')
-- vim.g.mapleader = " "
vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<C-p>', builtin.git_files, {})
vim.keymap.set('n', '<leader>g', builtin.live_grep, {})
print("This loaded")

The print works, the file is getting loaded in. If I comment out that mapleader set then my leader key bindings work. If I start neovim and run verbose map <leader>g it says no mapping is found. I'm pretty sure that the extraLuaConfig block is being added AFTER the plugin configs. I also tried creating a nvim/init.lua and putting the leader map in there with no luck

Maintainer CC

No response

System information

- system: `"x86_64-linux"`
 - host os: `Linux 6.1.59, NixOS, 23.05 (Stoat), 23.05.20231021.5550a85`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.13.6`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos
teto commented 8 months ago

I had started writing a message on how to work around this but lost it. Anyway I have plans to make this possible via the standard way of lib.mkBefore / lib.mkAfter but it needs some patches that are in nixpkgs' staging-next branch. Till then you can use https://github.com/nix-community/home-manager/pull/2391 or add a fake plugin with your config probably.

twiclo commented 8 months ago

My workaround so far has been to put the leader setting at the top of the first loaded file.

stale[bot] commented 5 months ago

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

* If this is resolved, please consider closing it so that the maintainers know not to focus on this. * If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

* If you are also experiencing this issue, please add details of your situation to help with the debugging process. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

avrahamappel commented 1 week ago

There's still no option to add vim or lua lines before the plugin configs.

Followed @twiclo's advice and declared a dummy plugin at the beginning of my plugin list

    programs.neovim.plugins = [
      # Null plugin, just sets leader keys
      # Must be on top so it's the first thing written to init.vim
      {
        plugin = pkgs.stdenv.mkDerivation {
          name = "vim-plugin-set-leader-keys-to-space";
          src = ./empty;
          installPhase = ''
            cp -r $src $out
          '';
        };
        config = ''
          let g:mapleader = ' '
          let g:maplocalleader = ' '
        '';
      }
      # actual plugins...
    ];