d2

command module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: MPL-2.0 Imports: 2 Imported by: 0

README

Table of Contents

What does D2 look like?

vars: {
  d2-config: {
    layout-engine: elk
    # Terminal theme code
    theme-id: 300
  }
}
network: {
  cell tower: {
    satellites: {
      shape: stored_data
      style.multiple: true
    }

    transmitter

    satellites -> transmitter: send
    satellites -> transmitter: send
    satellites -> transmitter: send
  }

  online portal: {
    ui: {shape: hexagon}
  }

  data processor: {
    storage: {
      shape: cylinder
      style.multiple: true
    }
  }

  cell tower.transmitter -> data processor.storage: phone logs
}

user: {
  shape: person
  width: 130
}

user -> network.cell tower: make call
user -> network.online portal.ui: access {
  style.stroke-dash: 3
}

api server -> network.online portal.ui: display
api server -> logs: persist
logs: {shape: page; style.multiple: true}

network.data processor -> api server

D2 render example

Open in playground

For more examples, see ./docs/examples.

Quickstart

The most convenient way to use D2 is to just run it as a CLI executable to produce SVGs from .d2 files.

# First, install D2
curl -fsSL https://d2lang.com/install.sh | sh -s --

echo 'x -> y -> z' > in.d2
d2 --watch in.d2 out.svg

A browser window will open with out.svg and live-reload on changes to in.d2.

Install

The easiest way to install is with our install script:

curl -fsSL https://d2lang.com/install.sh | sh -s --

You can run the install script with --dry-run to see the commands that will be used to install without executing them.

Or if you have Go installed you can install from source though you won't get the manpage:

go install github.com/d2lang/d2@latest

You can also install a release from source which will include manpages. See ./docs/INSTALL.md#source-release.

To uninstall with the install script:

curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall

For detailed installation docs, see ./docs/INSTALL.md. We demonstrate alternative methods and examples for each OS.

As well, the functioning of the install script is described in detail to alleviate any concern of its use. We recommend using your OS's package manager directly instead for improved security but the install script is by no means insecure.

D2 as a library

In addition to being a runnable CLI tool, D2 can also be used to produce diagrams from Go programs.

For examples, see ./docs/examples/lib.

Themes

D2 includes a variety of official themes to style your diagrams beautifully right out of the box. See ./d2themes to browse the available themes and make or contribute your own creation.

Fonts

D2 ships with "Source Sans Pro" as the font in renders. If you wish to use a different one, please see ./d2renderers/d2fonts.

Export file types

D2 supports SVG, PNG, GIF, PDF, and PPTX exports.

Language tooling

D2 is designed with language tooling in mind. D2's parser can parse multiple errors from a broken program, has an autoformatter, syntax highlighting, and we have plans for LSP's and more. Good language tooling is necessary for creating and maintaining large diagrams.

The extensions for VSCode and Vim can be found in the Related section.

Plugins

D2 is designed to be extensible and composable. The plugin system allows you to change out layout engines and customize the rendering pipeline. Plugins can either be bundled with the build or separately installed as a standalone binary.

Layout engines:

  • Dagro (default, bundled): A native Go port of the Dagre directed graph layout engine that produces layered/hierarchical layouts. Based on Graphviz's DOT algorithm.
  • elk-go (bundled): A native Go port of the ELK directed graph layout engine, particularly suited for node-link diagrams with an inherent direction and ports.
  • TALA (bundled, opt-in): Native layout and edge-routing engine designed specifically for software architecture diagrams. Select it with --layout=tala, D2_LAYOUT=tala, or a layout-engine: tala setting under vars.d2-config in D2 source.

D2 intends to integrate with a variety of layout engines, e.g. dot, as well as single-purpose layout types like sequence diagrams. You can choose whichever layout engine you like and works best for the diagram you're making.

Sketch-mode rendering uses rough-go, a native Go compatibility port of the Rough.js 4.6.6 rendering surface used by D2.

LaTeX labels use mathjax-go, a native Go port of the MathJax 3.2.2 TeX-to-SVG pipeline.

Comparison

For a comparison against other popular text-to-diagram tools, see https://text-to-diagram.com.

Contributing

Contributions are welcome! See ./docs/CONTRIBUTING.md.

License

Open sourced under the Mozilla Public License 2.0. See ./LICENSE.txt.

We are constantly working on new plugins, integrations, extensions. Contributions are welcome in any official or community plugins. If you have somewhere in your workflow that you want to use D2, feel free to open a discussion. We have limited bandwidth and usually choose the most high-demand ones to work on. If you make something cool with D2 yourself, let us know and we'll be happy to include it here!

Official plugins
Community plugins
Misc

FAQ

  • Does D2 collect telemetry?
    • No, D2 does not use an internet connection after installation, except to check for version updates from Github periodically.
  • Does D2 need a browser to run?
    • No, D2 can run entirely server-side.
  • What's coming in the next release?
  • I have a question or need help.
    • The best way to get help is to ask on D2 Discord
  • I have a feature request, proposal, or bug report.
    • Please open up a Github Issue.
  • I have a private inquiry.

Notable open-source projects documenting with D2

