How to substitute OS variables into the configuration files?

When building a release, I need to substitute OS variables into the configuration files
I don’t understand how I can do this
I have this line in my file vm.args

-ssl_dist_optfile ./etc/inet_tls.conf
[
  {server, [
    {certfile, "./etc/${HOSTNAME}.crt"},

in the file I would like to make a substitution with a variable, but all I see is the original file, without the substitution with a variable

1 Like

You can replace relx’s parameter {vm_args, "./vm.args"} with {vm_args_src, "./vm.args.src"}

2 Likes

I am familiar with this technique, but I couldn’t get it to work

Start simple (I’ll expand on the technique @Led mentioned):

  1. rebar3 new release
  2. inside myapp (newly created folder) edit rebar.config
    2.1. remove vm_args and un-comment vm_args_src
  3. mv ./config/vm.args ./config/vm.args.src
    3.1. to use the file you’re now pointing to
  4. update config/vm.args.src to contain at least one variable
    4.1. let’s’ e.g. replace +A30 with +A${A}
  5. rebar3 release
    5.1. file _build/default/rel/myapp/releases/0.1.0/vm.args.src now contains +A${A}
    5.2. note: mind you the variables are only replaced once the executable is called, so don’t expect the created file to have them expanded directly
    5.2.1. file _build/default/rel/myapp/releases/0.1.0/vm.args does not exist yet
  6. A=30 _build/default/rel/myapp/bin/myapp
    6.1. this prints “Usage:” and expands to vm.args
    6.1.2. _build/default/rel/myapp/releases/0.1.0/vm.args is created with +A30
2 Likes

great explanation, I thought that vm.args would have the necessary substitutions after the release.
but this is not what I asked about, I wanted to make a replacement in the inet_tls.conf file during the build, I need to get a ready release with substitutions for a specific computer, I compile in github CI\CD and distribute the releases to different computers on the network.

but this is not what I asked about

I see. Kinda got confused by the original post where it seems you’re asking about vm.args, not some other generic file. I’m not sure rebar3 has a generic replacement mechanism, though…

1 Like

It does not, but that would perhaps be nice :smiley:

@heiheshang I think if you want to use os vars to build up a tls dist configuration, you should probably use -ssl_dist_opt options, such as -ssl_dist_opt server_certfile ${HOSTNAME}.crt that is specified in a startup script. You may also specify all these in ERL_FLAGS env var.

The reason what you’re trying to do doesn’t work is this file is read in via file:consult/1. Whereas, vm_args_src and sys_config_src, are a feature of relx in rebar releases, but as noted there is currently no counter part for tls dist options iirc.

However, you may specify the entire path to a cert file in an OS var and use os:getenv/1. Example :

[{server, [{certfile, os:getenv("NODE_CERT_FILE")}]}].

1 Like