`cmakeFlags` are currently passed to CMake as follows:
[pkgs/development/tools/build-managers/cmake/setup-hook.sh](https://github.com/NixOS/nixpkgs/blob/b6236dbddb6852a9567588f1a87ba2521bb71e58/pkgs/development/tools/build-managers/cmake/setup-hook.sh#L121-L122)
```
cmake ${cmakeDir:-.} $cmakeFlags "${cmakeFlagsArray[@]}"
```
so Bash will tokenize them on spaces and there is no way to prevent it.
The only alternative is adding the flag to `cmakeFlagsArray` in a Bash code. For example:
```
preConfigure = ''
cmakeFlagsArray+=(
"-DSOMELIB_CXX_FLAGS=\"-O3 -march=native\""
)
'';
```
In the future, this might be possible from Nix through [__structuredAttrs](https://github.com/NixOS/nixpkgs/pull/72074).
{
makeFlags = [
# FIXME stdenv.mkDerivation fails to preserve the space after "-fcommon"
#''EXTRA_CFLAGS="-fcommon -DTARGET_FORMAT=${targetFormat}"''
# no. in bash, the backslash is a separate char of the string
#"EXTRA_CFLAGS=-fcommon\\ -DTARGET_FORMAT=${targetFormat}"
];
preConfigure = ''
# no. gcc: error: unrecognized command-line option '-fcommon -DTARGET_FORMAT=AVM_LE'
#makeFlagsArray+=('EXTRA_CFLAGS="-fcommon -DTARGET_FORMAT=${targetFormat}"')
makeFlagsArray+=('EXTRA_CFLAGS=-fcommon -DTARGET_FORMAT=${targetFormat}')
'';
https://github.com/NixOS/nixpkgs/issues/114044
https://discourse.nixos.org/t/cmakeflags-and-spaces-in-option-values/20170
https://github.com/NixOS/nixpkgs/pull/175649
workaround