Writing a plugin
This guide is written around the kanban plugin, because a board is the smallest feature that needs every part of the system at once: models and migrations, a service with real logic, routes, React Query hooks, two admin pages, a nav entry, an npm dependency, and injections into six existing files.
What a plugin actually is
A Grit plugin is not loaded at runtime. It is a code generator with a receipt. Install writes files, patches existing files at named markers, and records every one of those actions in .grit/plugins.lock.json. Removal reads that record and replays it backwards.
That one design decision shapes everything else here. Grit previously had a feature that generated code one way and removed it via a separately maintained list. The two drifted, and removal left behind handlers and switch arms that no longer compiled. A plugin system multiplies that risk by every plugin anyone writes, so removal is derived from what install did, never from a second list an author has to remember to update.
The anatomy
A plugin is one value. Here is kanban's, trimmed:
func kanbanPlugin() Plugin {return Plugin{Name: "kanban",Version: "1.0.0",Summary: "Boards, columns and cards with drag-and-drop ordering",// Refuses before the first write if the project cannot host it.Preflight: kanbanPreflight,// Fetched before any file is written, so a network failure aborts// while the project is still untouched.NodeDeps: []Dependency{{Name: "@dnd-kit/core", Version: "6.3.1", Workspace: "apps/admin"},{Name: "@dnd-kit/sortable", Version: "10.0.0", Workspace: "apps/admin"},},// Functions, not data: a triple project needs admin pages an --api// project does not.Files: kanbanFiles,Injections: kanbanInjections,NextSteps: []string{"Install the new admin dependency: pnpm install","Run migrations: cd apps/api && go run cmd/migrate/main.go",},}}
Files
Files returns a map of project-relative path to content. It is a function because the right paths depend on the project: apiPath resolves apps/api or the root, adminFile resolves the Next admin or the TanStack one, and adminSource converts a component written in the Next dialect for whichever admin the project has.
func kanbanFiles(ctx Context) map[string]string {files := map[string]string{apiPath(ctx, "internal/kanban/rank.go"): kanbanRankSource,apiPath(ctx, "internal/models/board.go"): kanbanModelsGo(ctx),apiPath(ctx, "internal/services/board.go"): kanbanServiceGo(ctx),apiPath(ctx, "internal/handlers/board.go"): kanbanHandlerGo(ctx),apiPath(ctx, "internal/handlers/board_test.go"): kanbanHandlerTestGo(ctx),}if !hasAdmin(ctx) {return files}files[adminFile(ctx, "hooks/use-boards.ts")] = adminSource(ctx, kanbanHook())files[adminFile(ctx, "components/kanban/board-view.tsx")] = adminSource(ctx, kanbanBoardView())// A Next page is its own route; TanStack needs a separate shim.list, listRoute := adminPageFile(ctx, "boards", "boards")files[list] = adminSource(ctx, kanbanBoardsPage())if listRoute != "" {files[listRoute] = tanStackRouteShim("boards", "@/pages/boards")}return files}
Embed code you want tested
kanbanRankSource is not a Go string. It is //go:embed kanban/rank.go, so the ordering algorithm the project receives is byte for byte the code Grit's own test suite runs on every CI run. A plugin that keeps its algorithm in a string literal has no tests for what it ships: the string compiles nowhere until somebody installs it.
Injections
An injection inserts a snippet on the line before a marker comment. The exact text is recorded in the lockfile, so removal takes out precisely what was put in.
{File: apiPath(ctx, "internal/models/user.go"),Marker: "// grit:models",Code: "\t\t&Board{},\n\t\t&BoardColumn{},\n\t\t&BoardCard{},",},{File: apiPath(ctx, "internal/routes/routes.go"),Marker: "// grit:handlers",Code: "\tboardHandler := handlers.NewBoardHandler(db)",},
The markers available in a scaffolded project:
| Marker | In | For |
|---|---|---|
| // grit:models | models/user.go | Register a model for AutoMigrate |
| // grit:handlers | routes/routes.go | Construct a handler |
| // grit:routes:protected | routes/routes.go | Routes behind auth |
| // grit:routes:custom | routes/routes.go | Routes with no auth middleware, for genuinely anonymous endpoints |
| // grit:icons:import | admin lib/icons.ts | Import a lucide icon |
| // grit:icons:map | admin lib/icons.ts | Make it reachable by name. Both are needed: getIcon falls back to a document icon and nothing reports the miss |
| // grit:nav:content | admin CollapsibleSidebar.tsx | A working surface, in the rail beside the resources |
| // grit:nav:system | admin CollapsibleSidebar.tsx | An operational surface, in the System Hub's Extensions tab |
Those last two are worth getting right, and kanban got it wrong first. A board filed under System, Extensions is three clicks from the dashboard and nobody looks for their board there. grit:nav:content exists because building this plugin found that there was no anchor for a surface people open while working rather than while configuring.
Preflight
Preflight states a requirement the plugin cannot inject its way out of. It runs before anything is written, so a project that is not ready is told what to do rather than left half-installed.
func kanbanPreflight(ctx Context) error {if !hasAdmin(ctx) {return fmt.Errorf("kanban needs an admin app, and this project is --%s\n\n"+"The board UI lives in the admin. Scaffold with --triple, or install the\n"+"API half by hand from the files this plugin would have written.", ctx.Architecture)}return nil}
Say what to do instead. "Incompatible" is not a fix; "run grit upgrade first" is.
The rules
Each of these exists because something went wrong without it.
1Never write a file you did not create
Removal deletes every file the lockfile records. If a plugin overwrites a file the project already had, removal deletes the user’s file. The installer refuses to overwrite for exactly this reason: change an existing file with an injection instead.
2Every injection needs a marker that will still be there
An injection anchors on a comment like // grit:models. If a plugin anchors on ordinary code, a reformat or a rename silently breaks the install. Anchor on markers, and if the marker you need does not exist, add it to the scaffold rather than anchoring on something else.
3Declare dependencies, do not assume them
GoDeps and NodeDeps are fetched before any file is written, so a network failure aborts before the project changes. A plugin that imports a package it did not declare installs cleanly and then fails to compile.
4Refuse early rather than install half
Preflight runs before the first write. A plugin whose UI needs an admin app should say so on a project that has none, not write the Go half and report success while the feature is invisible.
5Adapt to the architecture
A --single project has no apps/api, and a TanStack admin keeps its code under src/ with a different router. Path helpers exist for both. Plugins that wrote the Next shape unconditionally landed their files where the alias does not point, which is not a build error: the plugin reports success and the feature is simply not there.
6Pin a column name GORM would not guess
GORM derives column names by looking for known initialisms anywhere in a field name. WIPLimit contains IP, so the column is w_ip_limit. Reading the struct works, AutoMigrate is happy, and only a write by column name fails, in a handler, in production.
7Test that it removes, not just that it installs
Installing is the easy half, and the half everybody tests. Removal replays the lockfile backwards, so it only works if every change the plugin made is one the lockfile recorded. A plugin that edits a file outside the injection mechanism installs fine and leaves wreckage behind.
Writing one outside the Grit repo
The plugins above are Go values compiled into the CLI. You do not need to fork Grit to write one: the same shape is expressible as a directory with a JSON manifest, and install and removal behave identically.
Start from a working one rather than an empty directory:
grit plugin new audit-trail
That writes a plugin that installs as it stands, which is the point: get the loop working before the plugin does anything interesting. It exercises one file written always, one gated by a when clause, a Go test that runs in the installing project, and two injections, because a route needs its import and neither compiles alone.
audit-trail/plugin.json the manifestfiles/api/audittrail.go the endpoint, installed alwaysfiles/api/audittrail_test.go its test, which runs in the projectfiles/admin/audit-trail-card.tsx installed only in a tripleREADME.md the manifest, the markers, what not to do.github/workflows/plugin.yml the install and remove loop, in CI
The skeleton is generated from templates inside the CLI rather than copied from a template repository, so it cannot fall behind the manifest schema: the CLI's own tests load it with the same function grit plugin add ./dir calls, and check the markers it injects at against the routes.go the scaffold actually writes. A marker typo is the one mistake here with no error message, because an injection at a marker that is not in the file is not refused, it simply does not happen.
my-plugin/plugin.json the manifestfiles/ file bodies, referenced by "from"
{"name": "calendar","version": "1.0.0","summary": "A month view for any resource with a date field","nodeDeps": [{ "name": "date-fns", "version": "4.1.0", "workspace": "apps/admin" }],"files": [{ "from": "files/calendar.tsx", "to": "apps/admin/components/calendar.tsx" }],"injections": [{"file": "apps/admin/components/chrome/CollapsibleSidebar.tsx","marker": "// grit:nav:content","code": " { href: \"/calendar\", label: \"Calendar\", iconKey: \"Calendar\", adminOnly: false },"}],"nextSteps": ["pnpm install"]}
File bodies get {{MODULE}} and {{PROJECT}} substituted, so a plugin does not need to know the module path of the project installing it. Install it from a path or straight from a repository:
grit plugin add ./my-plugingrit plugin add github.com/you/grit-calendar
Testing it
On a freshly scaffolded project, in this order:
grit new testbed --triple --nextcd testbedgrit plugin add ./my-plugin # installs without errorscd apps/api && go build ./... # still compilesgo test ./internal/... # the tests it shipped passcd ../.. && pnpm install && cd apps/admin && npx tsc --noEmitcd ../.. && grit plugin remove calendar --forcecd apps/api && go build ./... # STILL compiles
That last build is the one people skip, and it is the one that matters. Installing is the easy half. If removal leaves a project that does not compile, the plugin changed something the lockfile did not record.
Publishing it
Put it in a public repository with the manifest at the root or in plugin/, then run grit plugin submit for the checklist and the submission link. Accepted plugins appear in grit plugin list and on the marketplace.
