# Presets - combine env/cache variables from multiple presets

**URL:** https://discourse.cmake.org/t/presets-combine-env-cache-variables-from-multiple-presets/15369
**Category:** Usage
**Created:** [December 7, 2025, 11:26am UTC](https://discourse.cmake.org/t/presets-combine-env-cache-variables-from-multiple-presets/15369 "2025-12-07T11:26:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Andrej730](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrej730/32/5752_2.png) [@Andrej730](https://discourse.cmake.org/u/Andrej730)
#### Post date: [December 7, 2025, 11:26am UTC](https://discourse.cmake.org/t/presets-combine-env-cache-variables-from-multiple-presets/15369/1 "2025-12-07T11:26:25Z")

</div>

Is it possible to somehow combine the same env/cache variable from two presets into a single value? Ideally, the goal is to define `CMAKE_PREFIX_PATH` cache variable based on values from multiple presets. Or at least define `$env{CMAKE_PREFIX_PATH}` that way.

1. From reading documentation there seems to be no way to reference one variable from another inside preset.
2. For environment variables `$env{CMAKE_PREFIX_PATH}` works, but since it doesn’t allow circular references you can only save this value to a different variable, not `CMAKE_PREFIX_PATH`.  
Using `$penv{CMAKE_PREFIX_PATH}` is accessing process environment variables, so it’s not very helpful for this case.

```sh
$env:CMAKE_PREFIX_PATH="42"
cmake --preset default
# Preset CMake variables:
# 
# DEPENDENCY_PATH="L:/path/to/second/dependency"
# 
# Preset environment variables:
# 
# CMAKE_PREFIX_PATH="L:/path/to/first/dependency"
# CMAKE_PREFIX_PATH_env1="L:/path/to/second/dependency;L:/path/to/first/dependency"
# CMAKE_PREFIX_PATH_env2="L:/path/to/second/dependency;42"

```

CMakeUserPresets.json

```json
{
  "version": 10,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "$comment": "An example CMakePresets.json file",
  "include": [],
  "configurePresets": [
    {
      "name": "default",
      "inherits": ["dependency"],
      "binaryDir": "${sourceDir}/build_",
      "cacheVariables": {
        "DEPENDENCY_PATH": "L:/path/to/second/dependency"
      },
      "environment": {
        "CMAKE_PREFIX_PATH_env1": "L:/path/to/second/dependency;$env{CMAKE_PREFIX_PATH}",
        "CMAKE_PREFIX_PATH_env2": "L:/path/to/second/dependency;$penv{CMAKE_PREFIX_PATH}"
      }
    },
    {
      "name": "dependency",
      "cacheVariables": {
          "DEPENDENCY_PATH": "L:/path/to/first/dependency"
        },
      "environment": {
        "CMAKE_PREFIX_PATH": "L:/path/to/first/dependency"
      }
    }
  ]
}

```

---

<div class="post-metadata">

### Author: ![Chardrazle](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chardrazle/32/4362_2.png) [@Chardrazle](https://discourse.cmake.org/u/Chardrazle)
#### Post date: [December 24, 2025, 10:42am UTC](https://discourse.cmake.org/t/presets-combine-env-cache-variables-from-multiple-presets/15369/2 "2025-12-24T10:42:59Z")

</div>

Not sure how much decoupling of those sections you require, but it’s possible to combine env-vars so long as they have different names. Then set the cache variable from the final result.

```auto
{
  "version": 10,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "$comment": "An example CMakePresets.json file",
  "include": [],
  "configurePresets": [
    {
      "name": "default",
      "inherits": ["dependency"],
      "binaryDir": "${sourceDir}/build_",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{PRIVATE_PREFIX_PATH}"
      },
      "environment": {
        "PRIVATE_PREFIX_PATH": "L:/path/to/second/dependency;$env{PRIVATE_PREFIX_PATH_OTHER}"
      }
    },
    {
      "name": "dependency",
      "environment": {
        "PRIVATE_PREFIX_PATH_OTHER": "L:/path/to/first/dependency"
      }
    }
  ]
}

```
