gists/config/nvim-local-config-docker.md
2025-04-14 13:58:58 +00:00

5.2 KiB

Neovim has the exrc option, which enables project-local configuration. This option be enabled in the neovim config:

vim.opt.exrc = true

Upon starting neovim in a directory, any code in ./nvim.lua, will be aplied as a local configuration after the global one. As an example, consider the following configuration, which

  • modifies the LSP settings for clangd to run in a docker container
  • adds overseer templates to generate, build and flash a project using docker
-- The user and group ids for the docker container can be found using `id -u` and `stat -c %g /dev/ttyACM0`
--
--
-- Set up LSP to run in docker container
--
--
local lspconfig = require('lspconfig')

for _, client in pairs(vim.lsp.get_clients({
    name = "clangd"
})) do vim.lsp.stop_client(client.id) end

lspconfig.clangd.setup({
    cmd = {
        "docker",
        "run",
        "--rm",
        "-v",
        "/home/andreas/workspace/work/kinemic/toolassist-fw-new:/home/andreas/workspace/work/kinemic/toolassist-fw-new",
        "-i",
        "-u",
        "1000:1001",
        "toolassist",
        "clangd-18",
        "--background-index",
        "--clang-tidy",
        "--completion-style=bundled",
        "--cross-file-rename",
        "--header-insertion=iwyu",
        "--query-driver=/**/*g++",
        "--offset-encoding=utf-16"
    }
})

--
--
-- Set up Overseer actions to run in docker containers
--
--

local overseer = require("overseer")

overseer.register_template({
    name = "Flash (Docker)",
    builder = function()
        local file = vim.fn.expand("%:p")
        return {
            cmd = {
                "docker"
            },
            args = {
                "run",
                "--rm",
                "-v",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new:/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-w",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-i",
                "-u",
                "1000:1001",
                "--device=/dev/ttyACM0",
                -- "--device=/dev/ttyUSB0",
                "toolassist",
                "idf.py",
                "flash"
            },
            components = {
                {
                    "on_output_quickfix"
                },
                "default"
            }
        }
    end,
    condition = {
        filetype = {
            "cmake",
            "cpp"
        }
    },
    priority = 1
})

overseer.register_template({
    name = "CMake Generate (Docker)",
    params = {
        build_type = {
            type = "enum",
            choices = {
                "Debug",
                "Release",
                "RelWithDebInfo",
                "MinSizeRel"
            },
            default = "Release"
        },
        generator = {
            type = "enum",
            choices = {
                "Ninja",
                "Unix Makefiles"
            },
            default = "Ninja"
        },
        compiler = {
            type = "enum",
            choices = {
                "g++",
                "clang++"
            },
            default = "g++"
        }
    },
    builder = function(params)
        return {
            cmd = {
                "docker"
            },
            args = {
                "run",
                "--rm",
                "-v",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new:/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-w",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-i",
                "-u",
                "1000:1001",
                "toolassist",
                "cmake",
                "-B",
                "build",
                "-S",
                ".",
                "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
                "-DCMAKE_BUILD_TYPE=" .. params.build_type,
                "-DCMAKE_CXX_COMPILER=" .. params.compiler,
                "-G",
                params.generator
            },
            components = {
                "default"
            }
        }
    end,
    condition = {
        filetype = {
            "cmake",
            "cpp"
        }
    },
    priority = 1
})

overseer.register_template({
    name = "CMake Build (Docker)",
    builder = function()
        local file = vim.fn.expand("%:p")
        return {
            cmd = {
                "docker"
            },
            args = {
                "run",
                "--rm",
                "-v",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new:/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-w",
                "/home/andreas/workspace/work/kinemic/toolassist-fw-new",
                "-i",
                "-u",
                "1000:1001",
                "toolassist",
                "cmake",
                "--build",
                "build",
                "-j16"
            },
            components = {
                {
                    "on_output_quickfix"
                },
                "default"
            }
        }
    end,
    condition = {
        filetype = {
            "cmake",
            "cpp"
        }
    },
    priority = 1
})