Module attribute constants nil in Elixir
Module attributes in elixir (@something
) can be used as constants and assigned a value, however one must make sure that the value assigned to the constant is available at compile time.
For example when using a constant that loads environment variables if the value is not available in compile time they will resolve to nil
.
@salt System.get_env(:your_app_name, "AUTH_SALT")
So either export the environment variables before compiling, or don't use module attributes, instead you can use a function:
defp salt do
System.get_env(:your_app_name, "AUTH_SALT")
end
Consider:
- The downside to using a function is that it will be re-evaluated each time.
- The downside of exporting env-vars before compile is that they you might forget and your app will crash in production. You can circumvent that by writing a script for compilation.
-
You can also call
System.get_env
from yourconfig.exs
but make sure to runmix clean
after doing so since the compiler seems to cache the config compilation.