From 9bc8b0f423b999d0da9641cb0ce03c26686b7137 Mon Sep 17 00:00:00 2001
From: Eduardo Trujillo <ed@chromabits.com>
Date: Mon, 20 Mar 2023 19:27:30 -0700
Subject: [PATCH] feat: Switch to lazy.nvim

---
 home-manager/default.nix   |   8 ++
 home-manager/lazy-nvim.nix |  14 ++++
 src/lazy-lock.initial.json |  30 ++++++++
 src/lua/config/lualine.lua |   3 +-
 src/lua/plugins.lua        | 151 ++++++++++++++++++++-----------------
 5 files changed, 137 insertions(+), 69 deletions(-)
 create mode 100644 home-manager/lazy-nvim.nix
 create mode 100644 src/lazy-lock.initial.json

diff --git a/home-manager/default.nix b/home-manager/default.nix
index 98159f5..96d42dc 100644
--- a/home-manager/default.nix
+++ b/home-manager/default.nix
@@ -3,6 +3,10 @@
   lib,
   ...
 }: {
+  imports = [
+    ./lazy-nvim.nix
+  ];
+
   config = {
     programs.neovim = {
       enable = true;
@@ -14,6 +18,10 @@
       source = ../src/init.lua;
       target = ".config/nvim/init.lua";
     };
+    home.file.".config/nvim/lazy-lock.initial.json" = {
+      source = ../src/lazy-lock.initial.json;
+      target = ".config/nvim/lazy-lock.initial.json";
+    };
     home.file.".config/nvim/after" = {
       source = ../src/after;
       target = ".config/nvim/after";
diff --git a/home-manager/lazy-nvim.nix b/home-manager/lazy-nvim.nix
new file mode 100644
index 0000000..b7aa33a
--- /dev/null
+++ b/home-manager/lazy-nvim.nix
@@ -0,0 +1,14 @@
+{pkgs, ...}: {
+  config = {
+    home.file.".local/share/nvim/lazy/lazy.nvim" = {
+      source = pkgs.fetchFromGitHub {
+        owner = "folke";
+        repo = "lazy.nvim";
+        rev = "690f9e88e2a7dc92bcb0cca85f778a3e99fe1f7e";
+        sha256 = "Sg5dVBtU+o80qzNdhmPCN7viVfEcH5jCYCCTEKzdfAg=";
+      };
+      target = ".local/share/nvim/lazy/lazy.nvim";
+    };
+  };
+}
+
diff --git a/src/lazy-lock.initial.json b/src/lazy-lock.initial.json
new file mode 100644
index 0000000..9967393
--- /dev/null
+++ b/src/lazy-lock.initial.json
@@ -0,0 +1,30 @@
+{
+  "LuaSnip": { "branch": "master", "commit": "a835e3d680c5940b61780c6af07885db95382478" },
+  "barbar.nvim": { "branch": "master", "commit": "1c9d324c493650667ff621c835d552e56fd229ca" },
+  "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" },
+  "cmp-cmdline": { "branch": "main", "commit": "8fcc934a52af96120fe26358985c10c035984b53" },
+  "cmp-nvim-lsp": { "branch": "main", "commit": "0e6b2ed705ddcff9738ec4ea838141654f12eeef" },
+  "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
+  "cmp_luasnip": { "branch": "master", "commit": "18095520391186d634a0045dacaa346291096566" },
+  "gitsigns.nvim": { "branch": "main", "commit": "b1f9cf7c5c5639c006c937fc1819e09f358210fc" },
+  "indent-blankline.nvim": { "branch": "master", "commit": "018bd04d80c9a73d399c1061fa0c3b14a7614399" },
+  "lsp-status.nvim": { "branch": "master", "commit": "54f48eb5017632d81d0fd40112065f1d062d0629" },
+  "lualine.nvim": { "branch": "master", "commit": "e99d733e0213ceb8f548ae6551b04ae32e590c80" },
+  "null-ls.nvim": { "branch": "main", "commit": "13dd1fc13063681ca7e039436c88f6eca7e3e937" },
+  "nvim-cmp": { "branch": "main", "commit": "777450fd0ae289463a14481673e26246b5e38bf2" },
+  "nvim-dap": { "branch": "master", "commit": "7e81998e31277c7a33b6c34423640900c5c2c776" },
+  "nvim-dap-ui": { "branch": "master", "commit": "bdb94e3853d11b5ce98ec182e5a3719d5c0ef6fd" },
+  "nvim-lspconfig": { "branch": "master", "commit": "0f94c5fded29c0024254259f3d8a0284bfb507ea" },
+  "nvim-scrollbar": { "branch": "main", "commit": "f85b29805cf917f9b1d5ff0c9a52c5b1bdca5943" },
+  "nvim-tree.lua": { "branch": "master", "commit": "a50723e35f57f89fb67019127a16d90f16edfef8" },
+  "nvim-treesitter": { "branch": "master", "commit": "379305aa7f55e810950cde24a82789681478df04" },
+  "nvim-web-devicons": { "branch": "master", "commit": "074e13f21f52bb45d654069bdadbcb6d7c2b65d0" },
+  "one-small-step-for-vimkind": { "branch": "main", "commit": "aef1bdbb8347e6daaf33d5109002f3df243ebfe9" },
+  "plenary.nvim": { "branch": "master", "commit": "253d34830709d690f013daf2853a9d21ad7accab" },
+  "prettier.nvim": { "branch": "main", "commit": "08f7f08fae57fd970c7470d883c9127f172bd9e6" },
+  "telescope.nvim": { "branch": "master", "commit": "b79cd6c88b3d96b0f49cb7d240807cd59b610cd8" },
+  "toggleterm.nvim": { "branch": "main", "commit": "9a595ba699837c4333c4296634feed320f084df2" },
+  "tokyonight.nvim": { "branch": "main", "commit": "edffa82026914be54c8220973b0385f61d3392f0" },
+  "trouble.nvim": { "branch": "main", "commit": "7915277a259fdff5d46c6f1d2e100df2ec384d3b" }
+}
+
diff --git a/src/lua/config/lualine.lua b/src/lua/config/lualine.lua
index 21daf8a..4af74ab 100644
--- a/src/lua/config/lualine.lua
+++ b/src/lua/config/lualine.lua
@@ -2,8 +2,9 @@ return function()
   local lualine = require('lualine');
 
   lualine.setup {
+    extensions = {'nvim-tree'},
     options = {
-      globalstatus = true,
+      globalstatus = false,
       theme = 'tokyonight',
     },
     sections = {
diff --git a/src/lua/plugins.lua b/src/lua/plugins.lua
index 1001dcd..5679d0c 100644
--- a/src/lua/plugins.lua
+++ b/src/lua/plugins.lua
@@ -1,63 +1,83 @@
-local ensure_packer = function()
-  local fn = vim.fn
-  local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
-
-  if fn.empty(fn.glob(install_path)) > 0 then
-    fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
-    vim.cmd [[packadd packer.nvim]]
-    return true
-  end
-
-  return false
+-- If a lazy-lock.json file does not exist, but we have a
+-- lazy-lock.initial.json file available, create a writeable copy.
+--
+-- This allow us to ship a lockfile with this repo, managed by home-manager,
+-- ensuring the environment is reproducible while also giving the end-user the
+-- option to update plugins manually.
+--
+-- If something breaks during na update, the user can always rever tback to
+-- the state in this repo by copying the initial lockfile again.
+local lockpath = vim.fn.stdpath("config") .. "/lazy-lock.json"
+local initial_lockpath = vim.fn.stdpath("config") .. "/lazy-lock.initial.json"
+if (not vim.loop.fs_stat(lockpath)) and vim.loop.fs_stat(initial_lockpath) then
+  vim.loop.fs_copyfile(initial_lockpath, lockpath)
+  vim.loop.fs_chmod(lockpath, tonumber("664", 8))
 end
 
-local packer_bootstrap = ensure_packer()
+-- Install Lazy.nvim if it's not already available.
+--
+-- This should allow for this config to be usable even outside of a
+-- home-manager/Nix environment.
+local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
+if not vim.loop.fs_stat(lazypath) then
+  vim.fn.system({
+    "git",
+    "clone",
+    "--filter=blob:none",
+    "https://github.com/folke/lazy.nvim.git",
+    "--branch=stable", -- latest stable release
+    lazypath,
+  })
+end
+vim.opt.rtp:prepend(lazypath)
 
-return require('packer').startup(function()
-  use 'wbthomason/packer.nvim'
-  use {
+require('lazy').setup({
+  -- Prevent lazy.nvim from updating itself. The plugin will be managed
+  -- externally to lock down the version used.
+  { "folke/lazy.nvim", enabled = false },
+  {
     'folke/tokyonight.nvim',
     config = require('config.tokyonight'),
-  }
-  use {
+  },
+  {
     'nvim-treesitter/nvim-treesitter',
     config = require('config.treesitter'),
-    run = function()
+    build = function()
       local ts_update = require('nvim-treesitter.install')
         .update({ with_sync = true })
 
       ts_update()
     end,
-  }
-  use {
+  },
+  {
     'nvim-lualine/lualine.nvim',
-    requires = { 'nvim-tree/nvim-web-devicons' },
+    dependencies = { 'nvim-tree/nvim-web-devicons', 'nvim-tree/nvim-tree.lua' },
     config = require('config.lualine'),
-  }
-  use {
+  },
+  {
     'nvim-tree/nvim-tree.lua',
-    requires = { 'nvim-tree/nvim-web-devicons' },
+    dependencies = { 'nvim-tree/nvim-web-devicons' },
     config = require('config.tree')
-  }
-  use {
+  },
+  {
     'lewis6991/gitsigns.nvim',
-    requires = {
+    dependencies = {
       'nvim-lua/plenary.nvim'
     },
     config = require('config.gitsigns')
-  }
-  use {
+  },
+  {
     'romgrk/barbar.nvim',
-    requires = { 'nvim-tree/nvim-web-devicons' },
+    dependencies = { 'nvim-tree/nvim-web-devicons' },
     config = require('config.barbar')
-  }
-  use {
+  },
+  {
     "lukas-reineke/indent-blankline.nvim",
     config = require('config.indent-blankline')
-  }
-  use {
+  },
+  {
     'hrsh7th/nvim-cmp',
-    requires = {
+    dependencies = {
       'hrsh7th/cmp-nvim-lsp',
       'hrsh7th/cmp-buffer',
       'hrsh7th/cmp-path',
@@ -66,61 +86,56 @@ return require('packer').startup(function()
       'L3MON4D3/LuaSnip',
     },
     config = require('config.cmp'),
-  }
-  use {
+  },
+  {
     'nvim-lua/lsp-status.nvim',
     config = require('config.lspstatus')
-  }
-  use {
+  },
+  {
     'neovim/nvim-lspconfig',
     config = require('config.lspconfig'),
     after = 'lsp-status.nvim'
-  }
-  use {
+  },
+  {
     'nvim-telescope/telescope.nvim', tag = '0.1.0',
-    requires = { { 'nvim-lua/plenary.nvim' } },
+    dependencies = { { 'nvim-lua/plenary.nvim' } },
     config = require('config.telescope'),
-  }
-  use {
+  },
+  {
     'mfussenegger/nvim-dap',
     config = require('config.dap')
-  }
-  use {
+  },
+  {
     'rcarriga/nvim-dap-ui',
+    dependencies = {'nvim-dap'},
     config = require('config.dapui'),
-    after = 'nvim-dap'
-  }
-  use 'jbyuki/one-small-step-for-vimkind'
-  use {
+  },
+  'jbyuki/one-small-step-for-vimkind',
+  {
     'petertriho/nvim-scrollbar',
     config = require('config.scrollbar'),
-  }
-  use {
+  },
+  {
     'akinsho/toggleterm.nvim',
     config = require('config.toggleterm'),
-  }
-  use {
+  },
+  {
     'folke/trouble.nvim',
-    requires = { 'nvim-tree/nvim-web-devicons' },
+    dependencies = { 'nvim-tree/nvim-web-devicons' },
     config = require('config.trouble'),
-  }
-  use {
+  },
+  {
     'jose-elias-alvarez/null-ls.nvim',
-    requires = { 'nvim-lua/plenary.nvim' },
+    dependencies = { 'nvim-lua/plenary.nvim' },
     after = 'nvim-lspconfig',
     config = require('config.null-ls'),
-  }
-  use {
+  },
+  {
     'MunifTanjim/prettier.nvim',
-    requires = {
+    dependencies = {
       'neovim/nvim-lspconfig',
       'jose-elias-alvarez/null-ls.nvim',
     },
-    after = 'null-ls.nvim',
     config = require('config.prettier'),
-  }
-
-  if packer_bootstrap then
-    require('packer').sync()
-  end
-end)
+  },
+})
-- 
GitLab