From d23c7f6f51b5730f94628dab6ce43f76fb6048f2 Mon Sep 17 00:00:00 2001 From: Daniel Winkler Date: Thu, 21 May 2026 20:04:45 +1000 Subject: [PATCH] load packages only if installed --- flake.nix | 51 +++++++++++++++++++----------------- ftplugin/markdown.lua | 55 +++++++++++++++++++++------------------ plugin/20_startup.lua | 36 +++++++++++++------------ plugin/21_datascience.lua | 32 ++++++++++++----------- 4 files changed, 92 insertions(+), 82 deletions(-) diff --git a/flake.nix b/flake.nix index f84715f..2780328 100644 --- a/flake.nix +++ b/flake.nix @@ -39,10 +39,8 @@ wrappers, ... } @ inputs: let - mkWrapperConfig = pkgs: let - def = pkgs.lib.mkDefault; - in { - cats = pkgs.lib.mapAttrs (_: v: def v) { + mkWrapperConfig = pkgs: { + cats = { clickhouse = false; gitPlugins = true; julia = false; @@ -55,22 +53,12 @@ }; settings = { lang_packages = { - python = def (with pkgs.python3Packages; [ - duckdb - polars - ]); - r = def ((with pkgs.rpkgs.rPackages; [ - arrow - broom - data_table - janitor - languageserver - styler - ]) ++ [ pkgs.nvimcom ]); - julia = def ["DataFramesMeta" "QuackIO"]; + python = []; + r = []; + julia = []; }; }; - binName = def "vv"; + binName = "vv"; }; wrapperSettings = pkgs: let @@ -78,7 +66,7 @@ in wrapper.config.wrap { inherit pkgs; - inherit (cfg) cats settings binName; + inherit (cfg) settings binName; }; systems = [ @@ -135,14 +123,29 @@ devShells = forAllSystems ( system: let pkgs = mkPkgs system; - cfg = mkWrapperConfig pkgs; nvimPkg = wrapperSettings pkgs; + pythonPkgs = with pkgs.python3Packages; [ + duckdb + polars + ]; + + rPkgs = (with pkgs.rpkgs.rPackages; [ + arrow + broom + data_table + janitor + languageserver + styler + ]) ++ [ pkgs.nvimcom ]; + + juliaPkgs = ["DataFramesMeta" "QuackIO"]; + pythonPackages = let python_packages_fn = if pkgs ? basePythonPackages - then ps: pkgs.basePythonPackages ps ++ cfg.settings.lang_packages.python - else _: cfg.settings.lang_packages.python; + then ps: pkgs.basePythonPackages ps ++ pythonPkgs + else _: pythonPkgs; in with pkgs; [ (python3.withPackages python_packages_fn) @@ -153,7 +156,7 @@ ]; rPackages = let - r_packages = (pkgs.baseRPackages or []) ++ cfg.settings.lang_packages.r; + r_packages = (pkgs.baseRPackages or []) ++ rPkgs; in with pkgs; [ (rWrapper.override {packages = r_packages;}) @@ -167,7 +170,7 @@ ]; juliaPackages = let - julia_with_packages = pkgs.julia-bin.withPackages cfg.settings.lang_packages.julia; + julia_with_packages = pkgs.julia-bin.withPackages juliaPkgs; in [julia_with_packages]; markdownPackages = with pkgs; [ diff --git a/ftplugin/markdown.lua b/ftplugin/markdown.lua index b673b60..db316fc 100644 --- a/ftplugin/markdown.lua +++ b/ftplugin/markdown.lua @@ -1,32 +1,35 @@ -- Add the key mappings only for Markdown files in a zk notebook. -if require("zk.util").notebook_root(vim.fn.expand('%:p')) ~= nil then - local map = vim.keymap.set - -- Open the link under the caret. - map("n", "", "lua vim.lsp.buf.definition()", { noremap = true, silent = false, buffer = true }) +local nix = require("config.nix") +if nix.get_cat("markdown", false) then + if require("zk.util").notebook_root(vim.fn.expand('%:p')) ~= nil then + local map = vim.keymap.set + -- Open the link under the caret. + map("n", "", "lua vim.lsp.buf.definition()", { noremap = true, silent = false, buffer = true }) - -- Create a new note after asking for its title. - -- This overrides the global `zn` mapping to create the note in the same directory as the current buffer. - map("n", "zhn", "ZkNew { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", - { noremap = true, silent = false, buffer = true, desc = "Note (here)" }) - -- Create a new note in the same directory as the current buffer, using the current selection for title. - map("v", "zhnt", ":'<,'>ZkNewFromTitleSelection { dir = vim.fn.expand('%:p:h') }", - { noremap = true, silent = false, buffer = true, desc = "Note from selection (title)" }) - -- Create a new note in the same directory as the current buffer, using the current selection for note content and asking for its title. - map("v", "zhnc", - ":'<,'>ZkNewFromContentSelection { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", - { noremap = true, silent = false, buffer = true, desc = "Note from selection (content)" }) + -- Create a new note after asking for its title. + -- This overrides the global `zn` mapping to create the note in the same directory as the current buffer. + map("n", "zhn", "ZkNew { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", + { noremap = true, silent = false, buffer = true, desc = "Note (here)" }) + -- Create a new note in the same directory as the current buffer, using the current selection for title. + map("v", "zhnt", ":'<,'>ZkNewFromTitleSelection { dir = vim.fn.expand('%:p:h') }", + { noremap = true, silent = false, buffer = true, desc = "Note from selection (title)" }) + -- Create a new note in the same directory as the current buffer, using the current selection for note content and asking for its title. + map("v", "zhnc", + ":'<,'>ZkNewFromContentSelection { dir = vim.fn.expand('%:p:h'), title = vim.fn.input('Title: ') }", + { noremap = true, silent = false, buffer = true, desc = "Note from selection (content)" }) - -- Open notes linking to the current buffer. - map("n", "zb", "ZkBacklinks", { noremap = true, silent = false, buffer = true, desc = "Backlinks" }) - -- Alternative for backlinks using pure LSP and showing the source context. - --map('n', 'zb', 'lua vim.lsp.buf.references()', opts) - -- Open notes linked by the current buffer. - map("n", "zL", "ZkLinks", { noremap = true, silent = false, buffer = true, desc = "Links" }) - map("n", "zi", "ZkInsertLink", { noremap = true, silent = false, buffer = true, desc = "Insert link" }) + -- Open notes linking to the current buffer. + map("n", "zb", "ZkBacklinks", { noremap = true, silent = false, buffer = true, desc = "Backlinks" }) + -- Alternative for backlinks using pure LSP and showing the source context. + --map('n', 'zb', 'lua vim.lsp.buf.references()', opts) + -- Open notes linked by the current buffer. + map("n", "zL", "ZkLinks", { noremap = true, silent = false, buffer = true, desc = "Links" }) + map("n", "zi", "ZkInsertLink", { noremap = true, silent = false, buffer = true, desc = "Insert link" }) - -- Preview a linked note. - -- Open the code actions for a visual selection. - map("v", "za", ":'<,'>lua vim.lsp.buf.range_code_action()", - { noremap = true, silent = false, buffer = true, desc = "Code actions" }) + -- Preview a linked note. + -- Open the code actions for a visual selection. + map("v", "za", ":'<,'>lua vim.lsp.buf.range_code_action()", + { noremap = true, silent = false, buffer = true, desc = "Code actions" }) + end end diff --git a/plugin/20_startup.lua b/plugin/20_startup.lua index ca18bd8..0404b2c 100644 --- a/plugin/20_startup.lua +++ b/plugin/20_startup.lua @@ -390,23 +390,25 @@ end) -- zk now_if_args(function() - require("zk").setup({ - picker = "minipick", - lsp = { - -- `config` is passed to `vim.lsp.start_client(config)` - config = { - cmd = { "zk", "lsp" }, - name = "zk", - -- on_attach = ... - -- etc, see `:h vim.lsp.start_client()` - }, + if nix.get_cat("markdown", false) then + require("zk").setup({ + picker = "minipick", + lsp = { + -- `config` is passed to `vim.lsp.start_client(config)` + config = { + cmd = { "zk", "lsp" }, + name = "zk", + -- on_attach = ... + -- etc, see `:h vim.lsp.start_client()` + }, - -- automatically attach buffers in a zk notebook that match the given filetypes - auto_attach = { - enabled = true, - filetypes = { "markdown" }, - }, + -- automatically attach buffers in a zk notebook that match the given filetypes + auto_attach = { + enabled = true, + filetypes = { "markdown" }, + }, - }, - }) + }, + }) + end end) diff --git a/plugin/21_datascience.lua b/plugin/21_datascience.lua index 62d8f49..70902ca 100644 --- a/plugin/21_datascience.lua +++ b/plugin/21_datascience.lua @@ -71,22 +71,24 @@ end) now(function() vim.treesitter.language.register("markdown", { "quarto", "rmd" }) - vim.api.nvim_create_autocmd("FileType", { - pattern = { "quarto" }, - callback = function() - require("otter").activate() - end, - }) + if nix.get_cat({"r", "markdown"}, false) then + vim.api.nvim_create_autocmd("FileType", { + pattern = { "quarto" }, + callback = function() + require("otter").activate() + end, + }) - require("otter").setup({ - lsp = { - diagnostic_update_events = { "BufWritePost", "InsertLeave" }, - }, - buffers = { - set_filetype = true, - write_to_disk = true, - }, - }) + require("otter").setup({ + lsp = { + diagnostic_update_events = { "BufWritePost", "InsertLeave" }, + }, + buffers = { + set_filetype = true, + write_to_disk = true, + }, + }) + end end) later(function()