-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read environment variables in config/config.exs #4
Comments
If I could use the syntax |
If you run the following in unless Mix.env == "prod" do
Envy.auto_load
end |
Dependencies are not loaded when config runs, so it doesn't. |
Can you call |
Adding the following: unless Mix.env == :prod do
Envy.auto_load
end Fails with:
With Application.load(:envy)
unless Mix.env == :prod do
Envy.auto_load
end It doesn't change. In a pry session:
|
Ah! Turns out Mix.Config doesn't allow dependencies to be used, or easily compiled and used in the config file. There is a workaround, which I think may be a good addition to Envy if it works for you. You can run the following to re-eval the config: Mix.Config.read!("config/config.exs") |> Mix.Config.persist I think you'd want to put it directly after |
Yep! Following patch works well: From adebf8561a99b5cd2054e2d97c16e28aff078147 Mon Sep 17 00:00:00 2001
From: Tute Costa <tutecosta@gmail.com>
Date: Thu, 30 Jun 2016 12:18:52 -0400
Subject: [PATCH] Add Envy for environment variables
---
.sample.env | 3 +++
lib/app_name.ex | 5 +++++
mix.exs | 1 +
mix.lock | 1 +
4 files changed, 10 insertions(+)
create mode 100644 .sample.env
diff --git a/.sample.env b/.sample.env
new file mode 100644
index 0000000..707fbf9
--- /dev/null
+++ b/.sample.env
@@ -0,0 +1,3 @@
+AWS_ACCESS_KEY_ID=abcd
+AWS_SECRET_ACCESS_KEY=abcd
+AWS_S3_BUCKET=abcd
diff --git a/lib/app_name.ex b/lib/app_name.ex
index f2b6334..5899679 100644
--- a/lib/app_name.ex
+++ b/lib/app_name.ex
@@ -6,6 +6,11 @@ defmodule AppName do
def start(_type, _args) do
import Supervisor.Spec, warn: false
+ unless Mix.env == :prod do
+ Envy.auto_load
+ Mix.Config.read!("config/config.exs") |> Mix.Config.persist
+ end
+
children = [
# Start the endpoint when the application starts
supervisor(AppName.Endpoint, []),
diff --git a/mix.exs b/mix.exs
index 8f2feb2..98f8472 100644
--- a/mix.exs
+++ b/mix.exs
@@ -46,6 +46,7 @@ defmodule AppName.Mixfile do
{:arc, "~> 0.5.2"},
{:comeonin, "~> 2.4"},
{:cowboy, "~> 1.0"},
+ {:envy, "~> 0.0.2"},
{:ex_aws, "~> 0.4.10"},
{:ex_machina, "~> 1.0.0-beta.1", github: "thoughtbot/ex_machina"},
{:floki, "~> 0.8.1"},
diff --git a/mix.lock b/mix.lock
index bb52130..ef01aad 100644
--- a/mix.lock
+++ b/mix.lock
@@ -7,6 +7,7 @@
"db_connection": {:hex, :db_connection, "0.2.5", "3e5e28019e0ec744345568d22a2f5206109bff0e2571f4d7819e0d14cf955f3e", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:connection, "~> 1.0.2", [hex: :connection, optional: false]}]},
"decimal": {:hex, :decimal, "1.1.2", "79a769d4657b2d537b51ef3c02d29ab7141d2b486b516c109642d453ee08e00c", [:mix], []},
"ecto": {:hex, :ecto, "2.0.0-rc.5", "a95184a260edb89669be7746b3b270725ce6940e378b05d19df82eecf52544cb", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:postgrex, "~> 0.11.1", [hex: :postgrex, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:mariaex, "~> 0.7.1", [hex: :mariaex, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
+ "envy": {:hex, :envy, "0.0.2", "278045cf7a7c7d46bd607e9cd6632b7d3fc71ddaa9c26bf364ec11af961b313b", [:mix], []},
"ex_aws": {:hex, :ex_aws, "0.4.19", "84284a2ce936b1aed3d6db4dac4ccf28105f2b617201194b2804d737cd0ef818", [:mix], [{:jsx, "~> 2.5", [hex: :jsx, optional: true]}, {:httpotion, "~> 2.0", [hex: :httpotion, optional: true]}, {:poison, "~> 1.2 or ~> 2.0", [hex: :poison, optional: true]}, {:httpoison, "~> 0.8", [hex: :httpoison, optional: true]}, {:sweet_xml, "~> 0.5", [hex: :sweet_xml, optional: true]}]},
"ex_machina": {:git, "https://github.com/thoughtbot/ex_machina.git", "a24c02618b460094bb81ea90f41e052355383e5a", []},
"floki": {:hex, :floki, "0.8.1", "06aa75bf2d1e01cda7d2ad54f68614be653a11e76b474d8fcb1d838f8c1e0ad1", [:mix], [{:mochiweb_html, "~> 2.15", [hex: :mochiweb_html, optional: false]}]},
--
2.8.1 Thank you Blake! |
No problem! I think this would be nice as |
Then I think it'd be worth bumping up to 1.0 since Envy has seen good use and is pretty stable. 😃 |
👍 |
This was fixed in 1.0. Closing. Thanks for the report! |
I'm running into this same issue, but when configuring another app, pushex specifically, it seems to have a presence validation when it starts up its app and that throws an error since it isn't set at boot time. Error looks like so:
Any idea how to work around this? |
Not sure if it would work, but if we made Envy an I'll try to write something up real quick, push it up to a branch, and see if that works. Feel free to ping me in our #elixir channel too btw. |
@jakecraige I just pushed the branch |
Hi @BlakeWilliams I'm trying to use config :kafka_ex,
brokers: [
{System.get_env("KAFKA_BROKER_HOST"), 9092}
],
consumer_group: System.get_env("KAFKA_CONSUMER_GROUP"),
auto_commit: false,
use_ssl: false But I'm getting an error:
I'm reading values from # mix.lock
"envy": {:git, "https://github.com/BlakeWilliams/envy.git", "446ec3f609e5da1ece1870ed45e6edcf295cc3e9", [branch: "bmw-application"]}, # mix.exs
def application do
# Specify extra applications you'll use from Erlang/Elixir
[extra_applications: (Mix.env == :dev && [:exsync] || []) ++ [:envy, :logger, :maru, :timex, :ecto, :postgrex,
:timex_ecto, :kafka_ex],
mod: {Geronimo, []}]
end Any ideas? Thanks in advance! |
config/config.exs
loads beforelib/APP_NAME.ex
.How can we have envy read
.env
vars before theconfig.exs
file is read? I'm specifically configuring https://github.com/stavro/arc#s3-configuration.Thank you, Blake! :)
The text was updated successfully, but these errors were encountered: