2

I am trying to get env file values in my dev.exs file. But they are coming as nil.

config :extractor, :mailgun,
  domain: System.get_env['MAILGUN_DOMAIN'],
  key: System.get_env['MAILGUN_KEY']

When I go to iex they are coming as actual values. In my app mix.exs I have added dotenv like this

  def application do
    [mod: {Extractor, []},
     applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
                    :phoenix_ecto, :postgrex, :tzdata, :httpoison, :dotenv, :quantum]]
  end

Its not specified for any env. Its just in application list.

I already tried this way as well

  def application do
    [mod: {Extractor, []},
     applications: app_list(Mix.env)]
  end


  defp app_list(:dev), do: [:dotenv | app_list]
  defp app_list(:test), do: [:dotenv | app_list]
  defp app_list(_), do: app_list
  defp app_list, do: [
    :phoenix,
    :phoenix_pubsub,
    :phoenix_html,
    :cowboy,
    :logger,
    :gettext,
    :phoenix_ecto,
    :postgrex,
    :tzdata,
    :httpoison,
    :dotenv,
    :quantum
  ]

But no luck as well.. any help?

Junaid Farooq
  • 2,484
  • 5
  • 26
  • 63
  • 3
    https://github.com/avdi/dotenv_elixir has a big warning at the top of the README which (somewhat) mentions/explains this: "Elixir has an excellent configuration system and this dotenv implementation has a serious limitation in that it isn't available at compile time. ...". – Dogbert Oct 25 '16 at 08:43
  • An option is https://medium.com/@kkomaz/phoenix-setting-up-env-variables-6557eb1370ee But if you read it also read comments as there is small type. https://medium.com/p/6557eb1370ee/responses/show – Ula Apr 28 '20 at 22:35

3 Answers3

2

I don't think you need to use dotenv (as stated on their github page https://github.com/avdi/dotenv_elixir, Elixir has a very good system on its own to handle env configuration).

Try this out and tell me if it does it for you :

  1. Create a simple .env file at the root of your project where you export your env variables :
.env
export MAILGUN_DOMAIN=mailgundomain.com
...
  1. Source it : source .env before launching the server or iex.
  2. Access your env variables defined in your dev.exs as follows :

Application.get_env(:extractor, :mailgun)[:domain]

And use double quotes instead of single quotes. From Elixir point of view, 'foo' and "foo" are two different things.

Hope it helps !

Community
  • 1
  • 1
BachirC
  • 199
  • 1
  • 10
1

Looking at the warnings at https://github.com/avdi/dotenv_elixir and referring to https://stackoverflow.com/a/20909045/111021, I came up with this in ~/.bashrc:

alias mix='env $([ -f .env ] && cat .env | xargs) mix'
alias iex='env $([ -f .env ] && cat .env | xargs) iex'

This works for common use cases mix phoenix.server and iex -S mix. If your dotenv has complicated values like quotes or spaces you may need to further tweak the commands.


Caveat: if you use something like mix release https://github.com/bitwalker/distillery you may get env vars mixed into the package.

Community
  • 1
  • 1
kizzx2
  • 18,775
  • 14
  • 76
  • 83
0

You should use double quoted strings as a keys, not single quoted charlists.

System.get_env["LOGNAME"]
> "patnowak"

System.get_env['LOGNAME']
> nil
PatNowak
  • 5,721
  • 1
  • 25
  • 31