d2 icon indicating copy to clipboard operation
d2 copied to clipboard

Passing variables to D2 through environment variables

Open albertored11 opened this issue 2 years ago • 3 comments

Variables are a great feature in D2, but it would be nice to be able to set values for the variables in other ways rather than directly in the .d2 file. In my case, I would like to reuse files for different diagrams, and having to create a new file with the values of the variables to import it from the main file is a bit inconvenient.

How about passing variables through environment variables in a similar fashion to Terraform variables?

E.g. to give bucket variable the value mybucket-1234 to be used when compiling a d2 file, do something like the following beforehand:

export D2_VAR_bucket="mybucket-1234"

I'd really appreciate your thoughts on this. Thanks in advance! :slightly_smiling_face:

albertored11 avatar Sep 25 '23 13:09 albertored11

Hmm, I can see that.

My first instinct was to have source variables take precedent over env variables, but I think I'd want to flip that so that the source variable is the "default", and all referenced variables have to exist in the source vars in order to compile.

vars: {
  num_servers: 2
  server_name: SET_ME_IN_ENVS
}

x: ${server_name}
...

alixander avatar Sep 25 '23 16:09 alixander

Maybe a simpler idea to start could be to allow an creation/override from d2 command line ? Something like --extra-vars parameter in Ansible CLI ( --extra-vars "version=1.23.45 other_variable=foo)

jygastaud avatar Nov 01 '23 08:11 jygastaud

this is also useful for themes

alixander avatar Aug 06 '24 18:08 alixander