Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugins/lazy.nvim: Import Statements & Adding user-specified packages to lazyPath #1904

Closed

Conversation

my7h3le
Copy link

@my7h3le my7h3le commented Jul 21, 2024

When using the lazy nvim plugin manager I couldn't find any way to add import statements to import additional plugin modules in any given spec without having to resort to just using lua in extraConfigLua (See: Importing Specs, config & opts).

This PR adds 2 features:

  • Import Statements
  • Allow user-specified packages to be made available in the lazyPath

I'm still pretty new to Nix and I'd appreciate any suggestions or comments on this PR

Import Statements

Importing a single plugin module

A single plugin module can be imported like this

{
  plugins = {
    lazy = {
      enable = true;
      plugins = with pkgs.vimPlugins; [
        {
          pkg = <some-package>;
          import = "<some-plugin-module>";
        }
      ];
    };
  };
}

The lua equivalent for this would look something like this:

require("lazy").setup({
  spec = {
    { dir = "<path-to-pkg-in-nix-store>", import = "<some-plugin-module>" },
  }
})

Importing multiple plugin modules

Several plugin modules can be imported like this
```Nix
{
  plugins = {
    lazy = {
      enable = true;
      plugins = with pkgs.vimPlugins; [
        {
          pkg = <some-package>;
          import = [
            "<some-plugin-module>"
            "<another-plugin-module>"
          ];
        }
      ];
    };
  };
}

The lua equivalent for this would look something like this:

require("lazy").setup({
  spec = {
    { dir = "<path-to-pkg-in-nix-store>", import = "<some-plugin-module>" },
    { import = "<another-plugin-module>" },
  }
})

Allow user-specified packages to be made available in the lazyPath

Some plugin modules will look for packages/plugins in the lazyPath. This was an issue I encountered when trying to set up LazyVim with nixvim. Adding nix packages to the lazyPath can be done now from any plugin spec.

Maybe using the name packages isn't good for this, but I couldn't think of anything better at the time and I'm open to suggestions.

Single package to add to lazyPath example:

{
  plugins = {
    lazy = {
      enable = true;
      plugins = with pkgs.vimPlugins; [
        {
          pkg = <some-package>;
          packages = [
            "<some-package-to-be-added-to-lazy-path>"
            "<another-package-to-be-added-to-lazy-path>"
          ];
        }
      ];
    };
  };
}

Multiple packages to be added to lazyPath example:

{
  plugins = {
    lazy = {
      enable = true;
      plugins = with pkgs.vimPlugins; [
        {
          pkg = <some-package>;
          packages = [
            "<some-package-to-be-added-to-lazy-path>"
            "<another-package-to-be-added-to-lazy-path>"
          ];
        }
      ];
    };
  };
}

Aside Example: Setting up LazyVim with this PR's changes

Just as an aside I thought it might help or be cool to see how LazyVim can be setup using these changes as a practical example.

The flake.lock and flake.nix for this can be found here https://gist.github.com/tashrifsanil/613a8c127c1a80f3949501590f622feb

flake.nix

{
  description = "Setup LazyVim using NixVim";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.nixvim.url = "github:tashrifsanil/nixvim/lazy-plugin-manager-enhancements";
  inputs.nixvim.inputs.nixpkgs.follows = "nixpkgs";
  inputs.nixvim.inputs.flake-parts.follows = "flake-parts";
  inputs.flake-parts.url = "github:hercules-ci/flake-parts";
  inputs.flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";

  outputs =
    {
      self,
      nixpkgs,
      nixvim,
      flake-parts,
    }@inputs:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [
        "aarch64-darwin"
        "aarch64-linux"
        "x86_64-darwin"
        "x86_64-linux"
      ];

      perSystem =
        {
          pkgs,
          lib,
          system,
          ...
        }:
        let
          miniNvimModules =
            with pkgs.vimPlugins;
            [ mini-nvim ]
            ++
              lib.map
                (miniModule: {
                  name = "${miniModule}";
                  pkg = mini-nvim;
                })
                [
                  "mini.ai"
                  "mini.icons"
                  "mini.move"
                  "mini.pairs"
                  "mini.splitjoin"
                  "mini.trailspace"
                ];

          lazyVimPlugins =
            with pkgs.vimPlugins;
            [
              bufferline-nvim
              catppuccin-nvim
              cmp-buffer
              cmp-nvim-lsp
              cmp-path
              conform-nvim
              dashboard-nvim
              dressing-nvim
              flash-nvim
              friendly-snippets
              gitsigns-nvim
              indent-blankline-nvim
              lazydev-nvim
              lualine-nvim
              #luvit-meta
              mason-lspconfig-nvim
              mason-nvim
              mini-nvim
              neo-tree-nvim
              noice-nvim
              nui-nvim
              nvim-autopairs
              nvim-cmp
              nvim-lint
              nvim-lspconfig
              nvim-notify
              nvim-snippets
              nvim-spectre
              nvim-treesitter
              nvim-treesitter-textobjects
              nvim-ts-autotag
              persistence-nvim
              plenary-nvim
              telescope-fzf-native-nvim
              telescope-nvim
              todo-comments-nvim
              trouble-nvim
              ts-comments-nvim
              which-key-nvim
              tokyonight-nvim
            ]
            ++ miniNvimModules;

          config = {
            extraPackages = with pkgs; [
              # LazyVim
              lua-language-server
              stylua
              # Telescope
              ripgrep
            ];

            plugins = {
              lazy = {
                enable = true;
                plugins = with pkgs.vimPlugins; [
                  {
                    pkg = LazyVim;
                    import = "lazyvim.plugins";
                    packages = lazyVimPlugins;
                  }
                  {
                    pkg = nvim-treesitter;
                    opts = {
                      highlight = {
                        enable = true;
                      };
                      ensure_installed = [
                        "vim"
                        "regex"
                        "lua"
                        "bash"
                        "markdown"
                        "markdown_inline"
                      ];
                      parser_install_dir.__raw = "vim.fs.joinpath(vim.fn.stdpath('data'), 'site')";
                    };
                  }
                  {
                    pkg = telescope-fzf-native-nvim;
                    enabled = true;
                  }
                  {
                    pkg = mason-nvim;
                    enabled = false;
                  }
                  {
                    pkg = mason-lspconfig-nvim;
                    enabled = false;
                  }
                ];
              };
            };

          };
          nixvim' = nixvim.legacyPackages."${system}";
          nvim = nixvim'.makeNixvim config;
        in
        {
          packages = {
            inherit nvim;
            default = nvim;
          };
        };
    };
}

@MattSturgeon
Copy link
Member

I think the underlying issue here is that the plugin type isn't freeform, so users can only use the option interface we've declared.

I'll review this later (unless someone else in @nix-community/nixvim beats me to it) but I think when we have time the implementation should be refactored to comply with rfc42.

@my7h3le
Copy link
Author

my7h3le commented Jul 22, 2024

I think the underlying issue here is that the plugin type isn't freeform, so users can only use the option interface we've declared.

I'll review this later (unless someone else in @nix-community/nixvim beats me to it) but I think when we have time the implementation should be refactored to comply with rfc42.

Oooh I had no idea what rfc42, and freeform was until I looked into it more after your comment. Pretty cool stuff! I think I might be able to refactor at least the lazy plugin manager to comply with rfc42 when I've got time. I have some local test changes that make it so that the setup option (plugins.lazy.setup) uses freeform so doing this:

{
  plugins = {
    lazy = {
      enable = true;
      setup = {
        rtp = {
          disabled_plugins = [
            "gzip"
            "tarplugin"
            "tohtml"
            "tutor"
            "zipplugin"
          ];
        };
      };
    };
  };
}

would lead to this lua code without having to explicitly define the rtp and disabled_plugins options:

require("lazy").setup({
  performance = {
    rtp = {
      disabled_plugins = {
        "gzip",
        "tarPlugin",
        "tohtml",
        "tutor",
        "zipPlugin",
      },
    },
  },
})

Just need to do something similar for lazy plugins

@MattSturgeon
Copy link
Member

You could reference the #1874 (lz-n) PR that has had the same issue.

Though it isn't important to get this all done in one single PR, once reviewed this could be merged and then another PR could refactor the setup options and another do the plugins? (For example)

@my7h3le
Copy link
Author

my7h3le commented Aug 25, 2024

You could reference the #1874 (lz-n) PR that has had the same issue.

Though it isn't important to get this all done in one single PR, once reviewed this could be merged and then another PR could refactor the setup options and another do the plugins? (For example)

So it's been a while since I could get back to this but I've created another PR that adds a freeform setup option, and makes the existing pluginType freeform here #2082

- Adds support for `import` statements to import additional plugin
  modules in any given spec.
- For multiple import modules, additional specs for each import are
  automatically created.
… in the `lazyPath`

- The `packages` attribute in the scope of `plugins.lazy.plugins` now
  allows user-specified packages to be made avalable in the `lazyPath`.
- When using `import` statements to import additional plugin modules,
  those imported plugin modules might require certain plugins/packages
  to be available in the `lazyPath`. For example importing the plugin
  module `lazyvim.plugins` when using LazyVim necessitates this feature.
@my7h3le my7h3le force-pushed the lazy-plugin-manager-enhancements branch from 07dd285 to 622041b Compare September 10, 2024 00:03
@my7h3le my7h3le marked this pull request as draft September 10, 2024 00:04
@my7h3le
Copy link
Author

my7h3le commented Sep 10, 2024

This is better handled by #2082

@my7h3le my7h3le closed this Sep 10, 2024
@my7h3le my7h3le deleted the lazy-plugin-manager-enhancements branch September 10, 2024 00:05
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.

2 participants