--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -471,14 +471,16 @@ endif()
 
 #### Find socket directories
 if(UNIX)
-    if(NOT $ENV{TMPDIR} STREQUAL "")
-        set(DBUS_SESSION_SOCKET_DIR $ENV{TMPDIR})
-    elseif(NOT $ENV{TEMP} STREQUAL "")
-        set(DBUS_SESSION_SOCKET_DIR $ENV{TEMP})
-    elseif(NOT $ENV{TMP} STREQUAL "")
-        set(DBUS_SESSION_SOCKET_DIR $ENV{TMP})
-    else()
-        set(DBUS_SESSION_SOCKET_DIR /tmp)
+    if(NOT DEFINED DBUS_SESSION_SOCKET_DIR)
+        if(NOT $ENV{TMPDIR} STREQUAL "")
+            set(DBUS_SESSION_SOCKET_DIR $ENV{TMPDIR})
+        elseif(NOT $ENV{TEMP} STREQUAL "")
+            set(DBUS_SESSION_SOCKET_DIR $ENV{TEMP})
+        elseif(NOT $ENV{TMP} STREQUAL "")
+            set(DBUS_SESSION_SOCKET_DIR $ENV{TMP})
+        else()
+            set(DBUS_SESSION_SOCKET_DIR /tmp)
+        endif()
     endif()
 endif()
 
