ayamir / nvimdots

A well configured and structured Neovim.
BSD 3-Clause "New" or "Revised" License
2.82k stars 450 forks source link

Improve NixOS support #1211

Closed misumisumi closed 2 months ago

misumisumi commented 3 months ago

Feature description

I'm glad to have contributed to the support for NixOS last time. Since then, we have been able to operate it stably for several months. However, previous support did not provide an option to place lazy-lock.json under .config/nvim. This is because NixOS places it as read-only, which means it cannot be updated with :Lazy. Fortunately, we were able to create a workflow that updates lazy-lock.json by following upstream in a forked repository based on update_lockfile.yml, so the above problem will be alleviated. I provide bind support in terms of the NixOS ethos of strongly anchoring the environment.

Also, last time it was provided as nixosModules, but since it actually only works with home-manager, we changed it to homeManagerModules. (breaking change) This is because I didn't know about the existence of homeManagerModules last time.

Additional information

name: auto merge upstream
on:
  workflow_dispatch: # allows manual triggering
  schedule:
    - cron: "0 19 * * 5" # runs weekly on Saturday at 04:00 (JST)

permissions:
  contents: write
  pull-requests: write
jobs:
  auto-merge-upstream:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: andstor/file-existence-action@v3
        id: check_lockfile
        with:
          files: "lazy-lock.json"

      - name: Merge upstream
        id: merge-upstream
        continue-on-error: true
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"

          git remote add upstream https://github.com/ayamir/nvimdots.git
          git fetch upstream
          git merge upstream/main --allow-unrelated-histories --no-commit --no-ff

          git reset HEAD lazy-lock.json
          git reset HEAD .github/workflows/update_lockfile.yml

          git checkout lazy-lock.json
          git checkout .github/workflows/update_lockfile.yml

      - name: Run count-new-commits
        run: |
          echo "NEW_COMMIT_COUNT=$(git log --oneline --since '24 hours ago' --perl-regexp --author='^((?!github-actions).*)$' | wc -l)" >> "$GITHUB_ENV"

      - uses: rhysd/action-setup-vim@v1
        if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && env.NEW_COMMIT_COUNT > 0 }}
        with:
          neovim: true

      - name: Run lockfile-autoupdate
        if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && env.NEW_COMMIT_COUNT > 0 }}
        timeout-minutes: 5
        run: |
          ./scripts/install.sh
          nvim --headless "+Lazy! update" +qa
          cp -pv "${HOME}/.config/nvim/lazy-lock.json" .

      - uses: peter-evans/create-pull-request@v6
        if: >-
          ${{
            (
              steps.check_lockfile.outputs.files_exists == 'true' &&
              env.NEW_COMMIT_COUNT > 0
            ) ||
            steps.merge-upstream.outcome == 'success'
          }}
        with:
          token: ${{ github.token }}
          commit-message: "chore: auto merge upstream and update lazy-lock.json"
          title: "chore: auto merge upstream and update lazy-lock.json"
          branch: "merge_upstream_and_update_lazy_lock_action"
          labels: |
            dependencies
            automated
CharlesChiuGit commented 3 months ago

im currently trying home manager on arch and ubuntu lol

CharlesChiuGit commented 3 months ago

do u have some materials on how pull a git repo to certain location via home manager? or do i have to do it via some nix scripts?

misumisumi commented 3 months ago

If you are using flakes.nix, you can use nvimdots.url = "github:misumisumi/nvimdots/improve-nixos"; in inputs.

CharlesChiuGit commented 3 months ago

will this keep it self a valid git repo that i can do git push/pull regularly or it will become a read-only symlink from nix store?

for example, i still want to do normal git push/pull in ~/.config/nvim.

misumisumi commented 3 months ago

will this keep it self a valid git repo that i can do git push/pull regularly or it will become a read-only symlink from nix store?

become a read-only symlink from nix store.

misumisumi commented 3 months ago

I misunderstood my intentions.

do u have some materials on how pull a git repo to certain location via home manager?

If you want to manage dotfiles on nix but don't want to include nvimdots in /nix/store, you can probably use home.activation. This allows you to define additional scripts to run when home-manager is started. For example, if .config/nvim does not exist, the following settings are pulled from the git repo.

{ config
, lib
, pkgs
, ...
}:
{
  home.activation.nvimdotsActivatioinAction = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
    if [ ! -d ${config.xdg.configHome}/nvim ]; then
      ${pkgs.git}/bin/git clone https://github.com/ayamir/nvimdots.git ${config.xdg.configHome}/nvim
    fi
  '';
}
Cyberczy commented 3 months ago

I am very interested in NixOS, but due to the lack of relevant documentation, I have yet to try it. Could you give me some advice or resources?

misumisumi commented 3 months ago

The following materials may be helpful.

Cyberczy commented 3 months ago

thank you!

misumisumi commented 3 months ago

