d2
Generate diagrams using D2 (Terrastruct) -- a modern, clean diagram scripting language with excellent auto-layout. Use when the diagram-router selects D2, or when the user wants clean architecture diagrams, nested containers, or prefers D2's aesthetic. Requires the d2 CLI binary.
What this skill does
# D2 Diagram Skill
Generate diagrams using D2's declarative syntax. D2 produces clean, modern-looking diagrams with excellent auto-layout, nested containers, and multiple layout engines.
**When to use D2:**
- Architecture diagrams with nested components (D2's container nesting is excellent)
- Clean, modern aesthetic without manual positioning
- User has d2 installed or prefers it
- Network/infrastructure diagrams
- Deployment diagrams with hierarchical grouping
**When another format is better:**
- Maximum visual customization (SVG)
- Diagrams in GitHub/GitLab markdown (Mermaid -- D2 doesn't render natively)
- Whiteboard/sketch aesthetic (Excalidraw)
- ER diagrams, Gantt charts (Mermaid has better support)
Consult the **design-system** skill for color, composition, and quality rules.
## Syntax Quick Reference
### Basic Connections
```d2
server -> database: query
database -> server: results
client -> server: HTTPS
```
Arrow types:
- `->` directed (default)
- `<->` bidirectional
- `--` undirected
### Shapes and Labels
```d2
# Named with label
api: API Gateway
# Shapes
db: Database {
shape: cylinder
}
user: User {
shape: person
}
decision: Route? {
shape: diamond
}
queue: Messages {
shape: queue
}
```
Available shapes: `rectangle` (default), `square`, `page`, `parallelogram`, `document`, `cylinder`, `queue`, `package`, `step`, `callout`, `stored_data`, `person`, `diamond`, `oval`, `circle`, `hexagon`, `cloud`.
### Containers (Nested)
D2's killer feature -- nested containers for hierarchy:
```d2
aws: AWS {
vpc: VPC {
public: Public Subnet {
alb: Load Balancer {
shape: hexagon
}
}
private: Private Subnet {
app: App Server
db: Database {
shape: cylinder
}
app -> db: SQL
}
public.alb -> private.app: HTTP
}
}
```
### Sequence Diagrams
```d2
shape: sequence_diagram
client: Client
server: Server
db: Database
client -> server: POST /users
server -> db: INSERT INTO users
db -> server: OK
server -> client: 201 Created
```
### SQL Tables
```d2
users: Users {
shape: sql_table
id: int {constraint: primary_key}
name: varchar
email: varchar {constraint: unique}
}
orders: Orders {
shape: sql_table
id: int {constraint: primary_key}
user_id: int {constraint: foreign_key}
total: decimal
}
users -> orders: has many
```
### Classes
```d2
classes: {
UserService: {
shape: class
+getUser(id): User
+createUser(data): User
-validateEmail(email): bool
}
}
```
## Styling
```d2
server: Server {
style: {
fill: "#EFF6FF"
stroke: "#2563EB"
border-radius: 8
font-color: "#1E293B"
shadow: true
}
}
# Connection styling
server -> db: {
style: {
stroke: "#64748B"
stroke-dash: 4
}
}
```
### Themes
D2 has built-in themes. Set at the top of the file:
```d2
vars: {
d2-config: {
theme-id: 200
}
}
```
Theme IDs: 0 (default), 1 (neutral grey), 3 (flagship terrastruct), 4 (cool classics), 100 (mixed berry blue), 200 (grape soda), 300 (aubergine). Dark themes: 200-302.
### Layout Engines
D2 supports multiple layout engines:
| Engine | Best For | Install |
|---|---|---|
| dagre (default) | General purpose, fast | Built-in |
| ELK | Complex hierarchies, wide graphs | Built-in |
| TALA | Architecture diagrams (proprietary) | Requires license |
Set via CLI flag: `d2 --layout elk diagram.d2 output.svg`
## Common Mistakes to Avoid
1. **Undeclared connections** -- reference nodes before connecting: define `a: Label` before `a -> b`
2. **Missing colons** -- labels need a colon: `server: API Server` not `server API Server`
3. **Nested path separators** -- use `.` for nested references: `aws.vpc.app` not `aws/vpc/app`
4. **Forgetting shape** -- containers don't need explicit shape; leaf nodes default to rectangle
5. **Style outside element** -- styles must be inside the element block: `server: { style: { ... } }`
6. **Too deep nesting** -- 3-4 levels max for readability
## Output Format
Save as `.d2` file. Render to SVG or PNG:
```bash
# Render to SVG
d2 diagram.d2 diagram.svg
# Render to PNG
d2 --format png diagram.d2 diagram.png
# Watch mode (live preview)
d2 --watch diagram.d2 diagram.svg
```
## Setup Detection
Before generating D2, check if the user has it installed:
```bash
command -v d2 && d2 --version
```
If not installed, suggest:
```bash
# macOS
brew install d2
# Linux (script)
curl -fsSL https://d2lang.com/install.sh | sh -s --
# Go install
go install oss.terrastruct.com/d2@latest
```
## Quality Checklist
Before presenting a D2 diagram:
- [ ] Connections reference defined nodes
- [ ] Container nesting is max 3-4 levels deep
- [ ] Labels are present on all connections that need explanation
- [ ] Shapes are semantically appropriate (cylinder for DB, person for user)
- [ ] Theme specified if not using default
- [ ] File saves with `.d2` extension
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.