hledger icon indicating copy to clipboard operation
hledger copied to clipboard

glibc-using-nix-installed or guix-installed hledger won't handle non-ascii data, even with LANG set

Open simonmichael opened this issue 5 years ago • 11 comments

Reported by @Vifon: hledger installed via the nix command on the download page fails if the journal contains non-ascii characters. Reproduced on Gentoo and Ubuntu GNU/Linux; does not happen on mac.

Eg:

$ nix-env -i -f https://github.com/NixOS/nixpkgs/archive/fe41fd.tar.gz -A hledger; curl -s https://raw.githubusercontent.com/simonmichael/hledger/master/examples/unicode.journal | ~/.nix-profile/bin/hledger -f- print
...
hledger: <stdin>: hGetContents: invalid argument (invalid byte sequence)
$ echo $LANG
en_US.utf8
$ locale -a | grep US
en_US.iso885915
en_US.utf8
$ curl -s https://raw.githubusercontent.com/simonmichael/hledger/master/examples/unicode.journal | ~/.local/bin/hledger -f- print
(works)

simonmichael avatar May 31 '19 22:05 simonmichael

@peti, any idea what might be the cause ?

simonmichael avatar May 31 '19 22:05 simonmichael

The Nix glibc does not know any locales (other than C). Locale data resides in a separate package, glibcLocales, and glibc can only find that package through an environment variable

LOCALE_ARCHIVE="$(nix-build --no-out-link "<nixpkgs>" -A glibcLocales)/lib/locale/locale-archive"

that needs to be set at run-time. Alternatively, that variable can also point to the locale archive of the native host OS:

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

This separation exists so that Nix users can choose a custom set of locales they'd like to install without affecting glibc. If glibc would have a reference to the locale data, then any change to the locate data would trigger a complete re-build of the entire system.

peti avatar Jun 01 '19 12:06 peti

Wow, good info, thanks. What do you think is the best approach for reliable install commands that take this into account ? This worked on Linux:

$ export LOCALE_ARCHIVE="$(nix-build --no-out-link '<nixpkgs>' -A glibcLocales)/lib/locale/locale-archive"
$ nix-env -i -f https://github.com/NixOS/nixpkgs/archive/fe41fd.tar.gz -A hledger hledger-web hledger-ui

but not on Mac:

$ nix-build --no-out-link '<nixpkgs>' -A glibcLocales
error: expression does not evaluate to a derivation (or a set or list of those)

simonmichael avatar Jun 01 '19 15:06 simonmichael

Also, I'm curious why it doesn't require this extra step on Mac.

simonmichael avatar Jun 01 '19 15:06 simonmichael

Nix doesn't use glibc on Darwin. I believe Darwin builds use the native libc, so the whole "re-building the world" problem doesn't exist on that OS.

peti avatar Jun 01 '19 17:06 peti

just wanted to chime in and say this fixed my issue on Ubuntu 18.04! thank you 😁

samm81 avatar Apr 10 '20 18:04 samm81

I just ran into this issue. It would be good if the file line (and character position) would be displayed so at least users can check what hledger doesn't like about the file.

tbm avatar Jul 26 '20 05:07 tbm

Same on Guix, although it's easily resolved by:

guix install glibc-locales

And adding

export GUIX_LOCPATH=$HOME/.guix-profile/lib/locale

somewhere to .profile

SqrtMinusOne avatar Jan 05 '22 07:01 SqrtMinusOne

This is really a nix issue, rather than a hledger issue. Perhaps we can close this?

Xitian9 avatar Mar 09 '22 02:03 Xitian9

This is not a Nix issue. I experienced it on Debian. I think it happens when the locales are not installed.

While it needs to be fixed by the user, I suggested something that hledger can do:

It would be good if the file line (and character position) would be displayed so at least users can check what hledger doesn't like about the file

Also something about installing locales could be put into the hledger docs.

tbm avatar Mar 09 '22 02:03 tbm

There are multiple issues (or a multi-headed issue), including:

  1. GHC-compiled programs handling non-ascii data require a utf8-aware locale to be configured, or will die with various unhelpful messages. (https://gitlab.haskell.org/ghc/ghc/-/issues/17755)
  2. Nix-installed programs on platforms using glibc require locale to be configured in a special nix way (not $LANG). (comment)
  3. GUIX-installed programs have a similar issue and special guix way of setting locale (comment)
  4. ~hledger install docs (https://hledger.org/install.html#utf-8-locale-required, https://hledger.org/install.html#check-your-locale) don't mention the special nix way~
  5. hledger doesn't include special handling to try to improve on the default behaviour (possibly related: https://github.com/simonmichael/hledger/issues/1619 ?)

simonmichael avatar Mar 09 '22 03:03 simonmichael

Same on Guix, although it's easily resolved by:

guix install glibc-locales

And adding

export GUIX_LOCPATH=$HOME/.guix-profile/lib/locale

somewhere to .profile

Hello @SqrtMinusOne .. do you know of a one-line GUIX install command suitable for our Install page ? Would guix install glibc-locales hledger (plus the GUIX_LOCPATH tip) be enough ?

simonmichael avatar Jun 23 '23 04:06 simonmichael

@simonmichael Well, Guix is meant to be configured declaratively, so there isn't a one liner that installs the package the Guix way :-)

But yeah, in principle

guix install glibc-locales hledger && echo "export GUIX_LOCPATH=$HOME/.guix-profile/lib/locale" >> ~/.profile

will do the job. Users will have to adapt it to their setup.

SqrtMinusOne avatar Jun 23 '23 19:06 SqrtMinusOne

Thanks, perhaps I'll leave GUIX off the page in that case.

simonmichael avatar Jun 24 '23 00:06 simonmichael

See also #2089.

simonmichael avatar Oct 10 '23 11:10 simonmichael