envy icon indicating copy to clipboard operation
envy copied to clipboard

How do I use envy with phoenix?

Open webmstk opened this issue 5 years ago • 3 comments

I placed section

    unless Mix.env() == :prod do
      Envy.auto_load()
    end

to lib/app/application.ex and it does not work (I have System.get_env("DB_USER", "postgres") in config/dev.exs)

webmstk avatar Apr 18 '20 08:04 webmstk

  1. create a .env file in the root of you App with content:
POSTGRES_USER=foobar
  1. open application.ex and add inside def start:
def start(_type, _args) do
  # ...
  unless Mix.env() == :prod do
    Envy.load([".env"])
  end
  # ...
end
  1. Any where on your application, just use:
<%= System.get_env("POSTGRES_USER") %>

It works on mine with

Erlang/OTP 23 [erts-11.0.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]
Elixir 1.10.4 (compiled with Erlang/OTP 23)
Phoenix Framework 1.5.4

Narven avatar Sep 19 '20 14:09 Narven

What about setting env vars in config.exs? Pretty sure config gets run before application start, so the env variables aren't set yet.

jckw avatar Jan 26 '21 08:01 jckw

In our codebase, we have Envy.load([".env"]) at the top of config files that require env vars from .env.

J3RN avatar Dec 27 '21 20:12 J3RN