While reviewing the source code, I realized that it could be written better. For example, we are now installing dependencies in the user's environment, which can be replaced with the path from /nix/store. Personally, I was very concerned about the fact that environment variables were declared using alias. Therefore, we have issued a PR that allows you to add build-dependent paths to the home-manager side. No more code is needed to resolve dependencies. (The haskell part etc. are stil required )

Therefore, I would like to make further changes based on upstream trends. Is that ok? Since the PR here only added functionality, I will open the PR again to resolve build dependencies.

j4ger commented 2 months ago

Hi there, I'm new to nixOS and I'm trying to get this to work with my old config. I'm using flakes and the base of nvimdots is working, but I couldn't find a way to merge my user configs, right now I've tried to put:

  xdg.configFile = {
    "nvim/lua/user".source = ./nvim/lua/user;
  };

in my home-manager config, but nix complains about Error installing file '.config/nvim/lua/user' outside $HOME, so I guess this isn't the correct way to do it.

Another issue I've found is that the generated wrapper for nvim doesn't work with fish shell, with the following error:

- (line 1): The function “nvim” calls itself immediately, which would result in an infinite loop.
function nvim  --description 'alias nvim CPATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/include CPLUS_INCLUDE_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/include/c++/v1 LD_LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib NIX_LD_LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib PKG_CONFIG_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/pkgconfig nvim';  CPATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/include CPLUS_INCLUDE_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/include/c++/v1 LD_LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib NIX_LD_LIBRARY_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/lib PKG_CONFIG_PATH=/etc/profiles/per-user/j4ger/lib/nvim-depends/pkgconfig nvim $argv

But it does work fine under bash tho.

misumisumi commented 2 months ago

Hi there, I'm new to nixOS and I'm trying to get this to work with my old config. I'm using flakes and the base of nvimdots is working, but I couldn't find a way to merge my user configs, right now I've tried to put:

I forgot that in nix flake, only git managed files are copied to /nix/store. An error occurs because lua/user in this repository is not managed by git. To resolve this, you need to fork to your own repository and add lua/user.

Another issue I've found is that the generated wrapper for nvim doesn't work with fish shell, with the following error:

Is this due to differences in the syntax of adding environment variables between bash or zsh and fish? The current alias settings have only been confirmed to work with bash and zsh.

misumisumi commented 2 months ago

We will need to adjust your options a bit for both issues. Especially for the latter, it seems necessary to write settings suitable for the shell.

j4ger commented 2 months ago

I forgot that in nix flake, only git managed files are copied to /nix/store. An error occurs because lua/user in this repository is not managed by git. To resolve this, you need to fork to your own repository and add lua/user.

I see. Perhaps an option allowing user specific config directory to be passed in would be nice (I'm new to flakes, is it achievable?), or is it possible to model every possible config entries exposed by nvimdots (sounds like a lot of work)?

Is this due to differences in the syntax of adding environment variables between bash or zsh and fish? The current alias settings have only been confirmed to work with bash and zsh.

I tested this on both stable and 23.11 branch of the official neovim package in nixpkgs and none of them worked, so I believe this is an upstream issue. Curious though as I couldn't find any report on this, maybe it's due to my setup? I'll try to investigate that wrapper script later tomorrow.

misumisumi commented 2 months ago

Perhaps an option allowing user specific config directory to be passed in would be nice (I'm new to flakes, is it achievable?), or is it possible to model every possible config entries exposed by nvimdots (sounds like a lot of work)?

Placing user directories is easy with home-manager. In that case, you need to specify the relative path in your repository instead of the path to this repository.

misumisumi commented 2 months ago

I tested this on both stable and 23.11 branch of the official neovim package in nixpkgs and none of them worked, so I believe this is an upstream issue

Does this mean that the environment variables (PATH etc.) set in the wrapped neovim cannot be used in the first place? is, makeWrapper is the wrapping script. I don't use fish so I don't know if this is a widespread problem.

j4ger commented 2 months ago

Placing user directories is easy with home-manager. In that case, you need to specify the relative path in your repository instead of the path to this repository.

I see, thanks for the explanation! I'll give it a try later.

Does this mean that the environment variables (PATH etc.) set in the wrapped neovim cannot be used in the first place? is, makeWrapper is the wrapping script. I don't use fish so I don't know if this is a widespread problem.

It turns out that there's a wrapper function (different from the wrapper script) defined for fish (I couldn't find out where is was defined, at least not in the source flake of neovim, or is it hidden in one of the nix functions?) that explicitly calls nvim, which should be command nvim, as stated in fish documentation:

If the alias has the same name as the aliased command, you need to prefix the call to the program with command to tell fish that the function should not call itself, but rather a command with the same name. If you forget to do so, the function would call itself until the end of time. Usually fish is smart enough to figure this out and will refrain from doing so (which is hopefully in your interest).

I'm not sure how to fix this because I couldn't locate the source of this wrapper function. Right now I'll use neovide to launch it I guess.

Cyberczy commented 2 months ago

@misumisumi Can we add nixd? Its effect looks like this. a1f1518e0de3904d21816a9255679cbc 7e063bef1149ede49bb00ee5a4e84e8b

misumisumi commented 2 months ago

I am using. It is possible to add settings for nixd I'm planning to include a development environment to make it easier to check the operation of the nix environment, but I'm busy so I don't know when that will happen. I'm going to do it as soon as possible.

CharlesChiuGit commented 2 months ago

Hi, @misumisumi I'm new to nix, so i might have some dumb question lol I recently bump into some issues with mason.nvim when using nix on macOS, hope u might have insight on how to find resources to fix those issues.

When ran :checkhealth, mason.nvim doesn't seem to able to find some of the pkgs installed via nix/home-manager, here's the screenshot: image

When I call :Mason, it seems it failed to connect to github: image

Mason.log

[ERROR Thu Apr 25 11:39:27 2024] ...te/lazy/mason.nvim/lua/mason-registry/sources/github.lua:146: Failed to install registry GitHubRegistrySource(repo=mason-org/mason-registry). "vim/_editor.lua:0: E5560: Vimscript function must not be called in a lua loop callback"
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"clang-format\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"eslint_d\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"jq\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"markdownlint\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"prettier\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"shfmt\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"stylua\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"nixpkgs-fmt\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"bash-language-server\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"docker-compose-language-service\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"dockerfile-language-server\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"json-lsp\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"lua-language-server\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"marksman\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"python-lsp-server\"".
[ERROR Thu Apr 25 11:39:27 2024] ...re/nvim/site/lazy/mason.nvim/lua/mason-registry/init.lua:79: Cannot find package "\"taplo\"".

This doesn't seem to be the issue on Ubuntu w/ nix, I can use mason in nvim normally.

Also, here's my nix/home-manager config: https://github.com/CharlesChiuGit/nix-hm-config

btw, i want to use the nvim-nightly version, so it seems that i cant use home-manager's option to config it like u do?

Is this happens to do with some nix config I need to change? Also I found this plugin: https://github.com/b-src/lazy-nix-helper.nvim Do I need to include this plugin if I want to use nix on other platform than NixOS?

misumisumi commented 2 months ago

I don't have a Mac environment, so I don't know the cause. I'll do some testing on the weekend. Is your Mac an ARM?

misumisumi commented 2 months ago

Also I found this plugin: https://github.com/b-src/lazy-nix-helper.nvim

Apparently this just disables mason.nvim in NixOS.

In this implementation, since we want to use the ecosystem related to mason.nvim (mason-lspconfig.nvim, etc.), LSP and formatter are built with mason.nvim (although this is against the Nix philosophy).

Also, if other plugins also require building, they may not be built correctly in a nix environment.

CharlesChiuGit commented 2 months ago

Is your Mac an ARM?

ys, i'm using m1

I'll do some testing on the weekend.

appreciate that!

although this is against the Nix philosophy

ys, i do notice your PR in home-manager lmao https://github.com/nix-community/home-manager/pull/5225

CharlesChiuGit commented 2 months ago

i think my final goal is:

  1. keeping ~/.config/nvim as a normal git repo that i can do git commands.
  2. use home-manager options to config nvim env in nix, or any other nix methods that can build up a functional nvim env.
  3. using neovim-nightly via overlay

currently i can do 1. & 3., but have no clue how to do 2. 🤣

misumisumi commented 2 months ago

use home-manager options to config nvim env in nix, or any other nix methods that can build up a functional nvim env.

home-manager has an option to configure neovim, so I think you can use that. It provides options to bundle python and lua packages with neovim, and options to configure .config and below. You don't need to use the latter, so 1. and 3. are satisfied.

CharlesChiuGit commented 2 months ago

I did tried it before, but i think home-manager option only supports nvim stable but nightly? or did i miss anything that can let me set it to nightly in https://github.com/nix-community/home-manager/blob/master/modules/programs/neovim.nix ?

misumisumi commented 2 months ago

Need add programs.neovim.package = pkgs.neovim-nightly; in home-manager.

CharlesChiuGit commented 2 months ago

tks! after try and error, i finally got it working! for those who might be interested: https://github.com/CharlesChiuGit/nix-hm-config/blob/a0842603ed9beecab652ac34caa0f8c1ccaa49c5/home.nix#L153-L188

$ nvim --version
NVIM v0.10.0-dev-a736e84
Build type: Release
LuaJIT 2.1.1693350652
Run "nvim -V1 -v" for more info

image image

i'm not sure why luarocks doesn't got detected tho, it should be installed.

misumisumi commented 2 months ago

i'm not sure why luarocks doesn't got detected tho, it should be installed.

extraLuaPackages is probably where you define packages that are used internally, such as python modules. Check and submit a PR.

CharlesChiuGit commented 2 months ago

ys i do place luarocks in extraLuapackages that's why i'm confused lol