pythoncmakegnomegio

Gio.Settings behaviour changing with environment


I have a CMake project where I want to execute a Python script as part of the install target to automatically set a global keyboard shortcut for the target that gets installed.

The script itself works when started from the terminal but when I add it to the install target like so

install(
  CODE
  "execute_process(
    COMMAND  /usr/bin/python3 \"${CMAKE_SOURCE_DIR}/tools/enable-shutdown-menu-keybind.py\" --command \"${CMAKE_INSTALL_FULL_BINDIR}/shutdown_menu\"
    RESULT_VARIABLE _ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT
  )
  if (_ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT GREATER 0)
    message(FATAL_ERROR \"Enabling shutdown-menu keybinds failed with exit code \${_ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT}\")
  endif()"
  CONFIGURATIONS Release RelWithDebInfo MinSizeRel
)

it suddenly fails. In particular it seems to fail when getting the list of custom keybinds from org.gnome.settings-daemon.plugins.media-keys like you would with gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings.

I don't think it's a problem resulting from running cmake with elevated rights in order to install the program as running the script from a terminal using sudo works as expected aswell.

What i've tried so far:

My guess is that it has something todo with the environment (which is why I tried the second point) but I'm not sure what I would be missing.

EDIT:

I just thought of something else I could test. I temporarily set the CMAKE_INSTALL_PREFIX to a directory owned by me and ran the cmake install command without elevated rights. To my surprise the script ran prefectly fine.

I guess this means that it has something to do with running cmake as root. In case its important, i elevate cmake using pkexec.

EDIT 2:

It seems I was wrong about sudo - it fails there too. It did work when I launched the script using a VSCode launch configuration with the "sudo": true option tho.

EDIT 3:

I've expanded the install function call to the following:


install(
  CODE
  "if (DEFINED ENV{PKEXEC_UID})
    set(UID \$ENV{PKEXEC_UID})
  elseif(DEFINED ENV{SUDO_UID})
    set(UID \$ENV{SUDO_UID})
  else()
    message(FATAL_ERROR \"Unable to determine callee UID.\")
  endif()
  execute_process(
    COMMAND sudo -H -u \"#\${UID}\" env DISPLAY=$ENV{DISPLAY} /usr/bin/python3 \"${CMAKE_SOURCE_DIR}/tools/enable-shutdown-menu-keybind.py\" --command \"${CMAKE_INSTALL_FULL_BINDIR}/shutdown_menu\"
    RESULT_VARIABLE _ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT
  )
  if (_ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT GREATER 0)
    message(FATAL_ERROR \"Enabling shutdown-menu keybinds failed with exit code \${_ENABLE_SHUTDOWN_MENU_KEYBIND_RESULT}\")
  endif()"
)

which leads to the program getting executed and reporting to have set the values. But the changes do not seem to get applied, as the keybind shows up neither in dconf, the gnome-command-center nor gsettings.


Solution

  • GSettings is per-user, not per system. So running the settings script as root is only going to affect the settings for the root user, not your user or the user who tries to use your software.

    You might want to look at the alternative of setting the custom keybinding when your program is first run, as that means it’ll be running as the target user. You probably also want to ask the user if they want their keybindings changed first, as unilaterally changing a user’s desktop settings could be considered quite rude.