0

I wrote the following script to enable certain GNOME shell extensions. I noticed that this script will return different behaviours when it is executed as "sudo" as compared to "ordinary user".

As Ordinary User: From terminal, after issuing python3.6 scriptname.py, this script will make the necessary settings changes, the effects will be visible, and the changes will be registered in the "dconf Editor", i.e. its /org/gnome/shell/enabled-extensions tab will contain the values of GS_ENABLED_EXTENSIONS. When I manually re-issued the command Alt+F2+r+Return, the enabled extensions remains in effect.

As sudo: First, I used the dconf Editor to reset /org/gnome/shell/enabled-extensions to it's original value. Next, from the terminal, after issuing sudo python3.6 scriptname.py, the script's setting will take effect and the setting changes will be visible. However, when I re-opened the dconf Editor, i.e. /org/gnome/shell/enabled-extensions, I noticed the values of GS_ENABLED_EXTENSIONS will not appear there. In fact, it's original value still appears. Now, when I manually re-issued the command Alt+F2+r+Return, the enabled extensions settings by the script will be visually lost, and visually the GNOME shell will show the extensions in the dconf Editor, i.e. it's original values.

Question: Why does the gsettings set command behave differently when it is executed as sudo as compared to an "ordinary user"? Is there a way for sudo to return the same behavior as an ordinary user?

#!/usr/bin/python3.6
# -*- coding: utf-8 -*-

import sys
import ast
import time
from subprocess import run, PIPE


INSTALLED_GSEXTENSIONS = ['user-theme@gnome-shell-extensions.gcampax.github.com',
                          'workspace-indicator@gnome-shell-extensions.gcampax.github.com']


def set_gs_enabled_extensions():
    global GS_ENABLED_EXTENSIONS, CMD

    print( '\nEnabling GNOME shell extensions...' )
    #Commands
    get_enabled_extensions = [ 'gsettings', 'get', 'org.gnome.shell', 'enabled-extensions' ]
    set_enabled_extensions = [ 'gsettings', 'set', 'org.gnome.shell', 'enabled-extensions' ]

    #Get existing GS enabled extensions (if any).
    enabled_extensions = run( get_enabled_extensions, stdout=PIPE ).stdout.decode().rstrip()
    GS_ENABLED_EXTENSIONS = set( ast.literal_eval( enabled_extensions ) )

    #Enable existing and installed GS extensions.
    GS_ENABLED_EXTENSIONS.update( INSTALLED_GSEXTENSIONS )
    begin=' '.join( set_enabled_extensions )
    CMD=f'{begin} "{[*GS_ENABLED_EXTENSIONS,]}"'
    print( '\nCMD=', CMD )
    run( CMD , shell=True, stdout=sys.stdout )
    print( 'Enabling GNOME shell extensions... Done' )


def restart_gnome_shell():
    print( '\nRestarting GNOME shell ...' )
    cmd = 'xdotool key "Alt+F2+r" && sleep 0.5 && xdotool key "Return"'
    run( cmd, shell=True, stdout=sys.stdout )
    print( '\Restarting GNOME shell ... Done.' )


def main():
    set_gs_enabled_extensions()
    restart_gnome_shell()


if __name__ == "__main__":
  main()
0

dconf settings are stored in a database on a per-user basis. When you run a command with sudo, the $USER is root, not anymore your own user. gsettings will therefore act on the dconf database of the root user if it exists, rather than on the dconf database of a specific user.

An in depth guide for system administrators about dconf is available on the gnome wiki. It suggests that the database on which dconf acts can be set by the DCONF_PROFILE environment variable.

  • Can you also explain what happens when Alt+F2+r+Return is executed? Btw do you know what is "the schema vendor override system as part of GSettings' mentioned in the link that you had provided? – Sun Bear Sep 5 at 13:15
  • Can you tell me where I can find the "dconf database of the root user"? – Sun Bear Sep 5 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.