Use TOML for `.env` Files?
snarky.ca
Use TOML for `.env` Files?
1–10 of 64 posts
Re: Use TOML for `.env` Files?
#2Given the myriad of configurations that can exist, including secrets management, static data etc. I would be very tempted to try to build tooling around a sqlite database, storing all your configs including static data, and update it with secrets at runtime. This way you can even remotely interface with the configuration for debug/monitoring, lint the config before commits etc.
Re: Use TOML for `.env` Files?
#3Re: Use TOML for `.env` Files?
#4If there are _different_ sets of variables, they should not be in that file, but in some different place, like the suggested settings.toml. Or perhaps use a bunch of different files in .envs/whatever.env and symlink them.
Let's not make everything complicated :)
Re: Use TOML for `.env` Files?
#5TBH, config is one of those Pandora's boxes - what happens if I want to do an integration test using a dev database and local docker images? what about testing after deployment to prod? Given the myriad of configurations that can exist, including secrets management, static data etc. I would be very tempted to try to build tooling around a sqlite database, storing all your configs including static data, and update it…
import values from local/or/public/private/url
...values
When reading the file, the environment variables will be obtained from the URL and populate the environment.This is what I had in mind when designing the import functionality for deon [1].
Being able to import also makes it easy to have a .base, a .production, a .local setup, and combine them accordingly.
Re: Use TOML for `.env` Files?
#6Then you define your config like that:
type ServerConfig struct {
Address string `env:"HTTP_ADDRESS"`
Port int `env:"HTTP_PORT"`
UseTLS bool `env:"HTTP_USE_TLS"`
CertFile string `env:"HTTP_CERT_FILE"`
KeyFile string `env:"HTTP_KEY_FILE"`
Timeout int `env:"HTTP_TIMEOUT"`
}
type Config struct {
Server ServerConfig `env:"SERVER"`
}
So, for example, with the code like this: if err := config.LoadToml(&cfg, tomlFileName); err != nil {
return nil, err
}
if err := config.LoadOverrides(&cfg); err != nil {
return nil, err
}
It can load from TOML file, and from environment variables. This means - service can be either started from container, or run locally with .toml file.Re: Use TOML for `.env` Files?
#7So, for my own projects in Go lang, I created a simple library for loading configuration files, so it can load configuration either from .toml files, or from .env files or just from an environment variables. Then you define your config like that: type ServerConfig struct { Address string `env:"HTTP_ADDRESS"` Port int `env:"HTTP_PORT"` UseTLS bool `env:"HTTP_USE_TLS"` CertFile string `env:"HTTP_CERT_FILE"` KeyFile str…
Re: Use TOML for `.env` Files?
#8The cool thing is, a file will always be configuration data, so decide what format works for you and your team and keep your wheels on the ground. Yaml, despite its opponents, is my first choice. I’ve never run into the problems people say the have with it, and I use it frequently to configure Docker-compose and kubernetes resources anyway.
Re: Use TOML for `.env` Files?
#9I always treated .env files as "this is the current set of environment variables", containing just NAME="value" pairs. If there are _different_ sets of variables, they should not be in that file, but in some different place, like the suggested settings.toml. Or perhaps use a bunch of different files in .envs/whatever.env and symlink them. Let's not make everything complicated :)
The author claims "There is no standard" but I think the standard is so simple it hasn't been written down. The standard is what you said, KEY="value" and that's it. Simple, easy to parse, fast and compatible with how environment variables are declared in `/etc/environment` since forever.
Having different .env files for different OSes is easy as well. You have one `.env` that provides the default values, then `.env.linux` for linux, `.env.windows` for windows and so on, and on runtime, first read .env, have values from .env.$os overwrite those, and finally have whatever the actual environment has overwrite those.
Again, simple and hard to misunderstand.
Re: Use TOML for `.env` Files?
#10I wrote a small Rust tool called ‘json_env’[0] to read JSON files and supply them as ENV vars to a program. I’m working on it in my free time and eventually want to also replace direnv with it. TOML and YAML support is also planned.