config/runtime.exs
In your README, you state that this isn't compatible with Elixir Releases because you can't use this module at compile time, which makes sense. But in Elixir 11, they added a new runtime configuration file documented here. Could you document how one might use this module using this config/runtime.exs file? Or is it still not possible?
@ksmithut interesting! Do you know where I can learn more about runtime.exs the documentation you linked to is pretty sparse. How is this different from the other configuration options?
I don't personally use releases, so your best bet is trying dotenv with runtime.exs to see if it supports runtime loading of configuration. Let me know what you find!
@iloveitaly Ya, their hex docs are very sparse... Looks like the best one I could find was their 1.11 release announcement.
So I haven't tried building it with releases, but I put this at the top of my config/runtime.exs file and it loads it in mix mode:
use Mix.Config
Dotenv.load!()
I'm still tweaking usage, I'm still pretty new to Elixir. Right now my setup isn't working with releases, but I'm diving in to get a setup that works. I'll let you know what I find.
Okay, I was able to get it to work with that tweak with one minor alteration. In my config/runtime.exs instead of use Mix.Config I used import Config, so the top of my file looked like this:
import Config
Dotenv.load!()
And it loads from my .env file just fine :)
@ksmithut nice, that's awesome! Do you know if runtime loads in prod as well? My guess is if you are doing a Heroku-style deploy (i.e. without releases) you'd actually want Dotenv not to run in that environment.
I believe runtime loads in prod as well, though you could probably do a check for the environment before calling Dotenv.load!
This discussion may be useful: https://elixirforum.com/t/what-cannot-be-runtime-config/38499/9
particularly the example app linked including the runtime.exs usage here: https://gitlab.com/code-stats/code-stats/-/blob/b1cf53462a3fa34369eaa06494754c7ae38aed2a/config/runtime.exs
A couple takeaways:
- you can't use
Mix.env()inside your application start (because in a release build,Mixis not available) - it might be useful to parse the
.envfrom inside yourruntime.exs - there might be a need for an
:overwriteflag for cases where a system ENV is already set (not sure if your implementation handles this or not, but imagineSOME_VAR=xyz _build/dev/rel/myapp/bin/myapp start_iex-- the expectation is thatSOME_VARwould override anySOME_VARdeclared in a.env
In my testing, this package worked fine with releases. I included an example of this in PR #40
In short, in your runtime.exs, you can do something like this:
import Config
if config_env() != :test do
Dotenv.load(".env")
config :yourapp,
db_url: System.fetch_env!("DB_URL"),
# ... etc ...
end