Mastering GSettings: glib-compile-schemas on Ubuntu

Overview

In Ubuntu and modern GNOME desktop environments, GSettings provides a robust framework for managing application preferences. Central to this architecture is the glib-compile-schemas utility, which compiles human-readable XML schema definitions into high-performance binary caches. This practical guide covers writing custom schema definitions, compiling them into binary format, and manipulating configuration keys using the standard gsettings command-line interface.

1. Installing the GLib Schema Compiler and Reviewing Default Paths

Before diving in, refresh your package repository index and clean up any lingering library states with sudo apt-get update -qq && sudo apt-get install -y libglib2.0-bin-.

sudo apt-get update -qq && sudo apt-get install -y libglib2.0-bin-

To provision the glib-compile-schemas executable along with essential GLib command-line utilities, install the requisite binary package using sudo apt-get install -y libglib2.0-bin.

sudo apt-get install -y libglib2.0-bin

Under the hood, glib-compile-schemas parses XML-based schema declarations (.gschema.xml) and compiles them into an optimized binary database named gschemas.compiled, enabling near-instantaneous preference lookups at runtime.
⚙️ [Key Command Options]

--targetdir=DIRECTORY : Sets the destination folder where the generated gschemas.compiled file will be stored.
--strict : Enforces strict parsing rules by immediately terminating on any schema warning or syntax issue.
--dry-run : Performs comprehensive syntax validation on target XML files without writing any binary cache to disk.
--allow-any-name : Relaxes strict naming convention validations for schema identifiers and paths.

glib-compile-schemas –help

To examine where the operating system stores its default, system-wide GSettings schemas, inspect the system repository with ls -l /usr/share/glib-2.0/schemas/ | head -n 10.

ls -l /usr/share/glib-2.0/schemas/ | head -n 10

2. Defining and Validating a Custom GSettings XML Schema

Create a clean, isolated scratch workspace to host our custom configuration experiment by running mkdir -p /tmp/custom_schema && cd /tmp/custom_schema.

mkdir -p /tmp/custom_schema && cd /tmp/custom_schema

Generate a new schema definition file named org.example.myapp.gschema.xml declaring a string key (theme-mode) and an integer key (window-width) using cat << 'EOF' > org.example.myapp.gschema.xml.

![cat « ‘EOF’ > org.example.myapp.gschema.xml

Prior to producing binary output, verify that the schema adheres to valid XML and GLib schema specifications by running glib-compile-schemas --dry-run /tmp/custom_schema.

glib-compile-schemas –dry-run /tmp/custom_schema

Confirm that the validation check completed successfully without emitting errors by printing the process return code with echo "Exit status: $?".

echo “Exit status: $?”

3. Compiling Binary Schema Caches with glib-compile-schemas

Ensure you are inside the staging directory containing your XML schema definitions by executing cd /tmp/custom_schema.

cd /tmp/custom_schema

Compile all .gschema.xml files in /tmp/custom_schema into a single binary index by running glib-compile-schemas /tmp/custom_schema.
Upon successful compilation, the system creates the binary cache file gschemas.compiled directly inside the target directory.

glib-compile-schemas /tmp/custom_schema

Inspect the size, ownership, and filesystem permissions of the freshly minted cache using ls -lh gschemas.compiled.
The output confirms that a 324-byte compiled binary file has been generated properly.

ls -lh gschemas.compiled

Verify that the artifact is recognized as a valid compiled GLib schema table rather than corrupted data by running file gschemas.compiled.
This utility analyzes file headers to ensure the binary payload conforms to the expected internal layout.

file gschemas.compiled

4. Querying and Modifying Custom Settings Using gsettings

To instruct the gsettings command-line client to look up our local custom schemas rather than just default system paths, export the directory with export GSETTINGS_SCHEMA_DIR=/tmp/custom_schema.

export GSETTINGS_SCHEMA_DIR=/tmp/custom_schema

Confirm that the active session recognizes your new schema namespace by running gsettings list-schemas | grep org.example.myapp.
The filtered output verifies that org.example.myapp is successfully registered and available.

gsettings list-schemas | grep org.example.myapp

List all keys exposed by the custom schema by executing gsettings list-keys org.example.myapp.
Both predefined keys—theme-mode and window-width—are visible and accessible.

gsettings list-keys org.example.myapp

Check the current value assigned to theme-mode by running gsettings get org.example.myapp theme-mode.
The utility returns 'dark', matching the default setting defined in the XML blueprint.

gsettings get org.example.myapp theme-mode

Update the setting to a new string value of 'light' by issuing gsettings set org.example.myapp theme-mode 'light'.

gsettings set org.example.myapp theme-mode ’light’

Validate that the modification took effect immediately by querying the key again with gsettings get org.example.myapp theme-mode.
The query confirms the updated state has switched to 'light'.

gsettings get org.example.myapp theme-mode

This walkthrough demonstrated how GSettings operates on Ubuntu, from crafting XML specifications to compiling optimized binary caches and managing keys through standard command-line tools. Always recompile schemas whenever your definitions change to keep desktop configuration management smooth, robust, and automated.