Override CMake preset environment variable from the outside

Take this simple set up of CMake presets.

{
  "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).

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

# 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

# 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.

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):

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.