Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config/runtime.exs #38

Open
ksmithut opened this issue Feb 8, 2021 · 8 comments
Open

config/runtime.exs #38

ksmithut opened this issue Feb 8, 2021 · 8 comments

Comments

@ksmithut
Copy link

ksmithut commented Feb 8, 2021

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?

@iloveitaly
Copy link
Collaborator

@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!

@ksmithut
Copy link
Author

ksmithut commented Feb 8, 2021

@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!()

@ksmithut
Copy link
Author

ksmithut commented Feb 8, 2021

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.

@ksmithut
Copy link
Author

ksmithut commented Feb 8, 2021

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 :)

@iloveitaly
Copy link
Collaborator

@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.

@ksmithut
Copy link
Author

I believe runtime loads in prod as well, though you could probably do a check for the environment before calling Dotenv.load!

@fireproofsocks
Copy link

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, Mix is not available)
  • it might be useful to parse the .env from inside your runtime.exs
  • there might be a need for an :overwrite flag for cases where a system ENV is already set (not sure if your implementation handles this or not, but imagine SOME_VAR=xyz _build/dev/rel/myapp/bin/myapp start_iex -- the expectation is that SOME_VAR would override any SOME_VAR declared in a .env

@fireproofsocks
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants