Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setlocale() fails for anything other than "C" (missing locale-archive?) #39328

Closed
timbertson opened this issue Apr 22, 2018 · 3 comments
Closed

Comments

@timbertson
Copy link
Contributor

Apologies if this has already been covered or is expected, I've searched through a lot of issues but I'm not familiar enough with how glibc locales are supposed to work to know for sure.

Issue description

setlocale() only supports trivial locales (e.g. C). Calling setlocale() with e.g. LC_ALL=en_AU.UTF-8 fails (returns null). I discovered this because after an upgrade, fish shell no longer thinks my locale supports UTF-8: fish-shell/fish-shell#4935

Steps to reproduce

First, the setup:

$ cat setlocale.c
#include <locale.h>
#include <stdio.h>

int main() {
	char* locale = setlocale(LC_ALL, "");
	printf("locale = %s\n", locale);
}

$ cat setlocale.nix 
with (import <nixpkgs> {});
stdenv.mkDerivation {
	name = "setlocale-0";
	buildCommand = ''
		gcc -o $out ${./setlocale.c}
	'';
}

# build one with system libraries:
$ gcc -o setlocale setlocale.c

# and one with nix:
$ nix-build setlocale.nix 
/nix/store/gc3s42m3j2jhvxqp73xp5bhdqfrckfah-setlocale-0

Now, to run them:

$ env LC_ALL=en_AU.UTF-8 ./setlocale 
locale = en_AU.UTF-8

$ env LC_ALL=en_AU.UTF-8 ./result 
locale = (null)

$ env LC_ALL=C ./result 
locale = C

Doing an strace, I found that the nix version is trying to open two locale files:

$ strace -eopenat -f ./result 2>&1 | grep locale-archive
openat(AT_FDCWD, "/nix/store/27x7pinqdsl9f3rpbm8bsszd9fhwq266-glibc-2.27/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

I'm guessing that my /usr/lib/locale/locale-archive is an old / incompatible version, since an strace confirms that file is opened by the (successful) ./setlocale

LDD info:

My system libc is 2.26, while nixpkgs is 2.27.

$ ldd setlocale
	linux-vdso.so.1 (0x00007f37f2cc3000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f37f26eb000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f37f2aa1000)

$ ll /lib64/libc.so.6
lrwxrwxrwx. 1 root root 12 Mar  2 23:05 /lib64/libc.so.6 -> libc-2.26.so

$ ldd result
	linux-vdso.so.1 (0x00007fffdf7de000)
	libc.so.6 => /nix/store/27x7pinqdsl9f3rpbm8bsszd9fhwq266-glibc-2.27/lib/libc.so.6 (0x00007f360fdc7000)
	/nix/store/27x7pinqdsl9f3rpbm8bsszd9fhwq266-glibc-2.27/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007f361017b000)

$ ll /nix/store/27x7pinqdsl9f3rpbm8bsszd9fhwq266-glibc-2.27/lib/libc.so.6
lrwxrwxrwx. 1 tim tim 12 Jan  1  1970 /nix/store/27x7pinqdsl9f3rpbm8bsszd9fhwq266-glibc-2.27/lib/libc.so.6 -> libc-2.27.so*

If I explicitly provide nix's libc locale archive, it works:

$ env LOCALE_ARCHIVE=/nix/store/8kw71xqs5bz6s68dylv6y13082zn0023-glibc-locales-2.27/lib/locale/locale-archive LC_ALL=en_AU.UTF-8 ./result 
locale = en_AU.UTF-8

Is it possible for libc to use this file by default? If not, is there anything I can put in my ~/.nixpkgs/config.nix to do this globally (ideally without causing mass rebuilds...)?

Technical details

$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 4.15.17-300.fc27.x86_64, Fedora, 27 (Workstation Edition)`
 - multi-user?: `no`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.0`
 - channels(tim): `"nixpkgs-18.09pre137305.d91caac6c3e"`
 - nixpkgs: `/home/tim/.nix-defexpr/channels/nixpkgs`
@xeji
Copy link
Contributor

xeji commented Apr 22, 2018

possibly related: #38991

@timbertson
Copy link
Contributor Author

thanks - definitely sounds familiar, although that seems to be nixos focussed, while this is an issue with plain nixpkgs (where it's likely systems will be running with an incompatible locale-archive for a long time)

@peti
Copy link
Member

peti commented Apr 24, 2018

This is a duplicate of #38991.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants