nix-community / home-manager

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

Neovim option to avoid writing init.vim #1907

Closed DanCardin closed 2 years ago

DanCardin commented 3 years ago

Issue description

https://github.com/nix-community/home-manager/commit/8e0c1c55fbb7f16f9fd313275ddf63c97b34394c, specifically

    xdg.configFile."nvim/init.vim".text = neovimConfig.neovimRcContent;

appears to have introduced a "regression" in my usage, given that I define much of my dotfile/XDG_CONFIG standalone in a way that doesn't depend on nix, so I therefore intentionally have a .config/nvim/init.vim. When running home-manager switch i now see the following:

Existing file '<elided>/.config/nvim/init.vim' is in the way of '/<elided>/.config/nvim/init.vim'
Please move the above files and try again or use -b <ext> to move automatically.

Ideally, there would be an option available to disable this behavior, but if it's an antigoal to provide this kind of customization, is there a straightforward way for me to override or patch remove that particular line, so that it doesn't attempt to write the init.vim file?

Meta

Maintainer CC

@teto

Technical details

Can do this, but it feels unimportant given the above context.

teto commented 3 years ago

I am in a similar scenario where my config is a complete mess of nix/vimscript/lua. One question first, do you have a config generated in nix ? if you've got no config, home-manager could skip writing the init.vim (which is not the case I think because we always have some init.vim like set compatible because it's generated by the vimrcContent function).

I have a fork of home-manager where I had changed home-manager to generate init.generated.vim instead of init,vim. And I would source it from my manual init.vim. I now do the reverse (to more easily test the home-manager master branch,

With the upcoming neovim, one can write a lua config too. I have one that I source too via luafile. I guess we will have to support luaConfig for plugins etc and generate a lua file as well.

DanCardin commented 3 years ago

My home-manager config for neovim looks like:

  programs.neovim = {
    enable = true;
    package = pkgs.neovim-unwrapped;
    vimAlias = true;
    withNodeJs = false;
    withPython3 = true;
    withRuby = false;

    extraPython3Packages = (ps: with ps; [
      jedi
      pynvim
      pkgs.python37Packages.python-language-server
      pkgs.python37Packages.pyls-mypy
      pkgs.python37Packages.pyls-isort
      pkgs.python37Packages.pyls-black
    ]);
  };

Which I only mention to show that i am only using it for making available dependencies for the lsp tooling.

Ulitmately i'd ideally prefer that it not generate any rc file (because I have all the options set myself, in my normal init.vim or init.lua). I guess I could configure nix to source foo.vim (and rename my init.vim to foo.vim), but i'd really prefer my config to be usable without nix.

Re lua, i almost did the opposite, make an init.lua, that immediately sources a renamed init.vim, to circumvent the issue, but the nvim help for it says that using init.lua and init.vim together will error, so i didn't bother.

Is there no other way to patch or otherwise override the modules/programs/neovim.nix file? (other than forking or someone applying a fix here and updating home-manager)?

teto commented 3 years ago

Starting neovim with init.lua hasn't reached feature parity with an init.vim so yeah I recommand sticking to init.vim .

Out of curiosity what's the content of the generated init.vim ? I would support a patch that doesn't generate the init.vim if not necessary. Otherwise I think it's fair for home-manager to create an init.vim and it an option would be meaningless.

Alternatively the home-manager is a lightwieght module around nixpkgs utilities so you could copy/paste what you need into your config. You can look into nixpkgs' pkgs/applications/editors/neovim/utils.nix for the relevant functions.

DanCardin commented 3 years ago
set nocompatible

set packpath^=/nix/store/ckjy56k5x70dmyzjgg4xl8by5fcjaa4n-vim-pack-dir
set runtimepath^=/nix/store/ckjy56k5x70dmyzjgg4xl8by5fcjaa4n-vim-pack-dir

filetype indent plugin on | syn on

I have no opinion on what happens if there's not already an init.vim there šŸ˜›

For maintainability's sake i'd rather not fork the whole module (and i've already encountered issues attempting it šŸ˜›), and if you're open to changing it, then i'm happy to wait (Although I'm afraid i really wouldn't even know where to start as far as it being me writing said patch šŸ˜¶)

teto commented 3 years ago

sry dont have time to do it but the goal is to write the file only if neovimrcContent != null at https://github.com/nix-community/home-manager/blob/be56b6f2c5df6a4da8c91d4a3174f77d66c35533/modules/programs/neovim.nix#L266 which leads to track how neovimrcContent is generated https://github.com/NixOS/nixpkgs/blob/024cee253499a5b10bee2e16ef0da95378838e52/pkgs/applications/editors/neovim/utils.nix#L99 so this will likely require nixpkgs too.,

sumnerevans commented 3 years ago

@DanCardin I know it's not ideal, but as a workaround, you could move your existing configuration to a different location and then source it using programs.neovim.extraConfig.

teto commented 3 years ago

related: https://github.com/NixOS/nixpkgs/issues/118953 there is a non-null defaut configuration that forces the creation of the file while it should be possible to avoid it.

teto commented 3 years ago

I've merged some changes in nixpkgs that should make it possible if you want to take a stab at this.

NB: pass beforePlugins = "" to makeNeovimConfig

wbadart commented 3 years ago

Here's a quick fix I've been using to maintain an init.vim outside of my home-manager config:

  programs.neovim = {
    enable = true;
    plugins = with pkgs.vimPlugins; [
      # ...
    ];
    extraConfig = builtins.readFile ./config/init.vim;
  };

where ./config/ is just a directory I have sitting alongside my home-manager configuration containing all my non-nix dotfiles.

Congee commented 3 years ago

This makes me really sad that I have to change my init.lua to init.vim and read it into peograms.neovim.extraConfig. https://github.com/nix-community/home-manager/blob/be56b6f2c5df6a4da8c91d4a3174f77d66c35533/modules/programs/neovim.nix#L266

Will this "bug" ever be fixed?

Congee commented 3 years ago

Well, https://github.com/nix-community/home-manager/pull/1945 might be a fix šŸ˜…

Congee commented 3 years ago

Still having this issue of writing set packpath=... to init.vim even with https://github.com/nix-community/home-manager/pull/2058 and https://github.com/NixOS/nixpkgs/pull/125547 merged.

After pulling the latest channels from home-manager and nixpkgs-unstable, inspection the derivation of this line

installPhase = lib.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList packageLinks packages));

