LoginSignup
9
1

More than 1 year has passed since last update.

Elixir mix.exsのconfig_providersオプションで設定ファイルの読み込みをカスタマイズ

Last updated at Posted at 2022-12-09

ElixirMixはデフォルトでconfig/runtime.exsを読み込みます。さらに任意の設定ファイルも読み込みたい場合は、mix release:config_providersオプションで設定ファイル読み込みの挙動をカスタマイズすることができます。

Jason AxelsonさんがCode BEAM Americe 2022で披露されたNervesプロジェクトで:config_providersが使用されています。参考になります。

Nervesファームウエアで隠しファイルに書かれた設定を/dataフォルダから読み込むのに利用されているようです。

mix release:config_providersオプション

mix.exs
 defmodule HelloNerves.MixProject do
   use Mix.Project

   ...

   def project do
     [
       app: @app,
       version: @version,
       elixir: "~> 1.12",
       archives: [nerves_bootstrap: "~> 1.9"],
       start_permanent: Mix.env() == :prod,
       deps: deps(),
       releases: [{@app, release()}],
       preferred_cli_target: [run: :host, test: :host]
     ]
   end

   ...

   def release do
     [
+      config_providers: [
+        {Config.Reader, "/data/.target.secret.exs"}
+      ],
       overwrite: true,
       cookie: "#{@app}_cookie",
       include_erts: &Nerves.Release.erts/0,
       steps: [&Nerves.Release.init/1, :assemble],
       strip_beams: [keep: ["Docs"]]
     ]
   end

   ...
 end

Config.Provider behaviour

  • システム起動時に外部設定を読み込むAPI(init/1とload/2)
  • 通常、mix releaseで使用される

Config.Reader

自分で実装する

自分で実装するする場合は以下の二つの関数(Config.Provider callbacks)を実装します。

  • init/1
  • load/2

ご参考までに

9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1