Skip to content

Commit 3403265

Browse files
committed
Update Elixir: Using Environment Variables
1 parent 0ea4b4a commit 3403265

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

content/articles/20210916-elixir-environment-variables.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ And the same call would work in your application source code.
2222

2323
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.
2424

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:
2626

2727
```sh
28-
mix new test
29-
cd test
28+
mix new foo
29+
cd foo
3030
mkdir config
3131
touch config/config.exs
3232
```
@@ -36,16 +36,16 @@ And edit your `config/config.exs` file, like this:
3636
```elixir
3737
import Config
3838

39-
config :test, hello: System.get_env("HELLO")
39+
config :foo, hello: System.get_env("HELLO")
4040
```
4141

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:
4343

4444
```elixir
45-
module Test do
45+
module Foo do
4646
# ...
4747
def hello do
48-
Application.get_env(:test, :hello, "default")
48+
Application.get_env(:foo, :hello, "default")
4949
end
5050
end
5151
```
@@ -60,12 +60,12 @@ MIX_ENV=prod mix release
6060
Now let's run an interactive terminal to our build, and let's override that env variable along the way:
6161

6262
```sh
63-
HELLO=bar ./_build/prod/rel/test/bin/test start_iex
63+
HELLO=bar ./_build/prod/rel/foo/bin/foo start_iex
6464
```
6565

66-
There, calling `Test.hello` would return "foo", not "bar".
66+
There, calling `Foo.hello` would return "foo", not "bar".
6767

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.
6969

7070
## What happened
7171

@@ -93,5 +93,19 @@ _Et voilà !_ Hope this helps.
9393

9494
If you want to know more about Elixir builds and releases, as always, [the official documentation is a great place to start][1].
9595

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.
106+
96107
[0]: https://hex.pm/packages/env
97108
[1]: https://hexdocs.pm/mix/1.12/Mix.Tasks.Release.html
109+
[2]: https://hexdocs.pm/elixir/config-and-releases.html
110+
[3]: /articles/20250227-loading-env-without-leaks
111+
[4]: https://hexdocs.pm/mix/1.18.4/Mix.Tasks.Release.html

0 commit comments

Comments
 (0)