I'm writing an Elixir package, and I want to specify a default application configuration (that the user can override by specifying custom values in their config.exs
). I was originally putting them in my project's config.exs
until I realized that the config file won't be loaded for projects that depend on this library.
The config file itself tells you that:
This configuration is loaded before any dependency and is restricted to this project. If another project depends on this project, this file won't be loaded nor affect the parent project. For this reason, if you want to provide default values for your application for 3rd-party users, it should be done in your "mix.exs" file.
I've been struggling to understand how to specify application defaults in my mix.exs
and use them. My current solution is to use Application.get_env/3
with a default argument but that doesn't seem right to me as the application defaults would be scattered through out the code.
Application.get_env(:my_library, :arg, "default value")
So, How can I specify application defaults in mix.exs
?
You can set default config values for your application in mix.exs
- these will also be available when used as dependency in another project. For example:
def applications do
[applications: [:logger, ...],
mod: {MyLibrary.Application, []},
env: [arg: "default value"]]
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With