---
name: d2
description: Make box-and-arrow diagrams as images with the D2 CLI, including Oxide-styled ones for slides, docs, or notes. Use when the user asks for a diagram as a PNG/SVG, wants an existing mermaid diagram rendered nicely, or mentions d2. Covers the render-and-look loop, the Oxide style import, and layout gotchas.
title: d2
canonical_url: https://skilld.dev/gh/david-crespo/dotfiles/d2
last_updated: 2026-10-27T15:12:45.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [examples/pipeline.d2](https://skilld.dev/api/skills-raw/david-crespo/dotfiles/d2/examples/pipeline.d2).
>
> If the user asked to install this Skill, run `npx skilld install david-crespo/dotfiles/d2`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/d2`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/david-crespo/dotfiles/d2) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/d2`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/d2 --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/d2 --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# D2 diagrams

`d2` is installed (`brew install d2`). It renders `.d2` source to SVG or PNG
with dagre or ELK layout built in. No browser needed, renders in tens of ms.

## Loop

1. Write the source to the scratchpad (or wherever the user wants it kept).
2. `d2 file.d2 file.png`
3. Look at the PNG with the Read tool. Fix. Repeat.

For the user to iterate alongside: `d2 --watch file.d2 file.svg` serves a
live-reloading preview in the browser.

## Oxide style

Import the shared style at the top of the file:

```d2
...@/Users/david/repos/dotfiles/d2/oxide.d2

direction: right
a: Some artifact { class: thing }
b: Another { class: thing }
g: A group {
  class: group
  c: Inside { class: thing }
}
a -> b { class: edge }
b -> g.c: uses { class: dashed }
```

Classes: `thing` (green box, the nouns), `group` (gray box, for containers
and verb labels), `edge` (solid), `dashed` (secondary relationship, takes a
label). The import sets ELK layout, padding, and the background. Colors are
design-system dark-theme tokens; the file comments name each one. Font is
D2's default; changing it needs TTF files via `--font-*` flags, not worth it.

See `examples/pipeline.d2` for a full diagram using a container, multi-line
labels, and edge ordering.

## Layout control (ELK)

- **Vertical order within a column** follows node declaration order. To move
  a node to the bottom, declare it last.
- **Port order on a fan-out node** follows edge declaration order. If two
  edges cross on the way to their targets, reorder the edge lines.
- **Nested `direction`** inside a container is ignored under ELK.
- **Container padding** is fixed and generous. A grid container (`grid-rows:
  2`, `grid-gap: 12`) packs children tightly, but only works when edges
  connect to the container itself, not to its children: edges into grid
  children route badly and can drag the container out of its column. For two
  small things that travel together, a single box with a two-line label
  (`"TypeScript client\n+ mock API server"`) usually reads better than any container.
  A container with transparent fill and stroke looks broken; avoid it.
- **Tall fan-out nodes**: a node with many outgoing edges grows to fit its
  ports. Pin it with `height: 70` (or whatever matches the other boxes).
- Invisible edges are not a tool here. There is no `~~~`. Use ordering.
- **Edges that flow against `direction` get routed around the outside**,
  even when a short straight path exists: ELK treats them as feedback
  edges. Declare every edge in the layout direction and flip the arrowhead
  with `<-` where the flow runs back: `server.thread <- you: comment` lays
  out like `server.thread -> you` but points at `server.thread`. A diagram
  of a round trip (request out, reply back) needs this on every return edge.
- **Kinks on edges into a column of nodes**: when the box at the other end
  is much shorter than the column, its ports bunch up and each edge jogs.
  Set that box's `height` to about the column's so its ports sit level with
  their targets. Try two or three heights; the straight one is obvious.
- **ELK's crossing minimization can override declaration order** for a node
  with edges on both sides. When reordering doesn't move it, merging or
  dropping a node usually helps more.

## Syntax gotchas

- Maps must be multi-line or use `;` separators. `vars: { d2-config: {
  layout-engine: elk, theme-id: 200 } }` fails with a confusing "layout not
  found" error. Write it over several lines.
- Newlines in labels: `impl: "API implementation\n(Rust)"`.
- Edge attributes go in braces after the optional label: `a -> b: label {
  class: dashed }`.
- Source arrowheads (`source-arrowhead`) only draw on bidirectional edges
  (`a <-> b`). On `a -> b` they are silently dropped. Arrowhead size cannot be
  changed.
- Import paths (`...@path`) are cut at the first `.`, so a path through
  `.config` or `.claude` fails with "failed to import /path/up/to/dot.d2".
  That is why the style lives at `~/repos/dotfiles/d2/oxide.d2`.
- Top-level background: `style.fill: "#..."` at file root. `theme-id` alone
  does not set it.

## When not to use D2

If the diagram needs to live as text inside an RFD, GitHub markdown, or an
Obsidian note, write mermaid instead: those render mermaid natively and D2
only as an image. Mermaid can be rendered to PNG with `mmdc` (mermaid-cli,
installed) but needs a theme JSON and a CSS file to look like anything.
