Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default application configs for Elixir packages

Tags:

elixir

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?

like image 602
Sheharyar Avatar asked Feb 04 '23 17:02

Sheharyar


1 Answers

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
like image 119
Patrick Oscity Avatar answered May 23 '23 12:05

Patrick Oscity