Strange xkbOptions behavior (GNOME)

I’ve been trying to figure this out for a long time and don’t know where to look anymore.

I have the following declarations in the Nix configuration file:

  services.xserver = {
    enable = true;

    displayManager.gdm.enable = true;
    desktopManager.gnome.enable = true;

    layout = "us,ru";
    xkbVariant = "";
    xkbOptions = "grp:alt_shift_toggle";

    libinput = {
      enable = true;  

      touchpad.disableWhileTyping = true;     
    };

    excludePackages = with pkgs; [
      xterm
    ];
  };

The problem
Pressing ALT + SHIFT once to switch from the us to ru layout seemingly changes nothing, and Latin characters are still being inputted. If I press ALT + SHIFT once again, however, cyrillic characters start being inputted. It’s as if there was another layout with Latin characters between the us and the ru ones. To switch back from ru to us, it is enough to press ALT + SHIFT just once. The problem is that it is very inconsistent.

Another thing to note is that even when switched to ru, the status bar (see attached screenshot) indicates “en”.
image

I read that GNOME overrides xkbOptions. Is the problem in dconf or GSettings?

I am very new to NixOS and know very little of Linux in general, so any help would be appreciated. To know what direction I should be digging in would be great.

As mentioned in the other thread, GNOME manages the keyboard layout and relevant settings itself and the services.xserver.{layout,xkb*} NixOS options are only used for initial set up.

Your issue sounds like you have another layout added in GNOME – try clicking the language panel in the top bar to see them.

Currently, the least foot-gunny option for setting the GNOME keyboard layout declaratively is using home-manager as mentioned in Problem with `xkbOptions` - it doesn't seem to take effect - #12 by jtojnar.

1 Like

Thank you for answering!

It only shows two layouts:
image
image

Thank you for recommending home-manager. I think I will leave the issue as is until I figure out how to set up home-manager then.

Hmm, if there is just the two languages, maybe there is split brain between X server and GNOME. I would probably avoid grp:alt_shift_toggle and configure the layout switching shortcut directly in GNOME.

And, for completeness, dconf databases also recently became configurable in NixOS so you do not need home-manager if you do not have multi-user system or all users use the same layouts (untested):

  services.xserver = {
    layout = "us,ru";
  };
  programs = {
    dconf = {
      profiles = {
        user = {
          databases = [
            {
              # Disallow changing the input settings in Control Center since unlike with NixOS options,
              # there is no merging between databases and user-db would just replace this.
              lockAll = true;
              settings = {
                "org/gnome/desktop/wm/keybindings" = {
                  switch-input-source = [ "<Alt>Tab" ];
                  switch-input-source-backward = [ "<Shift><Alt>Tab" ];
                };

                "org/gnome/desktop/input-sources" = {
                  sources = [
                    (lib.gvariant.mkTuple [
                      "xkb"
                      "us"
                    ])
                    (lib.gvariant.mkTuple [
                      "xkb"
                      "ru"
                    ])
                  ];
                };
              };
            }
          ];
        };
      };
    };
  };

Ideally, we would have option like console.useXkbConfig but it is tricky, see nixos/gnome/xkb-settings: init by jtojnar · Pull Request #257837 · NixOS/nixpkgs · GitHub.

1 Like