Skip to content

Instantly share code, notes, and snippets.

@peti
Last active January 29, 2024 00:21
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save peti/2c818d6cb49b0b0f2fd7c300f8386bc3 to your computer and use it in GitHub Desktop.
Save peti/2c818d6cb49b0b0f2fd7c300f8386bc3 to your computer and use it in GitHub Desktop.
Make NixOS provide version-specific LOCALE_ARCHIVE environment variables

This NixOS code ensures that the system provide version-specific $LOCALE_ARCHIVE environment variables to mitigate the effects of NixOS/nixpkgs#38991.

To deploy it, copy the file into your /etc/nixos folder using a file name like multi-glibc-locale-paths.nix. Then edit your configuration.nix file to contain the attribute:

imports = [ ./multi-glibc-locale-paths.nix ];

If you are running Nix on a host system other than NixOS, you'll have to configure those environment variables manually:

  • Set $LOCALE_ARCHIVE_2_27 to the path "${glibcLocales}/lib/locale/locale-archive". You can find out what glibcLocales is by running:

      $ nix-build --no-out-link "<nixpkgs>" -A glibcLocales
      /nix/store/m53mq2077pfxhqf37gdbj7fkkdc1c8hc-glibc-locales-2.27
    
  • Set $LOCALE_ARCHIVE_2_11 to the path of your system's locale.

{ config, pkgs, ... }: # multi-glibc-locale-paths.nix
/*
* Provide version-specific LOCALE_ARCHIVE environment variables to mitigate
* the effects of https://github.com/NixOS/nixpkgs/issues/38991.
*/
let
# A random Nixpkgs revision *before* the default glibc
# was switched to version 2.27.x.
oldpkgsSrc = pkgs.fetchFromGitHub {
owner = "nixos";
repo = "nixpkgs";
rev = "0252e6ca31c98182e841df494e6c9c4fb022c676";
sha256 = "1sr5a11sb26rgs1hmlwv5bxynw2pl5w4h5ic0qv3p2ppcpmxwykz";
};
oldpkgs = import oldpkgsSrc {};
# A random Nixpkgs revision *after* the default glibc
# was switched to version 2.27.x.
newpkgsSrc = pkgs.fetchFromGitHub {
owner = "nixos";
repo = "nixpkgs";
rev = "1d0a71879dac0226272212df7a2463d8eeb8f75b";
sha256 = "0nh6wfw50lx6wkzyiscfqg6fl6rb17wmncj8jsdvbgmsd6rm95rg";
};
newpkgs = import newpkgsSrc {};
in
{
environment.sessionVariables = {
LOCALE_ARCHIVE_2_11 = "${oldpkgs.glibcLocales}/lib/locale/locale-archive";
LOCALE_ARCHIVE_2_27 = "${newpkgs.glibcLocales}/lib/locale/locale-archive";
};
}
@shlevy
Copy link

shlevy commented Apr 25, 2018

We want LOCALE_ARCHIVE_2_11 for glibc before 2.27.

@peti
Copy link
Author

peti commented Jun 27, 2018

@shlevy, fixed.

@guibou
Copy link

guibou commented Jun 28, 2018

@peti the README says LOCALE_ARCHIVE_2_27 and the nix file says LOCALE_ARCHIVE_2_26. Is this normal?

@peti
Copy link
Author

peti commented Jun 30, 2018

@guibou, that was a bug I introduced recently. Thanks for pointing that out, it's fixed now.

@guibou
Copy link

guibou commented Jul 3, 2018

@peti did you correct 2_11 to 2_21 by mistake too? ;)

@ivan
Copy link

ivan commented Oct 30, 2018

I verified that this makes 18.03's perl shut up after changing LOCALE_ARCHIVE_2_21 to LOCALE_ARCHIVE_2_11, i.e.

# Based on https://gist.github.com/peti/2c818d6cb49b0b0f2fd7c300f8386bc3

{ config, pkgs, ... }:

/*
 * Provide version-specific LOCALE_ARCHIVE environment variables to mitigate
 * the effects of https://github.com/NixOS/nixpkgs/issues/38991.
 */

let

  # A random Nixpkgs revision *before* the default glibc
  # was switched to version 2.27.x.
  oldpkgsSrc = pkgs.fetchFromGitHub {
    owner = "nixos";
    repo = "nixpkgs";
    rev = "0252e6ca31c98182e841df494e6c9c4fb022c676";
    sha256 = "1sr5a11sb26rgs1hmlwv5bxynw2pl5w4h5ic0qv3p2ppcpmxwykz";
  };

  oldpkgs = import oldpkgsSrc {};

in

{
  environment.sessionVariables = {
    LOCALE_ARCHIVE_2_11 = "${oldpkgs.glibcLocales}/lib/locale/locale-archive";
    LOCALE_ARCHIVE_2_27 = "${pkgs.glibcLocales}/lib/locale/locale-archive";
  };
}

@ivan
Copy link

ivan commented Oct 30, 2018

I noticed that even with this module deployed, LOCALE_ARCHIVE_2_11 never made it into services like hydra-queue-runner.service, which was the thing emitting all the Perl locale warning spam for me. I managed to fix it by patching hydra:

diff --git a/hydra-module.nix b/hydra-module.nix
index ce5dc266..173fcf8e 100644
--- a/hydra-module.nix
+++ b/hydra-module.nix
@@ -10,10 +10,24 @@ let

   hydraConf = pkgs.writeScript "hydra.conf" cfg.extraConfig;

+  # https://gist.github.com/peti/2c818d6cb49b0b0f2fd7c300f8386bc3
+  #
+  # A random Nixpkgs revision *before* the default glibc
+  # was switched to version 2.27.x.
+  oldpkgsSrc = pkgs.fetchFromGitHub {
+    owner = "nixos";
+    repo = "nixpkgs";
+    rev = "0252e6ca31c98182e841df494e6c9c4fb022c676";
+    sha256 = "1sr5a11sb26rgs1hmlwv5bxynw2pl5w4h5ic0qv3p2ppcpmxwykz";
+  };
+
+  oldpkgs = import oldpkgsSrc {};
+
   hydraEnv =
     { HYDRA_DBI = cfg.dbi;
       HYDRA_CONFIG = "${baseDir}/hydra.conf";
       HYDRA_DATA = "${baseDir}";
+      LOCALE_ARCHIVE_2_11 = "${oldpkgs.glibcLocales}/lib/locale/locale-archive";
     };

   env =

(Even more fun: this can't be used in combination with the module in this gist because the environmental variable conflicts.)

@timeyyy
Copy link

timeyyy commented Mar 20, 2019

I get this error when i run nix-build --no-out-link "<nixpkgs>" -A glibcLocales
->
error: expression does not evaluate to a derivation (or a set or list of those) (non nixos system)

@573
Copy link

573 commented Oct 8, 2020

Got entirely rid of related warnings (bash: warning: setlocale: LC_CTYPE: cannot change locale (de_DE.UTF-8): No such file or directory) by editing my nix hosts (arch linux) /etc/locale.gen file accordingly and running sudo -i locale-gen. Still have the LOCALE_ARCHIVE settings applied in my nix config though not sure if they're needed in my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment