Locales

Revision as of 12:35, 21 March 2025 by Turbotimon (talk | contribs) (Partially taken from nixos.wiki/wiki/Locales to give a full picture. Adjusted/shortened some part to fit and fixed some spelling errors)


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

configuration.nix
{...}

# Mandatory
i18n.defaultLocale = "en_US.UTF-8";

# Optionally (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"];

# Optionally
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";
  };

defaultLocale will set the locale systemwide to the desired value. In addition, with supportedLocales, the system will also support Venezuelan Spanish. And in the extraLocaleSettings, it is possible to set the LC locales individually. This does allow fine-grained adjustments of the used locales. In the above example a mix of American English and Venezuelan Spanish is used. It is also possible to find these settings at NixOS options. Just search for i18 locale.

BEWARE: The setting i18n.supportedLocales requires a different format to i18n.extraLocaleSettings with the added /UTF-8. Otherwise, your locale settings will likely not work.

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 profile install nixpkgs#glibcLocales
# Using nix profile
export LOCALE_ARCHIVE="$(nix profile list --json | jq '.elements[] | select(.attrPath? and (.attrPath | type == "string") and (.attrPath | endswith("glibcLocales"))) | .storePaths[0]')/lib/locale/locale-archive"

# Legacy usage with `nix-env`: 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";
}