teamon

teamon

Runtime configuration only for production

Context

I have a small team working on one elixir application.

Every developer runs the app locally.

The app is built with mix release inside docker on CI and deployed as a container.

Over time, we’ve settled on this approach to configuration files:

  • config/config.exs provides a common build-time configuration base for all dev/test/prod
  • config/dev.exs provides a build-time configuration for all developers
  • config/dev.local.exs gives a handy way to override development config locally - each developer can put whatever they need in there (per-user config, developer secrets, etc.), this file is put into .gitignore
  • config/test.exs provides all necessary configuration to run tests - this is to ensure that mix test can be run right after git clone without any configuration necessary
  • config/prod.exs provides build-time config for production
  • config/releases.exs provides runtime config for production, which is essentially ENV variables mapping with System.fetch_env!/1 so the app fails to start when some configuration is missing

Config files examples

Click to expand
# config/config.exs - common build-time config for all
use Mix.Config

config :phoenix, :json_library, Jason

import_config "#{Mix.env()}.exs"
# config/dev.exs - all config for development
use Mix.Config

config :myapp, MyAppWeb.BasicAuth, user: "", pass: ""

import_config "dev.local*.exs"
# config/prod.exs - build-time config for production
use Mix.Config

config :myapp, MyAppWeb.Endpoint, server: true
# config/test.exs - all config for test
use Mix.Config

config :myapp, MyAppWeb.Endpoint, server: false
config :myapp, MyAppWeb.BasicAuth, user: "test", pass: "test"
# config/dev.local.exs - local config for development, gitignored
use Mix.Config

config :myapp, MyAppWeb.Endpoint, url: [host: "alice-dev-proxy.example.com"]
config :myapp, MyAppWeb.BasicAuth, user: "", pass: ""
# config/releases.exs - runtime config for production
import Config

config :myapp, MyAppWeb.BasicAuth,
  user: System.fetch_env!("ADMIN_USER"),
  pass: System.fetch_env!("ADMIN_PASS")

The problem

The new preferred way of runtime configuration is to use config/runtime.exs.

This file will be executed regardless if run with mix or as a part of the release.

Now, if I simply rename my config/releases.exs to config/runtime.exs it will blow up in dev & test due to System.fetch_env!/1 usage. Nobody have all the ENV values referenced locally (a lot of the are secrets).

I can’t seem to find any reference on how to deal with such case.

Any ideas?

Bonus question

Another, somehow-related issues is using .exs files as runtime configuration.

With elixir 1.10 I could add import_config "foo.exs" at the bottom of config/releases.exs and then mount foo.exs file on a production system at /app/releases/0.1.0/foo.exs (docker on kubernetes via ConfigMap).

With elixir 1.11 it now fails with ** (RuntimeError) import_config/1 is not enabled for this configuration file. Some configuration files do not allow importing other files as they are often copied to external systems, which seems to be a bit ironic, as I do want to “copy to external system” :upside_down:

The question here is: I DO want to use runtime-mounted .exs file as a configuration (alongside ENV vars) simply because it is much better than yaml, but it seems to be discouraged.

Most Liked

wojtekmach

wojtekmach

Hex Core Team

at hexpm we ended up with the following:

# config/runtime.exs
import Config

if config_env() == :prod do
  config :hexpm, ...
end

[1] https://github.com/hexpm/hexpm/blob/5b8663/config/runtime.exs

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement