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

minimal init.lua with pckr.nvim for neorg with luarocks support #1629

Open
1 task done
VioletJewel opened this issue Jan 8, 2025 · 2 comments
Open
1 task done

minimal init.lua with pckr.nvim for neorg with luarocks support #1629

VioletJewel opened this issue Jan 8, 2025 · 2 comments
Labels
feature Issues related to feature proposals. Please attach a module.

Comments

@VioletJewel
Copy link

VioletJewel commented Jan 8, 2025

Issues

  • I have checked existing issues and there are no existing ones with the same request.

Feature description

I made a minimal init.lua with pckr.nvim for neorg with luarocks support to automatically ensure that luarocks rocks are installed before installing and setting up neorg and its dependencies. Here is a gist, and for redundancy the code is as follows:

minimal init.lua with pckr.nvim for neorg with luarocks support
-- ========================= --
-- Set up luarocks variables --
-- ========================= --

local rocksDeps = {
  -- neorg
  'lua-utils.nvim',
  'nvim-nio',
  'nui.nvim',
  'plenary.nvim',
  'pathlib.nvim',
  -- image.nvim
  { 'magick', withArgs = '--dev' },
}

local luaVersion = _VERSION:gsub('Lua ', '')
local rockDir = vim.fs.joinpath(os.getenv 'HOME', '.luarocks', 'share', 'lua', luaVersion)
local manifestFile = vim.fs.joinpath(os.getenv 'HOME', '.luarocks', 'lib', 'luarocks', 'rocks-5.1', 'manifest')
local shFmt = [[sh -c ">/dev/null 2>/tmp/.nvim_luarock_err.log luarocks --local --lua-version='%s' install %s %s"]]

if not package.path:find(vim.pesc(rockDir)) then
  print('adding to path')
  package.path = package.path
      .. ';' .. vim.fs.joinpath(rockDir, '?.lua')
      .. ';' .. vim.fs.joinpath(rockDir, '?', 'init.lua')
end


-- ================================ --
-- Set up luarocks helper functions --
-- ================================ --

local function installRock(rock)
  if not vim.fn.executable 'luarocks' then
    vim.notify("'luarocks' is not installed!", vim.log.levels.ERROR)
    return false
  end
  local shCmd = string.format(shFmt, _VERSION:gsub('Lua ', ''),
    rock.withArgs or ' ', rock[1] or rock)
  print(string.format('Installing rock: %q', rock[1] or rock))
  local exit = os.execute(string.format(shCmd, luaVersion, rock))
  -- handle errors
  if exit == 0 then return true end
  local fd = assert(vim.uv.fs_open('/tmp/.nvim_luarock_err.log', 'r', 256))
  local data = assert(vim.uv.fs_read(fd, assert(vim.uv.fs_fstat(fd)).size, 0))
      :gsub('\n', '\n >> ')
  assert(vim.uv.fs_close(fd))
  vim.notify(string.format(
    'Error installing lua rock: %q; see error log:\n >> %s',
    rock, data), vim.log.levels.ERROR)
  return false
end

local function ensureRocks(rocks, cb)
  local manifest = {}
  local fd = vim.uv.fs_open(manifestFile, 'r', 256)
  if fd ~= nil then
    local data = assert(vim.uv.fs_read(fd, assert(vim.uv.fs_fstat(fd)).size, 0))
    assert(vim.uv.fs_close(fd))
    local f = assert(loadstring(data))
    setfenv(f, manifest)
    f()
  end
  local repo = manifest.repository or {}
  vim.iter(rocks or rocksDeps)
      :each(function(dep, _)
        if repo[dep[1] or dep] == nil then
          installRock(dep)
        end
      end)
  if cb then cb() end
end

-- -- Uncomment if you need to re-install Rocks for neorg
-- vim.api.nvim_create_user_command('InstallNeorgRocks', function()
--   vim.iter(rocksDeps):each(function(rock) installRock(rock) end)
-- end, {})


-- ================ --
-- Set up pckr.nvim --
-- ================ --

local pckr_path = vim.fn.stdpath("data") .. "/pckr/pckr.nvim"

if not (vim.uv or vim.loop).fs_stat(pckr_path) then
  vim.fn.system({
    'git',
    'clone',
    "--filter=blob:none",
    'https://github.com/lewis6991/pckr.nvim',
    pckr_path
  })
end

vim.opt.rtp:prepend(pckr_path)


-- ==================== --
-- Set up pckr packages --
-- ==================== --

local cmd = require 'pckr.loader.cmd'
-- local keys = require 'pckr.loader.keys'
-- local event = require 'pckr.loader.event'

require 'pckr'.add {

  {
    'nvim-neorg/neorg',
    requires = { '3rd/image.nvim', 'nvim-treesitter/nvim-treesitter' },
    tag = '*', -- Pin Neorg to the latest stable release
    cond = cmd 'Neorg',
    config = function()
      ensureRocks({ 'lua-utils.nvim', 'nvim-nio', 'nui.nvim', 'plenary.nvim', 'pathlib.nvim' }, function()
        require 'neorg'.setup {
        }
      end)
    end,
  },

  {
    '3rd/image.nvim',
    config = function()
      ensureRocks({ { 'magick', withArgs = '--dev' } }, function()
        require 'image'.setup {
          backend = 'kitty',
          processor = 'magick_rock',
          markdown = {
            enabled = true,
            clear_in_insert_mode = true,
            download_remote_images = true,
            only_render_image_at_cursor = false,
            floating_windows = false,
            filetypes = { 'markdown' },
          },
          neorg = {
            enabled = true,
            filetypes = { 'norg' },
          },
        }
      end)
    end
  },

  {
    'nvim-treesitter/nvim-treesitter',
    config = function()
      require 'nvim-treesitter.configs'.setup {
        auto_install = true,
        ensure_installed = { 'lua', 'vim', 'vimdoc', 'query', 'norg', 'markdown', 'markdown_inline' },
        sync_install = false,
        highlight = { enable = true, },
        indent = { enable = true },
        matchup = { enable = true, },
      }
    end,
  },

  {
    'nvim-treesitter/nvim-treesitter-textobjects',
    requires = { 'nvim-treesitter/nvim-treesitter', },
    config = function()
      require 'nvim-treesitter.configs'.setup {
      }
    end,
  },

}

I'm sorry it's really long. The other modern package managers handle all of this grit.

Help

Yes

Implementation help

I already have performed the work. I could add it to the README. I could also perform any requested modifications, if it is unsuitable for the README.

@VioletJewel VioletJewel added the feature Issues related to feature proposals. Please attach a module. label Jan 8, 2025
@github-project-automation github-project-automation bot moved this to added-updated-reopened in sorting neorg issue tracker Jan 8, 2025
@benlubas
Copy link
Contributor

We already have a more minimal init file using lazy here: https://github.com/nvim-neorg/neorg/wiki/Kickstart#creating-our-init-file

I don't think there's much point in keeping one around for pckr as well.

@Anrock
Copy link

Anrock commented Jan 14, 2025

I think there is a point of keeping other package manager configs - not all people use lazy. However it would be a bit too long for readme so I think it would be better to put into wiki on github

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Issues related to feature proposals. Please attach a module.
Projects
None yet
Development

No branches or pull requests

3 participants