`services.xserver.xkb.extraLayouts` doesn't seem to be compatible with Sway

I have some custom layouts configured using services.xserver.xkb.extraLayouts, like so:

services.xserver.xkb.extraLayouts = {
  bsk = {                            
    description = "BB Switch";       
    symbolsFile = ./bsk;             
    languages = [ "eng" "fr" "it" ]; 
  };                                 
};

and I also have something like this, in wayland.windowManager.sway.extraConfig:

input type:keyboard {
  xkb_layout bsk
}

Basically, I just want to have a custom layout and use it with my WM. It used to work, until I updated my system. Now, on nixos-rebuild switch, it complains that:

error: builder for '/nix/store/xq9h502wnqwxpgc3zx8zh19q1ysvj1dv-sway.conf.drv' failed with exit code 1;
       last 25 log lines:
       > Fontconfig error: No writable cache directories
       > Fontconfig error: No writable cache directories
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338] Couldn't find file "symbols/bsk" in include paths
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338] 1 include paths searched:
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338]         /nix/store/4d6p2fvack9fzcfp78xw9g45wz6m0pri-xkeyboard-config-2.41/etc/X11/xkb
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338] 3 include paths could not be added:
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338]       /homeless-shelter/.config/xkb
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338]   /homeless-shelter/.xkb
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-338]  /etc/xkb
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] [XKB-769] Abandoning symbols file "(unnamed)"
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] Failed to compile xkb_symbols
       > 00:00:00.048 [sway/input/keyboard.c:732] [xkbcommon] Failed to compile keymap
       > 00:00:00.048 [sway/config.c:872] Error on line 37 'xkb_layout bsk': Failed to compile keymap: [XKB-338] Couldn't find file "symbols/bsk" in include paths (/nix/store/h5g6096x1ri7s1g4yz5cy2xgwzds87sf-sway.conf)
       > 00:00:00.057 [sway/config.c:435] Error(s) loading config!
       For full logs, run 'nix log /nix/store/xq9h502wnqwxpgc3zx8zh19q1ysvj1dv-sway.conf.drv'
error: 1 dependencies of derivation '/nix/store/s64hkvjq5g4jc6fhk4f41fkcmclwv0bc-activation-script.drv' failed to build
...

My understanding of this error is that it tries to validate the configuration file at build time, but fails to resolve the keyboard layouts, because it doesnā€™t refer to the correct derivation of xkb, the one that does contain my custom layouts.

wayland.windowManager.sway.extraConfig

Where is this option coming from? home-manager?

I should have been more specific: it does indeed come from home-manager, which I use as a module of my system configuration.

Ok, Iā€™ve found it here.
The solution is to take into account XKB_CONFIG_ROOT.
If home-manager is installed as a NixOS module that command could pick it up from config.environment.sessionVariables. For example, hereā€™s how the xserver module validates its layout option:

    system.checks = singleton (pkgs.runCommand "xkb-validated" {
      inherit (cfg.xkb) dir model layout variant options;
      nativeBuildInputs = with pkgs.buildPackages; [ xkbvalidate ];
      preferLocalBuild = true;
    } ''
      ${optionalString (config.environment.sessionVariables ? XKB_CONFIG_ROOT)
        "export XKB_CONFIG_ROOT=${config.environment.sessionVariables.XKB_CONFIG_ROOT}"
      }
      XKB_CONFIG_ROOT="$dir" xkbvalidate "$model" "$layout" "$variant" "$options"
      touch "$out"
    '');

When using home-manager standalone, I donā€™t think itā€™s possible because that check is performed at build time and thereā€™s now way to know what the environment will look like at run time. Possible solutions could be:

  1. Add an option in the vein of networking.nftables.preCheckRuleset to remove config lines that canā€™t be checked reliably

  2. Add an option to disable the check entirely

  3. Make the failure non-fatal, just emit a warning.

1 Like

You should probably report this to the maintainer of the sway module in home-manager, because from NixOS thereā€™s not much we can do.

It seems that option 2. already exists, at wayland.windowManager.sway.checkConfig. Iā€™ve used it as a workaround. Iā€™ll notify the HM maintainers of this issue, eventually.

FWIW there are talks about the problem here:

And a PR for a possible way to fix things here:

1 Like