8342379

8342379

Handling environment variables in Elixir?

Hello,

I have searched the forum about using environment variables and none of the suggestions consistently worked for me.

I’m on Phoenix 1.7.12 and have tried Dotenvy and DotenvParser for reading .env files.

.env

SECRET=123ABCDE
HELLO=WORLD

config/runtime.exs

import Dotenvy
source!([".env", System.get_env()])

config :my_app,
  hello: env!("HELLO", :string!),
  secret: env!("SECRET", :string!)

iex

Dotenvy.source!(".env") 
# %{"HELLO" => "WORLD", "SECRET" => "123ABCDE"}
env!("HELLO", :string!) 
# "WORLD"
Application.get_env(:my_app, :hello)
# nil

When I try Application.get_env(:my_app, :hello), it returns nil. Same deal if I use DotenvParser. Not sure what I’m doing wrong

Thanks

Marked As Solved

8342379

8342379

I figured it out. I put the environment variables within the :prod scope. Working normally now that I moved it outside :prod. Thanks all!

Also Liked

cschmatzler

cschmatzler

Have you verified that the call in runtime.exs is actually looking for a .env file in the project root (which iex can see) rather than in the config directory?

Nicd

Nicd

Are you running iex -S mix or just plain iex? Just running iex won’t evaluate any of your project files such as the configuration.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement