21

The typical workflow to install a Gsettings Schema is this:

  1. Create a schema, with the extension .gschema.xml, with contents like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <schemalist>
      <schema id="com.companyname.appname" path="/com/companyname/appname/">
          <key type="b" name="mybool">
              <default>false</default>
              <summary>Example summary</summary>
              <description>Example description</description>
          </key>
      </schema>
    </schemalist>
    
  2. Copy the schema to /usr/share/glib-2.0/schemas/.

  3. Compile the schema by running this command:

    sudo glib-compile-schemas /usr/share/glib-2.0/schemas/
    

However, writing to /usr/share/glib-2.0/schemas/ requires root privileges. Is there a way to install a schema to a non-global user directory?

1 Answer 1

25

You can copy and compile your Gsettings Schemas to a user-writable directory:

$ cp com.companyname.appname ~/schemas/
$ glib-compile-schemas ~/schemas/

The tricky bit is to configure the application to use that particular directory. There are two ways to do this:

  • With the GSETTINGS_SCHEMA_DIR environment variable:

    $ GSETTINGS_SCHEMA_DIR=~/schemas/ ./example.py
    
  • Or using GSettingsSchemaSource and GSettingSchema objects:

    If you have access to the source code of your program, you can modify it to load compiled schemas from any directory. Here's how would do it in Python, although you could do this in any programming language with GObject introspection:

    schema_source = Gio.SettingsSchemaSource.new_from_directory(
        os.path.expanduser("~/schemas"),
        Gio.SettingsSchemaSource.get_default(),
        False,
    )
    schema = schema_source.lookup('com.companyname.appname', False)
    settings = Gio.Settings.new_full(schema, None, None)
    settings.set_boolean('mybool', True)
    

References:

2
  • Do you also know how to check if a given schema is already installed (and do the above only if it is not)? Jun 3, 2017 at 19:19
  • 3
    According to these docs you can also add the directory containing the glib-2.0/schemas directory to the XDG_DATA_DIRS environment variable.
    – ntc2
    Dec 21, 2018 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .