diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2025-05-14 10:50:27 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2025-05-14 10:50:27 +0200 |
commit | 43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c (patch) | |
tree | c1cc8a034395c9bb8188651f6835922b38887f32 /modules/i18n.nix | |
parent | 03d49aa8ec6f51c8f51bfb628e614ac537cca8e0 (diff) | |
download | nixos-43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c.tar nixos-43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c.tar.gz nixos-43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c.tar.bz2 nixos-43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c.tar.xz nixos-43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c.zip |
...
Diffstat (limited to 'modules/i18n.nix')
-rw-r--r-- | modules/i18n.nix | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/modules/i18n.nix b/modules/i18n.nix new file mode 100644 index 00000000..f84e8b64 --- /dev/null +++ b/modules/i18n.nix | |||
@@ -0,0 +1,156 @@ | |||
1 | { | ||
2 | config, | ||
3 | lib, | ||
4 | pkgs, | ||
5 | ... | ||
6 | }: | ||
7 | let | ||
8 | aggregatedLocales = | ||
9 | (builtins.map | ||
10 | (l: (lib.replaceStrings [ "utf8" "utf-8" "UTF8" ] [ "UTF-8" "UTF-8" "UTF-8" ] l) + "/UTF-8") | ||
11 | ( | ||
12 | [ config.i18n.defaultLocale ] | ||
13 | ++ (lib.optionals (builtins.isList config.i18n.extraLocales) config.i18n.extraLocales) | ||
14 | ++ (lib.attrValues (lib.filterAttrs (n: _v: lib.hasPrefix "LC_" n) config.i18n.extraLocaleSettings)) | ||
15 | ) | ||
16 | ) | ||
17 | ++ (lib.optional (builtins.isString config.i18n.extraLocales) config.i18n.extraLocales); | ||
18 | in | ||
19 | { | ||
20 | disabledModules = [ "config/i18n.nix" ]; | ||
21 | |||
22 | ###### interface | ||
23 | |||
24 | options = { | ||
25 | |||
26 | i18n = { | ||
27 | glibcLocales = lib.mkOption { | ||
28 | type = lib.types.path; | ||
29 | default = pkgs.glibcLocales.override { | ||
30 | allLocales = lib.any (x: x == "all") config.i18n.supportedLocales; | ||
31 | locales = config.i18n.supportedLocales; | ||
32 | }; | ||
33 | defaultText = lib.literalExpression '' | ||
34 | pkgs.glibcLocales.override { | ||
35 | allLocales = lib.any (x: x == "all") config.i18n.supportedLocales; | ||
36 | locales = config.i18n.supportedLocales; | ||
37 | } | ||
38 | ''; | ||
39 | example = lib.literalExpression "pkgs.glibcLocales"; | ||
40 | description = '' | ||
41 | Customized pkg.glibcLocales package. | ||
42 | |||
43 | Changing this option can disable handling of i18n.defaultLocale | ||
44 | and supportedLocale. | ||
45 | ''; | ||
46 | }; | ||
47 | |||
48 | defaultLocale = lib.mkOption { | ||
49 | type = lib.types.str; | ||
50 | default = "en_US.UTF-8"; | ||
51 | example = "nl_NL.UTF-8"; | ||
52 | description = '' | ||
53 | The default locale. It determines the language for program | ||
54 | messages, the format for dates and times, sort order, and so on. | ||
55 | It also determines the character set, such as UTF-8. | ||
56 | ''; | ||
57 | }; | ||
58 | |||
59 | extraLocales = lib.mkOption { | ||
60 | type = lib.types.either (lib.types.listOf lib.types.str) (lib.types.enum [ "all" ]); | ||
61 | default = [ ]; | ||
62 | example = [ "nl_NL.UTF-8" ]; | ||
63 | description = '' | ||
64 | Additional locales that the system should support, besides the ones | ||
65 | configured with {option}`i18n.defaultLocale` and | ||
66 | {option}`i18n.extraLocaleSettings`. | ||
67 | Set this to `"all"` to install all available locales. | ||
68 | ''; | ||
69 | }; | ||
70 | |||
71 | extraLocaleSettings = lib.mkOption { | ||
72 | type = lib.types.attrsOf lib.types.str; | ||
73 | default = { }; | ||
74 | example = { | ||
75 | LC_MESSAGES = "en_US.UTF-8"; | ||
76 | LC_TIME = "de_DE.UTF-8"; | ||
77 | }; | ||
78 | description = '' | ||
79 | A set of additional system-wide locale settings other than | ||
80 | `LANG` which can be configured with | ||
81 | {option}`i18n.defaultLocale`. | ||
82 | ''; | ||
83 | }; | ||
84 | |||
85 | supportedLocales = lib.mkOption { | ||
86 | type = lib.types.listOf lib.types.str; | ||
87 | visible = false; | ||
88 | default = lib.unique ( | ||
89 | [ | ||
90 | "C.UTF-8/UTF-8" | ||
91 | "en_US.UTF-8/UTF-8" | ||
92 | ] | ||
93 | ++ aggregatedLocales | ||
94 | ); | ||
95 | example = [ | ||
96 | "en_US.UTF-8/UTF-8" | ||
97 | "nl_NL.UTF-8/UTF-8" | ||
98 | "nl_NL/ISO-8859-1" | ||
99 | ]; | ||
100 | description = '' | ||
101 | List of locales that the system should support. The value | ||
102 | `"all"` means that all locales supported by | ||
103 | Glibc will be installed. A full list of supported locales | ||
104 | can be found at <https://sourceware.org/git/?p=glibc.git;a=blob;f=localedata/SUPPORTED>. | ||
105 | ''; | ||
106 | }; | ||
107 | |||
108 | }; | ||
109 | |||
110 | }; | ||
111 | |||
112 | ###### implementation | ||
113 | |||
114 | config = { | ||
115 | warnings = | ||
116 | lib.optional | ||
117 | ( | ||
118 | !( | ||
119 | (lib.subtractLists config.i18n.supportedLocales aggregatedLocales) == [ ] | ||
120 | || lib.any (x: x == "all") config.i18n.supportedLocales | ||
121 | ) | ||
122 | ) | ||
123 | '' | ||
124 | `i18n.supportedLocales` is deprecated in favor of `i18n.extraLocales`, | ||
125 | and it seems you are using `i18n.supportedLocales` and forgot to | ||
126 | include some locales specified in `i18n.defaultLocale`, | ||
127 | `i18n.extraLocales` or `i18n.extraLocaleSettings`. | ||
128 | |||
129 | If you're trying to install additional locales not specified in | ||
130 | `i18n.defaultLocale` or `i18n.extraLocaleSettings`, consider adding | ||
131 | only those locales to `i18n.extraLocales`. | ||
132 | ''; | ||
133 | |||
134 | environment.systemPackages = | ||
135 | # We increase the priority a little, so that plain glibc in systemPackages can't win. | ||
136 | lib.optional (config.i18n.supportedLocales != [ ]) (lib.setPrio (-1) config.i18n.glibcLocales); | ||
137 | |||
138 | environment.sessionVariables = { | ||
139 | LANG = config.i18n.defaultLocale; | ||
140 | LOCALE_ARCHIVE = "/run/current-system/sw/lib/locale/locale-archive"; | ||
141 | } // config.i18n.extraLocaleSettings; | ||
142 | |||
143 | systemd.globalEnvironment = lib.mkIf (config.i18n.supportedLocales != [ ]) { | ||
144 | LOCALE_ARCHIVE = "${config.i18n.glibcLocales}/lib/locale/locale-archive"; | ||
145 | }; | ||
146 | |||
147 | # ‘/etc/locale.conf’ is used by systemd. | ||
148 | environment.etc."locale.conf".source = pkgs.writeText "locale.conf" '' | ||
149 | LANG=${config.i18n.defaultLocale} | ||
150 | ${lib.concatStringsSep "\n" ( | ||
151 | lib.mapAttrsToList (n: v: "${n}=${v}") config.i18n.extraLocaleSettings | ||
152 | )} | ||
153 | ''; | ||
154 | |||
155 | }; | ||
156 | } | ||