# Override CMake preset environment variable from the outside

**URL:** https://discourse.cmake.org/t/override-cmake-preset-environment-variable-from-the-outside/10394
**Category:** Usage
**Created:** [March 18, 2024, 3:26pm UTC](https://discourse.cmake.org/t/override-cmake-preset-environment-variable-from-the-outside/10394 "2024-03-18T15:26:45Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![gtsc](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/9fc29f/32.png) [@gtsc](https://discourse.cmake.org/u/gtsc)
#### Post date: [March 18, 2024, 3:26pm UTC](https://discourse.cmake.org/t/override-cmake-preset-environment-variable-from-the-outside/10394/1 "2024-03-18T15:26:45Z")

</div>

Take this simple set up of CMake presets.

```auto
{
  "version": 6,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 27,
    "patch": 0
  },
  "configurePresets": [
    {
      "name": "base-preset",
      "hidden": true,
      "inherits": [],
      "environment": {
        "QT_ROOT": "/base/location/of/qt"
      },
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{QT_ROOT}/lib/cmake"
      }
    },
    {
      "name": "test-preset",
      "inherits": ["base-preset"],
      "environment": {
        "QT_ROOT": "/actual/location/of/qt"
      }
    }
  ],
  "buildPresets": []
}

```

I am wondering if there is a way to essentially “override” the environment variable `QT_ROOT` via the command line. This would be useful as particular users would want to set `QT_ROOT` differently than specified in `test-preset`.  
I know that the environment variables specified in the preset will always take precedence over the ones in the process’ environment (see [https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#configure-preset](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#configure-preset)).

In simple terms, executing `QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset`, I would expect the following output:

```auto
# QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset
Preset CMake variables:

  CMAKE_PREFIX_PATH="/user-defined/location/of/qt/lib/cmake"

Preset environment variables:

  QT_ROOT="/user-defined/location/of/qt"

```

but I get

```auto
# QT_ROOT="/user-defined/location/of/qt" cmake --preset test-preset
Preset CMake variables:

  CMAKE_PREFIX_PATH="/actual/location/of/qt/lib/cmake"

Preset environment variables:

  QT_ROOT="/actual/location/of/qt"

```

I am also aware of using `$penv{}` instead of `$env()` in defining the cache variable, but I would want to retain the ability to define a sensible default as part of the `environment` block in the preset.

Currently, I am making use of a user preset that inherits of `test-preset` and overrides `QT_ROOT` that way, however this solution becomes tedious when there are many presets.  
In that case, I would need a user preset per preset I would want to use, just to change an environment variable.

---

<div class="post-metadata">

### Author: ![nickvdbroeck](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/n/85f322/32.png) [@nickvdbroeck](https://discourse.cmake.org/u/nickvdbroeck)
#### Post date: [July 31, 2025, 10:26am UTC](https://discourse.cmake.org/t/override-cmake-preset-environment-variable-from-the-outside/10394/2 "2025-07-31T10:26:24Z")

</div>

Dear @gtsc,

I see nobody answered your question in the past. I was searching for the same thing (I think), so I tought to revive this old topic and share what I found. Even if no longer relevant to you, perhaps it will be relevant to a passerby searching for the same thing.

Overriding CMakePresets.json values can be done in two was (at least):

- The first option should be the CMakeUserPresets.json as you mention. That allows you to define it only once and you don’t need to think about it anymore. But yes, that can get tedious if you have a lot of those settings. (There has been a discussion for some years now on this topic: [https://gitlab.kitware.com/cmake/cmake/-/issues/22538](https://gitlab.kitware.com/cmake/cmake/-/issues/22538))
- Second, You can override values from the commandline as indicated here: [https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-preset](https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-preset). So for example you could write: `cmake --preset test-preset -DQT_ROOT="/user-defined/location/of/qt"` and that should give you the result you are looking for.

A third option that came to mind was using the cache. If variables are defined with CACHE, then the value in the cache will overwrite the value in the CMakeSettings files. However, I can’t find any reference that this will also work with presets (on the contrary, I see message that suggest this does not work). But it might be worth testing it out.

If all this does not cover your use case, it is always possible to define your own QT\_USER\_ROOT variable and add logic to the cmake file that if this variable is defined you use that instead of the QT\_ROOT from the presets. This new variable is then not defined by default, so you can cache it or put it in your environment or anywhere else.
