Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get the environment variables in Elixir project

I defined a environment variable related to the guide-started, my mix.exs is

defmodule Basic.Mixfile do
  use Mix.Project

  def project do
    [app: :basic,
     version: "0.0.1",
     elixir: "~> 0.13.0-dev",
     deps: deps,
     env: [
      dev: [foo: "bar"] ] ]
  end

  # Configuration for the OTP application
  #
  # Type `mix help compile.app` for more information
  def application do
    [ applications: [],
      mod: { Basic, [] } ]
  end

  # List all dependencies in the format:
  #
  # { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" }
  #
  # Type `mix help deps` for more examples and options
  defp deps do
    []
  end
end

and then I start the project with iex -S mix or MIX_ENV=dev iex -S mix, I want to get the environment variable with :application.get_env(:basic, :foo) , it turned to :undefined; and use :application.get_all_env(:basic), it returned [included_applications: []], there is not the env variable. And my question is how should I get the environment value?

like image 840
jsvisa Avatar asked Dec 12 '22 06:12

jsvisa


1 Answers

The :env must not be specified in project but inside the application one.

def application do
  [ applications: [],
    mod: { Basic, [] },
    env: [foo: :bar] ]
end

Then you can access it as:

Application.get_env(:basic, :foo)

In this case, there is no support for specifying the environment for a particular MIX_ENV (like dev or prod). This is exactly why :env inside project is deprecated. Having two keys being named :env with different behaviour is recipe for confusion. :)

like image 182
José Valim Avatar answered Feb 12 '23 20:02

José Valim