Skip to content

[Feature] Add --pack arg to nixvim-print-init #3738

@mairs8

Description

@mairs8

I have been managing my nixvim config in parallel with my lua config as i need to also use a Windows machine.

I recently discovered the nixvim-print-init command to print out nixvims derived init.lua. This was great as it allowed me to take some of my nixvim config with me to other machines. However there was still one piece missing - the installation of the plugins.

The addition of a native plugin manager in nvim-0.12 has now made this a possibility via the vim.pack.add command. All i needed to do was prepend the output from nixvim-print-init with the github url of all of my enabled plugins, wrap in vim.pack.add({...}) and viola... i now had a portable nixvim config that could be used on other machines.

There are some caveats, e.g. there will not be lazy loading functionality on my portable config. However this is a comprise i was willing to make in order to have a single nixvim configuration that i can use on non-nix machines.

Below is the nix command i have been using to make this happen. Most of the complexity is to handle 4 nixvim plugin configs that either 1) didn't have a plugins.enable option resulting in error on json parse (tmux and yq) 2) had an enable option but resulted in error on nix eval (packer and treesitter-playground). It is a messy proof of concept and i am sure there is a more elegant way to embed into nixvim. I imagine a nixvim-print-init --pack flag that will optionally print out the nixvim init.lua including native plugin installation.

(echo "vim.pack.add({"; for plugin in $(nix eval .#nixosConfigurations.$HOST.config.programs.nixvim.plugins --apply 'builtins.mapAttrs (name: plugin: let hasEnable = builtins.tryEval (builtins.hasAttr "enable" plugin); in if hasEnable.success && hasEnable.value then (let result = builtins.tryEval plugin.enable; in if result.success then result.value else null) else null)' --json 2>/dev/null | jq -r 'to_entries | map(select(.value == true)) | .[].key'); do homepage=$(nix eval .#nixosConfigurations.$HOST.config.programs.nixvim.plugins.$plugin.package.meta.homepage --raw 2>/dev/null); if [[ "$homepage" =~ ^https:// ]]; then echo "\"${homepage%/}\","; fi; done; echo "})"; nixvim-print-init) > init.lua

I have tested the above on my nixvim config with ~60 plugins and was able to run on my windows machine without issue. I hope others find this useful and something like this can be added into nixvim to make it portable to Windows,Mac and (non-nixos) Linux machines.

init.lua

vim.pack.add({
"https://github.com/catgoose/nvim-colorizer.lua",
"https://github.com/numtostr/comment.nvim",
"https://github.com/stevearc/conform.nvim",
"https://github.com/mfussenegger/nvim-dap",
"https://github.com/nvimdev/dashboard-nvim",
"https://github.com/stevearc/dressing.nvim",
"https://github.com/folke/flash.nvim",
"https://github.com/nvim-tree/nvim-web-devicons",
"https://github.com/folke/which-key.nvim",
"https://github.com/mikavilpas/yazi.nvim",


-- rest of plugins.....
})
vim.cmd([[
lua require('uv').setup({})
]])
-- Ignore the user lua configuration
vim.opt.runtimepath:remove(vim.fn.stdpath("config")) -- ~/.config/nvim
vim.opt.runtimepath:remove(vim.fn.stdpath("config") .. "/after") -- ~/.config/nvim/after

-- Nixvim's internal module table
-- Can be used to share code throughout init.lua
local _M = {}

-- Set up globals {{{
do
    local nixvim_globals = {
        autoformat = true,
        mapleader = " ",
        markdown_recommended_style = 0,
        root_spec = { "lsp", { ".git", "lua" }, "cwd" },
    }

    for k, v in pairs(nixvim_globals) do
        vim.g[k] = v
    end
end
-- }}}


-- rest of config.....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions