myl

myl

What is the cleanest way to load and parse en vars during both configuration and execution?

Hi everybody,

First, I am very new to Elixir and to this forum. Please forgive me if my english is bad or if I transgress any rule :upside_down_face:

I need to find a clean way to load environment variables during both configuration and execution phases, to configure the app depending on the environment (prod/dev/etc).

My new team already uses an imperfect solution :
At runtime, we dynamically load environment variables depending on the environment the app is running, thanks to an in-house helper (env.ex) that fetch the env var and parse it to verify its format.
As some of this vars are required on configuration phase, the module is compiled a first time from config.exs, and some variable are loaded later from the config.exs file (second parameter is a default value):

Code.compile_file("lib/our_app/utils/env.ex")
[...]
secondapp_server_url: Env.load("SECOND_APP_URL", "http://localhost:3001")

This solution is working, but the env.ex module is compiled a second time later (during the app compilation), resulting in a warning (double compilation) that we want to get rid of.
Also, they brainstormed before my arriving and concluded that this module has been very useful to them, so they’d like to keep it.

First, we thought that we simply had to put our env.ex in its own new hex package and add it as a dependency to our app. Doing so, I discovered by running mix deps.get that the configs.exs is parsed before deps are fetched and compiled. Thus, the mix deps.get command is crashing:

$ mix deps.get
** (UndefinedFunctionError) function Env.load/2 is undefined (module Env is not available)
    Env.load("SECOND_APP_URL", "http://localhost:3001")
    (stdlib 3.17) erl_eval.erl:685: :erl_eval.do_apply/6
    (stdlib 3.17) erl_eval.erl:893: :erl_eval.expr_list/6
    (stdlib 3.17) erl_eval.erl:237: :erl_eval.expr/5
    (stdlib 3.17) erl_eval.erl:229: :erl_eval.expr/5
    (stdlib 3.17) erl_eval.erl:230: :erl_eval.expr/5
    (stdlib 3.17) erl_eval.erl:893: :erl_eval.expr_list/6
    (stdlib 3.17) erl_eval.erl:408: :erl_eval.expr/5

I can’t find the best way to keep this module for both configuration and execution purpose (I believe transforming it in a .exs file would not make it), or which better practices I should follow to replace it.
So any help would be appreciated !
Thank you !

Marked As Solved

Nicd

Nicd

Tooting my own horn here, but I’ve written a blog post that describes a system of configuration that I personally use, where you have the same configuration in development and production: Simple Configuration Setup for Elixir Projects (v1.11+) – Random Notes – Reading it may provide you with some new thoughts.

But to summarise, you can probably move most of your config to config/runtime.exs like LostKobrakai said. This file is evaluated both in development and production when your system starts up. In that file you can also use your Env module without any issues, as it has already been compiled at that point (just like I use my own env helpers in my blog post). The rest of the “classic” config/*.exs files are for compile time configuration, which is not needed for most things you want to configure.

If you need environment variables in compile time configuration for some reason, then what you are doing now (with the double-compilation warning) is probably the best you can do.

Also Liked

fuelen

fuelen

I also wrote a utility which does casting, validation and inspection of ENV variables according to the predefined schema. You may be interested in some ideas or just in using the library :slight_smile: GitHub

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement