# Feature Request: Add env\_file option to CMake Command Mode

**URL:** https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344
**Category:** Development
**Created:** [December 10, 2020, 8:05pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344 "2020-12-10T20:05:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![WhoBrokeTheBuild](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whobrokethebuild/32/1017_2.png) [@WhoBrokeTheBuild](https://discourse.cmake.org/u/WhoBrokeTheBuild)
#### Post date: [December 10, 2020, 8:05pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/1 "2020-12-10T20:05:47Z")

</div>

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`

```auto
THIS=THAT
MY_PATH=/path/to/thing
DEBUG_MODE=yes

```

```auto
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](https://gitlab.kitware.com/WhoBrokeTheBuild/cmake/-/commit/96dacb3ea4cf7676cbfff717908edd64e71bae2b)

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 10, 2020, 8:34pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/2 "2020-12-10T20:34:57Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![WhoBrokeTheBuild](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whobrokethebuild/32/1017_2.png) [@WhoBrokeTheBuild](https://discourse.cmake.org/u/WhoBrokeTheBuild)
#### Post date: [December 10, 2020, 8:38pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/3 "2020-12-10T20:38:23Z")

</div>

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:

> **[Declare default environment variables in file](https://docs.docker.com/compose/env-file/)**
>
> Declare default environment variables in a file

node seems to use the same format as well.

Thanks for your quick reply!

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 10, 2020, 8:43pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/4 "2020-12-10T20:43:47Z")

</div>

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](https://gitlab.kitware.com/cmake/cmake/-/blob/master/CONTRIBUTING.rst).

---

<div class="post-metadata">

### Author: ![WhoBrokeTheBuild](https://discourse.cmake.org/user_avatar/discourse.cmake.org/whobrokethebuild/32/1017_2.png) [@WhoBrokeTheBuild](https://discourse.cmake.org/u/WhoBrokeTheBuild)
#### Post date: [December 10, 2020, 8:57pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/5 "2020-12-10T20:57:58Z")

</div>

I’ve submitted a Merge Request here:  
[https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/5596](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!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 11, 2020, 2:29pm UTC](https://discourse.cmake.org/t/feature-request-add-env-file-option-to-cmake-command-mode/2344/6 "2020-12-11T14:29:46Z")

</div>

> [@brad.king](#):
>
> Are there existing formats for this used by other tools?

There is also systemd’s format: [systemd.exec](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=)
