Unable to get environment variables in Elixir project

I defined an environment variable associated with the manual , my mix.exs

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 mixor MIX_ENV=dev iex -S mix, I want to get the environment variable with :application.get_env(:basic, :foo), she turned to :undefined; and use :application.get_all_env(:basic), he returned [included_applications: []], there is no variable env. And my question is: how do I get the environment value?

+4
source share
4 answers

:envshould not be specified in project, but inside the application.

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

Then you can access it:

Application.get_env(:basic, :foo)

MIX_ENV (, dev prod). :env project . :env - .:)

+9

, Mix.project[:foo] ( "bar" ), MIX_ENV dev. , v0.13.1, .

"The :env key in Basic.Mixfile project configuration is deprecated" 

, https://github.com/phoenixframework/ex_conf .

+2

env = :application.get_env(:example, Mix.env). , :dev mix.exs. Keyword.get(env, :foo) .

+1
source

This may be redundant for your needs, but Avdi Grimm recently ported the Dotens to Elixir .

+1
source

Source: https://habr.com/ru/post/1538751/


All Articles