Feature Request: Add env_file option to CMake Command Mode

Hello!

I propose adding a new option to cmake -E that takes a file of environment variables to set, much like cmake -E env.

Example:

.env

THIS=THAT
MY_PATH=/path/to/thing
DEBUG_MODE=yes
cmake -E env_file .env my_command

I often find myself adding large lists of environment variables to cmake -E env, and thought there could be a better solution.

I threw together a working example here:
https://gitlab.kitware.com/WhoBrokeTheBuild/cmake/-/commit/96dacb3ea4cf7676cbfff717908edd64e71bae2b

1 Like

I think something like this could instead be added directly to cmake -E env as a --from-file= option or something like that. Other than bikeshedding the name of the option, the main design question is the format of the file itself:

  • How to unset variables? Maybe each line can be --unset=FOO to unset and FOO= to set, almost like a response file.
  • How to reference existing variables to be equivalent to cmake -E env "VAR=prefix:$VAR:suffix"? When that appears on the command line, the shell replaces $VAR before we even see it. In the file we’d need a syntax for that, and a way to escape it.

Are there existing formats for this used by other tools?

1 Like

That sounds like a great idea! I was trying to think through some of the limitations of the env_file approach myself, like specifying envs both individually and from a file.

One that comes to mind is:

node seems to use the same format as well.

Thanks for your quick reply!

The docker format looks straightforward. The only drawback is that it doesn’t support referencing existing values. That’s probably okay for many use cases. To leave room for other formats later, we should indicate a format name or version in the option name, e.g. --from-file-v1=. Other name ideas, anyone?

If you’d like to work on this, please see CONTRIBUTING.rst.

1 Like

I’ve submitted a Merge Request here:
https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5596

I read through all the contributing guidelines, but if I missed something please let me know and I’ll change it.

Thanks for the help!

There is also systemd’s format: systemd.exec