Jump to content

Locales: Difference between revisions

From NixOS Wiki
Jooooscha (talk | contribs)
Add a note about the syntactic differences in setting the supported locales and the extra locale settings
Partially taken from nixos.wiki/wiki/Locales to give a full picture. Adjusted/shortened some part to fit and fixed some spelling errors
Line 1: Line 1:
== Notes when setting supportedLocales and extraLocaleSettings ==
Beaware when setting up your locale variables. The setting <code>i18n.supportedLocales</code> requires a different format to <code>i18n.extraLocaleSettings</code>.


The setting <code>i18n.supportedLocales</code> requries the requires the language string '''with''' the added <code>/UTF-8</code>


<syntaxhighlight lang="bash">
NixOS allows to set the default locale as well as individual locales in the configuration file:<syntaxhighlight lang="bash">
"en_US.UTF-8/UTF-8"
configuration.nix
"nl_NL.UTF-8/UTF-8"
{...}
</syntaxhighlight>


The setting <code>i18n.extraLocaleSettings</code>, however, requires the language string '''without''' any suffix.
# Mandatory
i18n.defaultLocale = "en_US.UTF-8";


<syntaxhighlight lang="bash">
# Optionally (BEWARE: requires a different format with the added /UTF-8)
LC_MESSAGES = "en_US.UTF-8"
i18n.i18n.supportedLocales = ["en_US.UTF-8/UTF-8" "es_VE.UTF-8/UTF-8"];
LC_TIME = "de_DE.UTF-8"
 
</syntaxhighlight>Otherwise, your locale settings will likely not work.
# 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";
  };
</syntaxhighlight>'''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 [https://search.nixos.org/options NixOS options]. Just search for i18 locale.
 
'''BEWARE:''' The setting <code>i18n.supportedLocales</code> requires a different format to <code>i18n.extraLocaleSettings</code>  with the added <code>/UTF-8</code>. Otherwise, your locale settings will likely not work.


== Troubleshooting when using nix on non-NixOS linux distributions ==
== Troubleshooting when using nix on non-NixOS linux distributions ==

Revision as of 12:35, 21 March 2025


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