2

In my umbrella project using distillery for releases, I have a db app config with the following:

config :main, Main.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "${DB_USERNAME}",
  password: "${DB_PASSWORD}",
  database: "${DB_NAME}",
  hostname: "${DB_HOST}",
  pool_size: 10

As I have set REPLACE_OS_VARS=true in my build, the environment vars are read correctly configures the database.

I have a similar set up in an email app which has the following config:

config :email, from_email: "${FROM_EMAIL}"

I am then looking to access this from inside my email app, like so:

@from_email Application.get_env(:email, :from_email)

But the value of @from_email is "${FROM_EMAIL}" not the environment variable I set for FROM_EMAIL.

I am not too familiar with how distillery works and am unsure how passing in these environment variables in these similar ways is causing it to be read differently.

Insight as to why this may be happening would be greatly appreciated.


EDIT:

We are able to pass the $FROM_EMAIL environment variable in when distillery compiles, but we would like to pass this in at run time. We are able to do this in the db app, but not in the email app for some reason.

Justin Wood
  • 9,941
  • 2
  • 33
  • 46
Sam Houston
  • 3,413
  • 2
  • 30
  • 46
  • Thanks for the fast response, but why would that come to mind? I have double checked and it and the `FROM_EMAIL` var is set, would it be an issue with not passing it in to app correctly – Sam Houston Feb 20 '18 at 14:29
  • Instead of storing in a module attribute, try calling `Application.get_env(:email, :from_email)` at runtime. – Dogbert Feb 20 '18 at 15:30
  • That's the answer, thanks a bunch @Dogbert. Post an answer and I'll accept it – Sam Houston Feb 20 '18 at 16:32

1 Answers1

3

Code directly inside the module definition (i.e. outside def) is evaluated at compile time. You'll need to call Application.get_env at runtime to get the correct value:

Add:

def from_email, do: Application.get_env(:email, :from_email)

to the module and then change all @from_email in the module to from_email().

Dogbert
  • 212,659
  • 41
  • 396
  • 397