Do you have or see an open-source project with .d2 files? Please submit a PR adding to this selected list of featured projects using D2.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
ci
tala-performance command
Command tala-performance measures TALA callbacks on an external D2 corpus and fingerprints complete layout outputs.
Command tala-performance measures TALA callbacks on an external D2 corpus and fingerprints complete layout outputs.
cmd
d2plugin-dagre command
TODO: Remove boxes and cleanup like d2ir
TODO: Remove boxes and cleanup like d2ir
Package d2compiler implements a parser, compiler and autoformatter for the Terrastruct d2 diagramming language.
Package d2compiler implements a parser, compiler and autoformatter for the Terrastruct d2 diagramming language.
Package d2ir implements a tree data structure to keep track of the resolved value of D2 keys.
Package d2ir implements a tree data structure to keep track of the resolved value of D2 keys.
d2elklayout
Package d2elklayout adapts D2 graphs to the native Go port of ELK.
Package d2elklayout adapts D2 graphs to the native Go port of ELK.
d2layoutfeatures
Package d2layoutfeatures contains legacy string names for layout plugin capabilities.
Package d2layoutfeatures contains legacy string names for layout plugin capabilities.
d2near
d2near applies near keywords when they're constants Intended to be run as the last stage of layout after the diagram has already undergone layout
d2near applies near keywords when they're constants Intended to be run as the last stage of layout after the diagram has already undergone layout
d2talalayout
Package d2talalayout adapts D2 graphs to TALA's layout and edge-routing engine.
Package d2talalayout adapts D2 graphs to TALA's layout and edge-routing engine.
d2talalayout/internal/engine
Package engine composes TALA's layout stages into one deterministic layout.
Package engine composes TALA's layout stages into one deterministic layout.
d2talalayout/internal/graphbounds
Package graphbounds computes work-accounted bounds for mutable TALA nodes.
Package graphbounds computes work-accounted bounds for mutable TALA nodes.
d2talalayout/internal/graphjson
Package graphjson serializes and reconstructs TALA layout graphs.
Package graphjson serializes and reconstructs TALA layout graphs.
d2talalayout/internal/grouping
Package grouping discovers and manages layout groups that temporarily move as a single vessel.
Package grouping discovers and manages layout groups that temporarily move as a single vessel.
d2talalayout/internal/hierarchy
Package hierarchy discovers DAG-like subgraphs, ranks their nodes, and places them in layers.
Package hierarchy discovers DAG-like subgraphs, ranks their nodes, and places them in layers.
d2talalayout/internal/invariant
Package invariant classifies violations of TALA's internal layout invariants.
Package invariant classifies violations of TALA's internal layout invariants.
d2talalayout/internal/labelgeom
Package labelgeom contains renderer-compatible label geometry that is shared by the mutable graph bounds and label-placement domains.
Package labelgeom contains renderer-compatible label geometry that is shared by the mutable graph bounds and label-placement domains.
d2talalayout/internal/labeling
Package labeling places node, icon, edge, and arrowhead labels on a routed layout graph.
Package labeling places node, icon, edge, and arrowhead labels on a routed layout graph.
d2talalayout/internal/layoutgraph
Package layoutgraph defines the mutable state used by TALA's layout algorithms.
Package layoutgraph defines the mutable state used by TALA's layout algorithms.
d2talalayout/internal/limits
Package limits provides the engine's topology ceilings and reusable work accounting primitives.
Package limits provides the engine's topology ceilings and reusable work accounting primitives.
d2talalayout/internal/loops
Package loops computes deterministic self-edge routes and the node extents those routes reserve during placement.
Package loops computes deterministic self-edge routes and the node extents those routes reserve during placement.
d2talalayout/internal/nodeshape
Package nodeshape defines the shape-specific port and label-placement policy used by TALA nodes.
Package nodeshape defines the shape-specific port and label-placement policy used by TALA nodes.
d2talalayout/internal/packing
Package packing arranges and compacts disconnected graph components without changing their internal layout or routed edge geometry.
Package packing arranges and compacts disconnected graph components without changing their internal layout or routed edge geometry.
d2talalayout/internal/placement
Package placement positions nodes and refines their geometry before routing.
Package placement positions nodes and refines their geometry before routing.
d2talalayout/internal/placementcost
Package placementcost evaluates candidate node geometry for placement and refinement algorithms.
Package placementcost evaluates candidate node geometry for placement and refinement algorithms.
d2talalayout/internal/proximity
Package proximity discovers layout relationships between nearby nodes.
Package proximity discovers layout relationships between nearby nodes.
d2talalayout/internal/quality
Package quality evaluates completed TALA layouts.
Package quality evaluates completed TALA layouts.
d2talalayout/internal/routing
Package routing builds orthogonal visibility graphs, searches them for edge routes, and applies the routing post-processing stages.
Package routing builds orthogonal visibility graphs, searches them for edge routes, and applies the routing post-processing stages.
d2talalayout/internal/trees
Package trees owns TALA's tree discovery, preprocessing, orientation, and placement algorithms.
Package trees owns TALA's tree discovery, preprocessing, orientation, and placement algorithms.
d2talalayout/internal/typedpool
Package typedpool provides a type-safe wrapper around sync.Pool.
Package typedpool provides a type-safe wrapper around sync.Pool.
Completion implements lsp autocomplete features Currently handles: - Complete dot and inside maps for reserved keyword holders (style, labels, etc) - Complete discrete values for keywords like shape - Complete suggestions for formats for keywords like opacity
Completion implements lsp autocomplete features Currently handles: - Complete dot and inside maps for reserved keyword holders (style, labels, etc) - Complete discrete values for keywords like shape - Complete suggestions for formats for keywords like opacity
Package d2plugin enables the d2 CLI to run functions bundled with the d2 binary or via external plugin binaries.
Package d2plugin enables the d2 CLI to run functions bundled with the d2 binary or via external plugin binaries.
d2renderers
d2ascii
Set DEBUG_ASCII=1 environment variable to enable verbose ASCII rendering debug logs.
Set DEBUG_ASCII=1 environment variable to enable verbose ASCII rendering debug logs.
d2fonts
TODO write a script to do this as part of CI Currently using an online converter: https://dopiaza.org/tools/datauri/index.php
TODO write a script to do this as part of CI Currently using an online converter: https://dopiaza.org/tools/datauri/index.php
d2raster
Package d2raster renders the supported d2scene subset with a pure-Go raster kernel.
Package d2raster renders the supported d2scene subset with a pure-Go raster kernel.
d2raster/internal/scanline
Package scanline rasterizes D2's closed vector paths with non-zero winding.
Package scanline rasterizes D2's closed vector paths with non-zero winding.
d2scene
Package d2scene defines the renderer-neutral scene representation used by D2's raster export pipeline.
Package d2scene defines the renderer-neutral scene representation used by D2's raster export pipeline.
d2scenebuild
Package d2scenebuild converts D2's typed export model into the immutable scene consumed by d2raster.
Package d2scenebuild converts D2's typed export model into the immutable scene consumed by d2raster.
d2svg
d2svg implements an SVG renderer for d2 diagrams.
d2svg implements an SVG renderer for d2 diagrams.
d2svgimport
Package d2svgimport imports the bounded, network-free SVG subset used by D2 assets and MathJax into d2scene.
Package d2svgimport imports the bounded, network-free SVG subset used by D2 assets and MathJax into d2scene.
internal/fontface
Package fontface owns the renderer-internal OpenType parsing, shaping, and color-glyph pipeline shared by scene construction and native raster output.
Package fontface owns the renderer-internal OpenType parsing, shaping, and color-glyph pipeline shared by scene construction and native raster output.
internal/fontfallback
Package fontfallback contains the renderer-internal handoff used to retain trusted fallback assets across multiple scene builds without widening D2's public font resolver interface.
Package fontfallback contains the renderer-internal handoff used to retain trusted fallback assets across multiple scene builds without widening D2's public font resolver interface.
internal/patternassets
Package patternassets owns the canonical built-in texture definitions shared by D2's SVG and scene renderers.
Package patternassets owns the canonical built-in texture definitions shared by D2's SVG and scene renderers.
d2themes defines themes to make d2 diagrams pretty Color codes: darkest (N1) -> lightest (N7)
d2themes defines themes to make d2 diagrams pretty Color codes: darkest (N1) -> lightest (N7)
docs
e2etests
report command
internal
rasterimage
Package rasterimage decodes a deterministic still image from supported raster formats.
Package rasterimage decodes a deterministic still image from supported raster formats.
testlog
Package testlog adapts testing-style loggers to slog.
Package testlog adapts testing-style loggers to slog.
testutil/imagediff
Package imagediff compares rendered images and produces deterministic visual diagnostics suitable for test failures.
Package imagediff compares rendered images and produces deterministic visual diagnostics suitable for test failures.
lib
compression
d2 uses compression for compressing large static assets when its built into WASM for d2.js
d2 uses compression for compressing large static assets when its built into WASM for d2.js
env
geo
imageasset
Package imageasset resolves bounded local, HTTP, and data-URI image resources without depending on a renderer or scene representation.
Package imageasset resolves bounded local, HTTP, and data-URI image resources without depending on a renderer or scene representation.
log
Copied from https://pkg.go.dev/log/slog#Handler
Copied from https://pkg.go.dev/log/slog#Handler
memfs
memfs implements an in-memory fs.FS implementation This is useful in for running d2 in javascript environments where native file calls are not available
memfs implements an in-memory fs.FS implementation This is useful in for running d2 in javascript environments where native file calls are not available
pdf
png
pptx
pptx is a package to create slide presentations in pptx (Microsoft Power Point) format.
pptx is a package to create slide presentations in pptx (Microsoft Power Point) format.
simplelog
Package simplelog contains a very simple interface for logging strings at either Debug, Info, or Error levels
Package simplelog contains a very simple interface for logging strings at either Debug, Info, or Error levels
svg
xgif
Package xgif quantizes, normalizes, and encodes rendered animation frames as GIF.
Package xgif quantizes, normalizes, and encodes rendered animation frames as GIF.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL