IBus

From NixOS Wiki
Revision as of 18:56, 13 April 2020 by imported>Symphorien (ibus with custom emoji)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

IBus is a bus for various input methods.

Installation

Enabling IBus is described in the manual. Mainly, it can be done as follows:

/etc/nixos.configuration.nix
{pkgs, lib, ...}
{
 i18n.inputMethod = {
  enabled = "ibus";
  ibus.engines = with pkgs.ibus-engines; [ /* any engine you want, for example */ anthy ];
 };
}

After switching, you still need to logout from your session and login again for ibus to work correctly.

Input methods

To get the list of available engines, you can use the tab completion of nix repl.

$ nix repl
nix-repl> ibus-engines.<Tab>

Custom emojis

Custom emojis can be added to the emoji selection dialog of IBus. This can be used to workaround the fact that GTK does not support compose rules which output more than one unicode codepoint. The core of the method is to set the following hidden gsettings:

gsettings set org.freedesktop.ibus.panel.emoji favorite-annotations "['shrug']"
gsettings set org.freedesktop.ibus.panel.emoji favorites "['¯\_(ツ)_/¯']"

Then, typing Ctrl+Shift+e and then shrug, Space and Return will insert ¯\_(ツ)_/¯.

But on NixOS, gsettings cannot access schemas without wrapping. In order to workaround this, and make this declarative, here is a possible Home Manager module.

~/.config/nixpkgs/home.nix
{ pkgs, lib, config, ... }:
let
  /* define here the list of desired favorite emoji */
  fav = {
    shrug = ''¯\_(ツ)_/¯'';
    flip = ''(╯°□°)╯︵ ┻━┻'';
  };
  /* end */
  listToFile = l: builtins.toFile "fav" (builtins.toJSON l);
  /* gsettings needs to be wrapped with ibus to find the schemas */
  gsettings = pkgs.stdenv.mkDerivation {
    name = "gsettings-for-ibus";
    buildInputs = [ pkgs.ibus ];
    nativeBuildInputs = [ pkgs.wrapGAppsHook ];
    dontUnpack = true;
    dontBuild = true;
    installPhase = ''
      mkdir -p $out/bin
      ln -s ${pkgs.glib.bin}/bin/gsettings $out/bin
    '';
  };
  /* this script applies our modifications */
  applyScript = pkgs.writeShellScript "apply-emoji-prefs" ''
    ${gsettings}/bin/gsettings set org.freedesktop.ibus.panel.emoji favorites "$(${pkgs.coreutils}/bin/cat ${listToFile (lib.attrValues fav)})"
    ${gsettings}/bin/gsettings set org.freedesktop.ibus.panel.emoji favorite-annotations "$(${pkgs.coreutils}/bin/cat ${listToFile (lib.attrNames fav)})"
  '';
in
{
  systemd.user.services.ibus-favorite = {
    Unit = {
      Description = "Set ibus favorite emoji";
    };
    Install = {
      WantedBy = [ "graphical-session.target" ];
    };
    Service = {
      ExecStart = "${applyScript}";
      Type = "oneshot";
      RemainAfterExit = true;
    };
  };
}