Locales

From NixOS Wiki
Jump to: navigation, search

NixOS allows to set the default locale as well as individual locales in the configuration file:

configuration.nix
{...}
i18n.defaultLocale = "en_US.UTF-8";

This will set the locale systemwide to the desired value. In the above example, en_US.UTF-8, which will set the default locale globally to American English using UTF-8 encoding.

In addition, with supportedLocales, the system will also support Venezuelan Spanish. The value "all" means that all locales supported by Glibc will be installed. A full list of supported locales can be found at https://sourceware.org/git/?p=glibc.git;a=blob;f=localedata/SUPPORTED.

# BEWARE: requires a different format with the added /UTF-8
i18n.i18n.supportedLocales = ["en_US.UTF-8/UTF-8" "es_VE.UTF-8/UTF-8"];

Setting Additional LC Locales

In addition to the default locale, it is possible to set the LC locales. This does allow fine-grained adjustments of the used locales. The desired LC locale variables can also be set in the configuration.nix file, using the i18.extraLocaleSettings option:

configuration.nix
{...}
i18n.extraLocaleSettings = {
    LC_ALL = "en_US.UTF-8";
    LC_CTYPE = "en_US.UTF8";
    LC_ADDRESS = "es_VE.UTF-8";
    LC_IDENTIFICATION = "es_VE.UTF-8";
    LC_MEASUREMENT = "es_VE.UTF-8";
    LC_MESSAGES = "en_US.UTF-8";
    LC_MONETARY = "es_VE.UTF-8";
    LC_NAME = "es_VE.UTF-8";
    LC_NUMERIC = "en_US.UTF-8";
    LC_PAPER = "es_VE.UTF-8";
    LC_TELEPHONE = "es_VE.UTF-8";
    LC_TIME = "es_VE.UTF-8";
    LC_COLLATE = "es_VE.UTF-8";
  };

In the above example a mix of American English and Venezuelan Spanish is used. As long as the locale exists, you are free to add it and even mix several languages, like in the example above. It is also possible to find these settings at NixOS options. Just search for i18 locale.

Troubleshooting when using nix on non-NixOS linux distributions

You may need to set the environmental variable LOCALE_ARCHIVE to point to your system's locale-archive. The following can be added to your .zshenv (zsh) or .profile (bash) and applies to Debian, Red Hat, and Arch derivatives:

export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive

And if that file from the local system is somehow broken:

# May require a one-time installation with: nix-env -iA nixpkgs.glibcLocales
export LOCALE_ARCHIVE="$(nix-env --installed --no-name --out-path --query glibc-locales)/lib/locale/locale-archive"

Enable locale support in Nix shell

To support locales within a Nix shell, for example to get localised command output, you need to do something similar:

pkgs.mkShell {
  # [other code omitted]
  LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
}