shows

"installPhase": "mkdir -p $out/pack/home-manager/start\nmkdir -p $out/pack/home-manager/opt",

from https://github.com/NixOS/nixpkgs/blob/1d1c6753c6fdbfb345743858131899d70431de84/pkgs/misc/vim-plugins/vim-utils.nix#L206-L213

Weird, home-manager itself seems passed as a package in packages the argument to nativeImpl.

@teto any idea? :thinking:

teto commented 3 years ago

@Congee I've just uncommented the neovim-no-init test and it creates an init.vim with packpath. the packpath is empty though so that's strange. You seem to know the code quite well by now so I am confident you can find the solution :) the check at https://github.com/NixOS/nixpkgs/blob/1d1c6753c6fdbfb345743858131899d70431de84/pkgs/misc/vim-plugins/vim-utils.nix#L344 doesn't seem to work correctly, you can run builtins.trace to check the value of packages.

OlivierNicole commented 3 years ago

I'm a bit lost between all the revisions and patch suggestions, but I still have this issue on nixos-stable-21.05 and release-21.05 for home-manager:

Existing file '/home/olivier/.config/nvim/init.vim' is in the way of '/nix/store/m3y7gwypvwd9i5iz94vh036xh97vkdi1-home-manager-files/.config/nvim/init.vim'
Please move the above files and try again or use -b <ext> to move automatically.

although my neovim config is just:

  programs.neovim = {
    enable = true;
    extraPython3Packages = ps: with ps; [ msgpack ];
  };
alexpearce commented 3 years ago

In case others were similarly confused, this issue prevents folks using a purely lua-based Neovim configuration.

A lua-based configuration starts with an init.lua file and Neovim will not start if it detects both an init.lua and an init.vim file. As Home Manager unconditionally creates an init.lua file one cannot use it like this:

programs.neovim = {
  enable = true;
  # ...
};

xdg.configFile."nvim/init.lua".source = ./init.lua;

My workaround is to just add Neovim to packages and then install my entire Neovim dotfiles explicitly:

packages = with pkgs; [ neovim ];

xdg.configFile.nvim = {
  source = ./config/neovim;
  recursive = true;
};
mtrsk commented 3 years ago

I'm currently migrating to lua and doing the following hack to avoid this:

  programs.neovim = {
    enable = true;
    # ...
    extraConfig = builtins.concatStringsSep "\n" [
      ''
      luafile ${builtins.toString ../dotfiles/nvim/settings.lua}
      luafile ${builtins.toString ../dotfiles/nvim/lsp.lua}
      ''
    ];

    plugins = with pkgs.vimPlugins; [
        # ...
    ];
  };

  xdg.configFile = {
    nvim = {
      source = ../dotfiles/nvim;
      recursive = true;
    };
  };

This will generate an init.vim that just loads lua files. Not ideal, but werks, at least my versioned config will only contain lua files.

bhavitsharma commented 3 years ago

I'm currently migrating to lua and doing the following hack to avoid this:

  programs.neovim = {
    enable = true;
    # ...
    extraConfig = builtins.concatStringsSep "\n" [
      ''
      luafile ${builtins.toString ../dotfiles/nvim/settings.lua}
      luafile ${builtins.toString ../dotfiles/nvim/lsp.lua}
      ''
    ];

    plugins = with pkgs.vimPlugins; [
        # ...
    ];
  };

  xdg.configFile = {
    nvim = {
      source = ../dotfiles/nvim;
      recursive = true;
    };
  };

