Skip to content

Dynamic Configuration

Luis Rascão edited this page Feb 2, 2015 · 7 revisions

With relx.config you can make use of dynamic configuration based on file:script/2.

If a <name>.script exists in the same directory as the original file (in the case of relx.config that would be relx.config.script, the script file will be evaluated and the result used as configuration. This also works for custom relx.config files.

For convenience two bindings (variables) are available in a script during evaluation:

  1. CONFIG - result of file:consult/1 if the script file being evaluated also exists without the .script extension. Otherwise, [].

  2. SCRIPT - filename of the script being evaluated

In all cases, the data returned by the script, that is the last thing evaluated in the file, must be data in the same format as the original non-script file. For example, if I have relx.config.script that script must return relx configuration data.

Simple Example

If you are using overlay vars you might have several vars.config files, for example one for dev and another for production.

The following relx.config.script file can be kept centrally, and linked into your application directory:

case os:getenv("PRODUCTION") of
    "true" ->
	    lists:keystore(overlay_vars, 1, CONFIG, {overlay_vars, "config/production.vars.config"});
    _ -> CONFIG % env var not defined or anything other than true
end.    

Whenever you want to build 'properly' (which you should, regularly), simply call unset PRODUCTION (or equivalent), and perform a clean build.

Note that file:script/2 differs from file:consult/1 in that only the result of the last expression is returned. You must therefore take care to return a list of config items. Before that, you may do any form of IO (including network), checking OS environment variables, reading files (perhaps calling file:script/2 on other files) or writing files. You can basically use all OTP libs. As in file:eval/2, each expression is terminated with a full stop.

Clone this wiki locally