How to Update Elixir Project Configuration with TOML in Runtime.

Distributed Systems Development A-Z Guide.

Dmytro Nasyrov
Pharos Production
7 min readJan 29, 2019

--

Elixir configurable releases with TOML

Give us a message if you’re interested in Blockchain and FinTech software development or just say Hi at Pharos Production Inc.

Or follow us on Youtube to know more about Software Architecture, Distributed Systems, Blockchain, High-load Systems, Microservices, and Enterprise Design Patterns.

Pharos Production Youtube channel

What is TOML? TOML is a configuration file format language that is intended to be minimal and easy to read. TOML stands for “Tom’s Obvious, Minimal Language,” which refers to the creator Tom Preston-Werner. TOML is cool and you can find more about syntax in TOML repo. But here we will explain how to integrate TOML configurations into Elixir project, release it with a production environment settings and reload configuration without stopping the server.

We have created a new project with usual.

$> mix new tutorial_toml

Let’s add TOML library and Distillery to make a release and use TOML.

Distillery and TOML libraries in deps

Let’s configure release settings. In the root of your project create rel directory. We need only 2 files for the basic release functionality. Config.exs contains release settings and config_prod.toml overrides configuration of the Elixir project.

Release configurations

In config directory of the project, we have 3 files — common configuration, dev env configuration, and prod configuration.

Configuration directory for 3 environments

We’re interested in prod.exs. Here we have a single value — demo_value. As you will see later, this value is completely overridden by TOML.

A project prod configuration file

TOML file in rel contains values that reflect configuration settings. Here you can see app name in brackets, it’s the same as atom name of the app. And a key with a corresponding value, same as we have in prod config of the project. But the difference is in value itself to show that all values from project config are completely overridden.

A project prod TOML configuration

To check the demo value we will use just one function from the root module of the project.

Root project module with just one function
We will use this function to check a value in TOML

Now, let’s look at release configuration file rel->config.exs. In the first place, we generate Erlang magic cookie. We will not explain what is it in this tutorial, because its related to distributed Erlang cluster configuration.

Erlang’s magic cookie

Next is a release overall settings. Here we can find a default release selector and automatically fetched environment. We use tutorial_toml name for the default release here.

Overall release settings

Next thing is release config. We set version and set of applications. In our project, there is only one application that should be included in the release.

Release configuration

The last part of the release config file is environment settings. Dev_mode, include_erts, include_system_libs, include_src, cookie — are settings that don’t related to TOML. We’re interested in next settings:

overlays — we copy existing toml file into etc directory of the release. Also we can ref link it instead of copying by using :link atom.

To build a release from the root folder of the project execute statement below. Distillery will do all things for us including TOML transformation into erlang’s sys.config. Here Mix environment is set to prod and release configuration is tutorial_prod.

Release command

How to Update Elixir Project Configuration with TOML in Runtime.

Distributed & High-Load Systems Development.

Elixir configurable releases with TOML

What is TOML? TOML is a configuration file format language that is intended to be minimal and easy to read. TOML stands for “Tom’s Obvious, Minimal Language,” which refers to the creator Tom Preston-Werner. TOML is cool and you can find more about syntax in TOML repo. But here we will explain how to integrate TOML configurations into Elixir project, release it with a production environment settings and reload configuration without stopping the server.

We have created a new project with usual.

$> mix new tutorial_toml

Let’s add TOML library and Distillery to make a release and use TOML.

Distillery and TOML libraries in deps

Let’s configure release settings. In the root of your project create rel directory. We need only 2 files for the basic release functionality. Config.exs contains release settings and config_prod.toml overrides configuration of the Elixir project.

Release configurations

In config directory of the project, we have 3 files — common configuration, dev env configuration, and prod configuration.

Configuration directory for 3 environments

We’re interested in prod.exs. Here we have a single value — demo_value. As you will see later, this value is completely overridden by TOML.

A project prod configuration file

TOML file in rel contains values that reflect configuration settings. Here you can see app name in brackets, it’s the same as atom name of the app. And a key with a corresponding value, same as we have in prod config of the project. But the difference is in value itself to show that all values from project config are completely overridden.

A project prod TOML configuration

To check the demo value we will use just one function from the root module of the project.

Root project module with just one function
We will use this function to check a value in TOML

Now, let’s look at release configuration file rel->config.exs. In the first place, we generate Erlang magic cookie. We will not explain what is it in this tutorial, because its related to distributed Erlang cluster configuration.

Erlang’s magic cookie

Next is a release overall settings. Here we can find a default release selector and automatically fetched environment. We use tutorial_toml name for the default release here.

Overall release settings

Next thing is release config. We set version and set of applications. In our project, there is only one application that should be included in the release.

Release configuration

The last part of the release config file is environment settings. Dev_mode, include_erts, include_system_libs, include_src, cookie, overlay_vars — are settings that don’t related to TOML. We’re interested in next settings:

overlays — we copy the existing TOML file into etc/ directory of the release. Also, we can ref link it instead of copying by using :link atom.

config_providers — we use to make Distillery read configuration from etc/ directory of the release. Transforms options here can be used to transform values from TOML to Erlang’s terms.

To build a release from the root folder of the project execute statement below. Distillery will do all things for us including TOML transformation into erlang’s sys.config. Here Mix environment is set to prod and release configuration is tutorial_prod.

Release command

After this, you should see completion information and a description of some of the tasks available inside the release.

Release successfully created

Run app with the attached console by executing

sh _build/prod/rel/tutorial_toml/bin/tutorial_toml console

Run app with attached console

Now execute the function from the root module. In the result, we get a value from TOML config file. You can also find TOML config file in the release directory in etc/config_prod.toml.

We get value from the config file
You can find TOML file in etc directory of the release

Let’s change the value to something new.

New configuration value in TOML

To update value you don’t need to shut down the running app. Execute in the second console from the root directory of the project and wait while change will be applied.

sh _build/prod/rel/tutorial_toml/bin/tutorial_toml reload_config

Run config update
Config successfully updated.

Now let’s check again configuration value.

New value applied.

You can find an example project on Github

Thanks for reading!

--

--

Dmytro Nasyrov
Pharos Production

We build high-load software. Pharos Production founder and CTO.