sashaafm
Retrieving Mix configurations at compile-time
Hey guys, continuing from my other thread I’m still building and exploring some compile-time stuff that Elixir can provide.
Right now I’m trying to operate on the Mix config files at compile-time, but I keep getting nil values from the app keys. At first I thought that maybe the configs could only be read at runtime, but then I found this in the Mix.Config doc page:
Configuration set using Mix.Config will set the application env, so that Application.get_env/3 and other Application functions can be used at run or compile time to retrieve or change the configuration.
Is there a specific way to retrieve the config values at compile-time, then? I’ve been trying both:
inside a macro
defmodule A do
defmacro test do
IO.inspect Application.get_env(:app, :key)
end
end
and just inside a module
defmodule A do
IO.inspect Application.get_env(:app, :key)
end
but I always get nil
. Any tips would be appreciated.
Marked As Solved
hubertlepicki
You must be doing something wrong. This should, and in my case - does work.
Check out this repo and run mix compile:
my results:
➜ compiletimeconfig git:(master) ✗ mix compile
Compiling 1 file (.ex)
"Hello"
Generated compiletimeconfig app
Which correctly prints “Hello” configured in config/config.ex
Here is the relevant commit:
Also Liked
sashaafm
Just looked at your repo and figured that I had the import_config command commented on my config.exs. I literally spent my whole afternoon trying to solve this. Even wrote a parser for the config file!

Thank you @hubertlepicki
hubertlepicki
yes, always tripple check if you do not have a typo or 1 character error before writing a parser of your own 








