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:
https://github.com/hubertlepicki/compiletimeconfig
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:
https://github.com/hubertlepicki/compiletimeconfig/commit/379de75e782943f3c7bf0a50d61f2d6d859a6b05
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 ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








