How to set GNOME keyboard layout programatically

The Ubuntu installer ubiquity should configure the GNOME keyboard layout to the keyboard layout that the user selects during installation. What is the canonical way to configure the keyboard layout in GNOME?

The only reliable way I found is:

  1. Get the configured input sources. Example:
$ gsettings get org.gnome.desktop.input-sources sources
[('xkb', 'us'), ('xkb', 'gb')]
  1. Set the input sources to only the desired layout. Example:
gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'de')]"
  1. Add the previously configured input sources back. Example:
gsettings set org.gnome.desktop.input-sources sources "[('xkb', 'de'), ('xkb', 'us'), ('xkb', 'gb')]"

This solution looks dirty to me (it is not race-free). Is this the recommended way to set the keyboard layout or is there a better way?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.