Getting bad term error trying to set env variable in rebar.config

When I run rebar3 shell I get this error:
===> Error reading file rebar.config: 1: bad term

I’m trying to establish an environment variable with a default value at start time. Here’s my rebar.config file:

{erl_opts, [debug_info, {d, 'DBFOLDER', os:getenv('DBFOLDER', "database")}]}.

{cover_enabled, true}.

{deps, [rocksdb]}.

{plugins, [steamroller, {rebar3_dialyzer_html, "0.2.0"}]}.

{
    relx,
    [
        {release, {src, "0.1.0"}, [event_store, rocksdb, mnesia]},
        {mode, dev},
        {dev_mode, true},
        {include_erts, false},
        {extended_start_script, true},
        %% automatically picked up if the files
        %% exist but can be set manually, which
        %% is required if the names aren't exactly
        %% sys.config and vm.args
        {sys_config, "./config/sys.config"},
        {vm_args, "./config/vm.args"}
        %% the .src form of the configuration files do
        %% not require setting RELX_REPLACE_OS_VARS
        %% {sys_config_src, "./config/sys.config.src"},
        %% {vm_args_src, "./config/vm.args.src"}
    ]
}.

{
    profiles,
    [
        {
            prod,
            [
                %{erl_opts, [{d, 'DBFOLDER', os:getenv("DBFOLDER", "database")}]},
                {
                    relx,
                    [
                        %% prod is the default mode when prod
                        %% profile is used, so does not have
                        %% to be explicitly included like this
                        {mode, prod}
                        %% use minimal mode to exclude ERTS
                        %% {mode, minimal}
                    ]
                }
            ]
        }
    ]
}.

{
    steamroller,
    [
        {line_length, 100},
        {indent, 4},
        {inputs, ["rebar.config", "{src,test,include}/*.{[he]rl,app.src}"]}
    ]
}.

{dialyzer, [{output_format, raw}]}.

1 Like

Rebar.config is a static Erlang term, you cannot invoke any functions here. What you need is this:

As a side note, hardcoding database path into your code at the build time isn’t portable at all. It’s better to read this OS environment variable in the runtime, I usually do it in the application start callback: Erlang -- application.

3 Likes

P.S. With Lee library you can completely automate reading OS environment variable and setting it to the application environment variable:

model() ->
  #{ db =>
       #{ directory => 
            {[value, os_env, app_env],
             #{ type => string()
              , default => "database"
              , os_env => "DBFOLDER"
              , app_env => {my_cool_app, db_path}
              }}
        }
    }.
2 Likes

2 posts were split to a new topic: Off topic posts from bad term error thread