From 690469bd99feb3e5de8517bc64014343b28c5e24 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Wed, 4 Mar 2026 19:22:57 -0500 Subject: [PATCH] fix(utils): discover nix submitEnv dynamically in dev checkout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: In a dev checkout, `_nix_submit_cmd` is nil (only baked in by the nix derivation). The uv fallback fails with code 127 on NixOS because `uv` is not a bare system binary — it's only available via the FHS-wrapped `cp-nvim-submit` script produced by `mkSubmitEnv`. Solution: Add `discover_nix_submit_cmd` mirroring `discover_nix_python`: runs `nix build #submitEnv --no-link --print-out-paths`, caches the result in `stdpath('cache')/cp-nvim/nix-submit`, and sets `_nix_submit_cmd`. `run_scraper` calls `setup_nix_submit_env()` before spawning submit. --- lua/cp/scraper.lua | 4 +++ lua/cp/utils.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lua/cp/scraper.lua b/lua/cp/scraper.lua index ecd3fa0..7ddca0a 100644 --- a/lua/cp/scraper.lua +++ b/lua/cp/scraper.lua @@ -44,6 +44,10 @@ local function run_scraper(platform, subcommand, args, opts) return { success = false, error = msg } end + if subcommand == 'submit' then + utils.setup_nix_submit_env() + end + local plugin_path = utils.get_plugin_path() local cmd if subcommand == 'submit' then diff --git a/lua/cp/utils.lua b/lua/cp/utils.lua index 578ca6c..ac55dbb 100644 --- a/lua/cp/utils.lua +++ b/lua/cp/utils.lua @@ -123,6 +123,71 @@ function M.get_python_submit_cmd(module, plugin_path) end local python_env_setup = false +local _nix_submit_attempted = false + +---@return boolean +local function discover_nix_submit_cmd() + local cache_dir = vim.fn.stdpath('cache') .. '/cp-nvim' + local cache_file = cache_dir .. '/nix-submit' + + local f = io.open(cache_file, 'r') + if f then + local cached = f:read('*l') + f:close() + if cached and vim.fn.executable(cached) == 1 then + _nix_submit_cmd = cached + return true + end + end + + local plugin_path = M.get_plugin_path() + vim.notify('[cp.nvim] Building submit environment with nix...', vim.log.levels.INFO) + vim.cmd.redraw() + local result = vim + .system( + { 'nix', 'build', plugin_path .. '#submitEnv', '--no-link', '--print-out-paths' }, + { text = true } + ) + :wait() + + if result.code ~= 0 then + logger.log('nix build #submitEnv failed: ' .. (result.stderr or ''), vim.log.levels.WARN) + return false + end + + local store_path = result.stdout:gsub('%s+$', '') + local submit_cmd = store_path .. '/bin/cp-nvim-submit' + + if vim.fn.executable(submit_cmd) ~= 1 then + logger.log('nix submit cmd not executable at ' .. submit_cmd, vim.log.levels.WARN) + return false + end + + vim.fn.mkdir(cache_dir, 'p') + f = io.open(cache_file, 'w') + if f then + f:write(submit_cmd) + f:close() + end + + _nix_submit_cmd = submit_cmd + return true +end + +---@return boolean +function M.setup_nix_submit_env() + if _nix_submit_cmd then + return true + end + if _nix_submit_attempted then + return false + end + _nix_submit_attempted = true + if vim.fn.executable('nix') == 1 then + return discover_nix_submit_cmd() + end + return false +end ---@return boolean local function discover_nix_python()