Runtime Configuration Strategies?

How is everyone handling runtime configuration?

I currently have the issue of needing to provide an application with atoms, integers, booleans, and strings through environment variables so the application remains environment-agnostic and we can supply it config at boot time. But the problem is environment variables only support strings. So I’d like to do some coercion when we retrieve the variable, but I don’t see an easy way to use a module from the application inside of prod.exs, or config.exs. Does anyone have a way around this or a better way of handling this kind of configuration?

If you’re fine doing it in config.exs or prod.exs you can just do:

config :myapp, MyApp.Thing, 
  num: String.to_integer(System.get_env("NUM") || "2"),
  atom: String.to_atom(System.get_env("ATOM") || "val"),

This won’t work with releases though. But if you’re not using them, this is fine.

Yeah that’s similar to what we have been doing, but it doesn’t work well for booleans. But it does get the job done. I guess I’m trying to do something a little more elegant and were working towards using releases, but right now just need the application to be more config agnostic for our kubernetes deployments. I’m looking at using TOML or the config.exs provider options for distillery 2.0 which might be the only solution to the issue we are having. Thanks for replying I appreciate it. :slight_smile:

I’ll mention this just so you’re aware: true == :true and false == :false. So, the String.to_atom/1 would work just as well for booleans.

The provider options for distillery 2.0 are definitely the way to go. I assume you’re already aware of: https://github.com/bitwalker/toml-elixir

Though, it’s worth noting that TOML has no way to specify an atom value. Which is useful for something like log level.

3 Likes

was not aware that :true == true that’s interesting. I know some atoms were synonymous with certain things but I wasn’t aware those were one of them. :slight_smile: So that’s awesome to know.

and yeah toml was something I was looking into. It seems like a decent solution. Although looks like I will need to write a custom transformer to handle atom conversions, which shouldn’t be too terrible. Unless I am completely misunderstanding those docs.

It’s not that they are synonyms, true quite literally is the atom :true, just that elixir lets you leave the : off on it, same with :false == false and :nil == nil. In erlang you just use atoms for all that (which is any word that starts with a lowercase or is surrounded by ''s). :slight_smile:

4 Likes