This will generate an init.vim that just loads lua files. Not ideal, but werks, at least my versioned config will only contain lua files.

To add, I renamed init.lua to init_lua.lua and did this:

  programs.neovim = {
   enable = true;
    viAlias = true;
    vimAlias = true;
    extraConfig = builtins.concatStringsSep "\n" [
      ''
      luafile ${builtins.toString ./init_lua.lua}
      ''
    ];
JonathanLorimer commented 3 years ago

It would be nice if home-manager provided an option that accepted an absolute path to your nvim "runtime path". This would enable users to take advantage of lua modules https://github.com/nanotee/nvim-lua-guide#modules . The biggest issue I have with my current config is that I cannot share any lua code between files, because local modules are not supported.

teto commented 2 years ago

for info https://github.com/nix-community/home-manager/pull/2637 can help you generate a lua file from your home-manager config. Would be cool if someone could fix https://github.com/nix-community/home-manager/blob/9d369c75ce2fdeb296ad42bcdc8c1a523c494550/tests/modules/programs/neovim/default.nix#L5 we could have some intelligence in home-manager that generates an init.lua in absence of generated vimscript.

marcelarie commented 2 years ago

Is it possible to fetch the config from a git repo directly?

teto commented 2 years ago

@marcelarie sure, there are some examples that were presented on the discourse, for instance https://github.com/jordanisaacs/neovim-flake. These are flake-based but nothing prevents you to adapt it.

marcelarie commented 2 years ago

@marcelarie sure, there are some examples that were presented on the discourse, for instance https://github.com/jordanisaacs/neovim-flake. These are flake-based but nothing prevents you to adapt it.

Thanks!! I'm really new to nix, is flake and home-manager combinable??

teto commented 2 years ago

they are but if you are new to nix, I am not sure what to recommand, on one side, flakes solve tons of UX/UI issues nix had but had the same time they are still experimental, with fewer examples and possibly harder if you dont have a good knowledge of nix the programming language.

marcelarie commented 2 years ago

The way you said it seems flakes are the best option on the long run. I will give it a try. Thanks!

teto commented 2 years ago

I've updated most of my setup to lua and got interested in fixing that, which translates into this PR https://github.com/NixOS/nixpkgs/pull/184364 . Not sure it solves this issue yet but it should. I've got a pretty crazy/custom neovim setup so I would like to know if anyone could test that PR and tell me if it broke anything for them ?

teto commented 2 years ago

while preparing another neovim PR, I've reenabled tests/modules/programs/neovim/no-init.nix and seems like this issue is fixed so closing.

NB: init.lua is not generated automatically though so you still have to add sthg like xdg.configFile = { "nvim/lua/init-home-manager.lua".text = extraLuaConfig; }. My next PR is on easing the addition of runtime files.

teto commented 2 years ago

if you have some plugin-specific lua config, home-manager still creates an init.vim to write lua require('init-home-manager') which is a pain. I will make HM write an init.lua instead ( once https://github.com/nix-community/home-manager/pull/3168 is merged)

EDIT: plugins without config still generate an empty string (HM only) :/ fix incoming..

teto commented 2 years ago

I reopen because of the issues up from my previous message.

DanCardin commented 2 years ago

I ideally dont want any file (init.vim/init.lua) written under any circumstances, since i maintain my configuration for nvim outside the context of nix. In fact, the autogenerated mucking about with the path breaks my config despite use of extraConfig to source my own files.

Today i need to manually mv ~/.config/nvim/init.vim{2,} and back before home-manager switching. Lest i get Existing file '.../.config/nvim/init.vim' is in the way of '.../.config/nvim/init.vim' Please move the above files and try again or use 'home-manager switch -b backup' to back up existing files automatically.

Assuming your above fix for empty string is merged at some point, what exactly (if anything) do i need to change about my config to prevent it from doing so?

teto commented 2 years ago

the init.vim is created when this condition is fullfilled https://github.com/nix-community/home-manager/blob/60c6bfe322944d04bb38e76b64effcbd01258824/modules/programs/neovim.nix#L363 . You will notice that even when no config is set, the init.vim ends up with multiple lines and here begins the rabbithole of preventing these empty lines from being generated.

teto commented 2 years ago

Looking for some people to try this https://github.com/nix-community/home-manager/pull/3233 . It shouldn't create any init.vim in absence of viml config (for real this time, I've improved the test).

DanCardin commented 2 years ago

It prints Path '~/.config/nvim/init.vim' does not link into a Home Manager generation. Skipping delete. in yellow (which i was initially mildly concerned about before reading more closely), but otherwise does not fail now.

So i'm very happy, thanks!