mcarvalho

mcarvalho

System.get_env vs. Application.get_env

What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.

Most Liked

NobbZ

NobbZ

System.get_env/3 gets from the system environment, while Application.get_env/3 gets from the applications environment.

The first is defined by the means of your operating system, while the later is mostly configured during build using the file(s) in config and local to your application.

The application environment is described in the docs: Application — Elixir v1.20.2

The system-environment is not, since it is not any part of elixir or the BEAM. It’s just there…

zenw0lf

zenw0lf

Replies above explain the difference between Application.get_env and System.get_env.

As for best practices, have a look at this: Configuration Demystified | Blog · Elixir School

mbuhot

mbuhot

My rule of thumb is to only use System.get_env during application initialization, eg in your Application.start, Repo.init, Endpoint.init callbacks.

Once the application is initialized, the environment should always be queried by Application.get_env, since System.get_env can only return strings.

I like the dev / test environment to be statically defined in the config.exs files, only reading from the system environment for prod builds.

Last Post!

pmarreck

pmarreck

Ah… good point. a value retrieval is a value retrieval, whether it happens once (at build) or many times (at runtime) depends on the caller. Of course. :man_facepalming:

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement