# Neovim for kernel hacking (Arch) clangd for modules + eBPF, pyright + ruff for gdb scripts, checkpatch nagging you on save. Run everything from the repo root. ## Packages ```bash sudo pacman -S neovim clang bear git tree-sitter-cli # essential sudo pacman -S pyright ruff # Python (gdb scripts) sudo pacman -S fzf ripgrep # ff / fg pickers sudo pacman -S libbpf bpf # eBPF headers + bpftool sudo pacman -S bash-language-server ``` `clang` brings `clangd` and `clang-tidy`. ## Config ```bash ln -s "$PWD/tooling/editors/nvim" ~/.config/lkw-nvim mkdir -p ~/.config/clangd && ln -s "$PWD/tooling/editors/clangd/config.yaml" ~/.config/clangd/ alias lkw-nvim='NVIM_APPNAME=lkw-nvim nvim' # → your shell rc ``` `NVIM_APPNAME` leaves your own `~/.config/nvim` alone. Want just the LSP bits? Steal from `nvim/init.lua`, it's one file. First start clones plugins and compiles parsers, give it a minute. What's in there: - **Plugins** via built-in `vim.pack`: lspconfig, treesitter, blink.cmp, fzf-lua, gitsigns, which-key - **LSPs**, each on if its binary exists: clangd, pyright, ruff, bashls - **C files**: kernel style (8-wide tabs, column 100) - **checkpatch** on open/save for C files next to a `Kbuild` (or a Makefile with `obj-m`), via `$KDIR/scripts/checkpatch.pl`. `:Checkpatch` runs it by hand. `~/.config/clangd/config.yaml` throws out the gcc-only kernel flags clang chokes on, plus `-Werror`, turns on sane clang-tidy checks and compiles `*.bpf.c` as `--target=bpf`. ## Keys `` and wait → which-key shows everything. The ones you'll use: | | | |-|-| | `gd` `grr` `K` | definition, references, hover | | `grn` `gra` | rename, code action | | `ff` `fg` `fw` | files, grep, grep word under cursor | | `ls` `li` `lo` | workspace symbols, who calls this, what does this call | | `]d` `e` `ld` | next diagnostic, show it, list all | | `lf` `lh` | format, source ↔ header | ## bear + clangd clangd has no idea where your module's headers are until you hand it a `compile_commands.json`. bear writes one by watching your build. `$KDIR` is the lab kernel tree you build against: ```bash cd my-module/ make -C "$KDIR" M=$PWD clean # bear only records what actually compiles bear -- make -C "$KDIR" M=$PWD modules ``` Reopen the file, red squiggles become completions. Redo it when you add files or change flags. - **eBPF / userspace**: `bear -- make -B`. `*.bpf.c` works without it too, as long as there's a `vmlinux.h`: `bpftool btf dump file "$KDIR/vmlinux" format c > vmlinux.h` - **the kernel tree itself**: `make compile_commands.json` in `$KDIR`, no bear needed - **still broken?** `:checkhealth vim.lsp` or `clangd --check=file.c`