Qt resetting CMAKE_BUILD_TYPE?

I am successfully overriding a package from nixpkgs to include debug symbols, by using

    geant4 = previous.geant4.overrideAttrs (old: {
      dontStrip = true;
      cmakeBuildType = "RelWithDebInfo";
    });

The effect of this override is visible in the nix log for the build:

[...]
configuring
fixing cmake files...
cmake flags: [...] -DCMAKE_BUILD_TYPE=RelWithDebInfo [...]
click to see more context
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/q658gq9fm3c0yc77alif5cq9j2iz18yd-geant4-v11.0.4.tar.gz
source root is geant4-v11.0.4
setting SOURCE_DATE_EPOCH to timestamp 1678723909 of file geant4-v11.0.4/source/visualization/modeling/src/G4VisTrajContext.cc
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
fixing cmake files...
cmake flags: -DCMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY=OFF -DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LOCALEDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/share/locale -DCMAKE_INSTALL_LIBEXECDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/libexec -DCMAKE_INSTALL_LIBDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/lib -DCMAKE_INSTALL_DOCDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/share/doc/geant4 -DCMAKE_INSTALL_INFODIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/share/info -DCMAKE_INSTALL_MANDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/share/man -DCMAKE_INSTALL_OLDINCLUDEDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/include -DCMAKE_INSTALL_INCLUDEDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/include -DCMAKE_INSTALL_SBINDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/sbin -DCMAKE_INSTALL_BINDIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/bin -DCMAKE_INSTALL_NAME_DIR=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4/lib -DCMAKE_POLICY_DEFAULT_CMP0025=NEW -DCMAKE_OSX_SYSROOT= -DCMAKE_FIND_FRAMEWORK=LAST -DCMAKE_STRIP=/nix/store/d9fndiing52fkalp5knfalrvlb3isi6w-gcc-wrapper-12.2.0/bin/strip -DCMAKE_RANLIB=/nix/store/d9fndiing52fkalp5knfalrvlb3isi6w-gcc-wrapper-12.2.0/bin/ranlib -DCMAKE_AR=/nix/store/d9fndiing52fkalp5knfalrvlb3isi6w-gcc-wrapper-12.2.0/bin/ar -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=/nix/store/pr0l843zwib31r6nr54ipwjfcp08cq4a-geant4-11.0.4 -DGEANT4_INSTALL_DATA=OFF -DGEANT4_USE_GDML=ON -DGEANT4_USE_G3TOG4=ON -DGEANT4_USE_QT=OFF -DGEANT4_USE_XM=OFF -DGEANT4_USE_OPENGL_X11=ON -DGEANT4_USE_INVENTOR=OFF -DGEANT4_USE_PYTHON=OFF -DGEANT4_USE_RAYTRACER_X11=OFF -DGEANT4_USE_SYSTEM_CLHEP=ON -DGEANT4_USE_SYSTEM_EXPAT=ON -DGEANT4_USE_SYSTEM_ZLIB=ON -DGEANT4_BUILD_MULTITHREADED=OFF

This is the only occurrence of CMAKE_BUILD_TYPE in the whole log.

However, if I override the derivation’s arguments to include a dependency on Qt, the debug symbols disappear. The corresponding nix log now shows

[...]
configuring
fixing cmake files...
cmake flags: [...] -DCMAKE_BUILD_TYPE=Release [...]

The full log now starts with two lines which were not there before:

@nix { "action": "setPhase", "phase": "qtPreHook" }
qtPreHook

The definition of qtPreHook doesn’t obviously look as if it might be interfering:

qtPreHook() {
    # Check that wrapQtAppsHook is used, or it is explicitly disabled.
    if [[ -z "$__nix_wrapQtAppsHook" && -z "$dontWrapQtApps" ]]; then
        echo >&2 "Error: wrapQtAppsHook is not used, and dontWrapQtApps is not set."
        exit 1
    fi
}

but I’m guessing that something dragged in by Qt is setting CMAKE_BUILD_TYPE to Release.

What can be done to ensure that the package is built with debug symbols?

The culprit seems to be here:

I am at a loss as to what to do about it.

I just ran into this myself. You can use configurePhase to run after the hook.

  configurePhase = let
    cmakeBuildType = if debug
                     then "Debug"
                     else "RelWithDebInfo";
  in ''
    cmakeBuildType=${cmakeBuildType} # qt6 setup hook resets this for some godforsaken reason
    cmakeConfigurePhase
  '';