You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/articles/20210916-elixir-environment-variables.md
+24-10Lines changed: 24 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,11 +22,11 @@ And the same call would work in your application source code.
22
22
23
23
However, you may want to avoid scattering those calls in every corner, and use a config file instead to centralize your settings and your default values.
24
24
25
-
Newer versions of Mix no longer generate config files by default, so you'd have to create one of those, like this:
25
+
Mix doesn't generate config files by default, so you'd have to create one of those, like this:
26
26
27
27
```sh
28
-
mix new test
29
-
cdtest
28
+
mix new foo
29
+
cdfoo
30
30
mkdir config
31
31
touch config/config.exs
32
32
```
@@ -36,16 +36,16 @@ And edit your `config/config.exs` file, like this:
36
36
```elixir
37
37
importConfig
38
38
39
-
config :test, hello:System.get_env("HELLO")
39
+
config :foo, hello:System.get_env("HELLO")
40
40
```
41
41
42
-
And lastly, modify `lib/test.ex` like this, to actually use the key-value pair we defined:
42
+
And lastly, modify `lib/foo.ex` like this, to actually use the key-value pair we defined:
43
43
44
44
```elixir
45
-
module Testdo
45
+
module Foodo
46
46
# ...
47
47
defhellodo
48
-
Application.get_env(:test, :hello, "default")
48
+
Application.get_env(:foo, :hello, "default")
49
49
end
50
50
end
51
51
```
@@ -60,12 +60,12 @@ MIX_ENV=prod mix release
60
60
Now let's run an interactive terminal to our build, and let's override that env variable along the way:
There, calling `Test.hello` would return "foo", not "bar".
66
+
There, calling `Foo.hello` would return "foo", not "bar".
67
67
68
-
You can easily get surprises when releasing your first Elixir app, because this is not something you can reproduce with `iex -S mix`.
68
+
You can easily get surprises when releasing your first Elixir app, because this is not something you can reliably reproduce with `iex -S mix` locally.
69
69
70
70
## What happened
71
71
@@ -93,5 +93,19 @@ _Et voilà !_ Hope this helps.
93
93
94
94
If you want to know more about Elixir builds and releases, as always, [the official documentation is a great place to start][1].
95
95
96
+
## 2025 Update
97
+
98
+
I wanted to make an update a while ago to state one thing: you probably don't need a library such as `env` in your application. You just have to remember the following statements:
99
+
- Your configuration is split between environments (`config/{dev,test,prod}.exs`) which is built at compilation time
100
+
- You have to make sure your environment variables are defined at compilation time if you make calls to `System.get_env`, otherwise, your config would contain empty settings. You can check [this article][3] for the most optimal way to do that locally.
101
+
- There's one exception to that: `config/runtime.exs` is *always* evaluated at runtime, and not only in production. This is where you can reliably call `System.get_env`. Just be cautious and insert production configuration inside the right "block" in there, because it's also used for dev and test.
102
+
103
+
There's actually another exception you may come across in older Elixir projects, which is `config/releases.exs`, used until Elixir v1.11.0.
104
+
105
+
Finally, checking both [Elixir][2] and [Mix][4] documentations for your versions is always a good idea.
0 commit comments