From 21e7ccb1268c88331c2e88c95bcc5fc1cd862f75 Mon Sep 17 00:00:00 2001 From: uku Date: Fri, 5 Dec 2025 22:08:40 +0100 Subject: [PATCH] formats.javaProperties: fix cross-compilation --- .../formats/java-properties/default.nix | 163 +++++++++--------- 1 file changed, 85 insertions(+), 78 deletions(-) diff --git a/pkgs/pkgs-lib/formats/java-properties/default.nix b/pkgs/pkgs-lib/formats/java-properties/default.nix index c97e9b275d19..f94663a9b386 100644 --- a/pkgs/pkgs-lib/formats/java-properties/default.nix +++ b/pkgs/pkgs-lib/formats/java-properties/default.nix @@ -65,89 +65,96 @@ in generate = name: value: - pkgs.runCommand name + pkgs.callPackage ( { - # Requirements - # ============ - # - # 1. Strings in Nix carry over to the same - # strings in Java => need proper escapes - # 2. Generate files quickly - # - A JVM would have to match the app's - # JVM to avoid build closure bloat - # - Even then, JVM startup would slow - # down config generation. - # - # - # Implementation - # ============== - # - # Escaping has two steps - # - # 1. jq - # Escape known separators, in order not - # to break up the keys and values. - # This handles typical whitespace correctly, - # but may produce garbage for other control - # characters. - # - # 2. iconv - # Escape >ascii code points to java escapes, - # as .properties files are supposed to be - # encoded in ISO 8859-1. It's an old format. - # UTF-8 behavior may exist in some apps and - # libraries, but we can't rely on this in - # general. + runCommand, + jq, + libiconvReal, + }: + runCommand name + { + # Requirements + # ============ + # + # 1. Strings in Nix carry over to the same + # strings in Java => need proper escapes + # 2. Generate files quickly + # - A JVM would have to match the app's + # JVM to avoid build closure bloat + # - Even then, JVM startup would slow + # down config generation. + # + # + # Implementation + # ============== + # + # Escaping has two steps + # + # 1. jq + # Escape known separators, in order not + # to break up the keys and values. + # This handles typical whitespace correctly, + # but may produce garbage for other control + # characters. + # + # 2. iconv + # Escape >ascii code points to java escapes, + # as .properties files are supposed to be + # encoded in ISO 8859-1. It's an old format. + # UTF-8 behavior may exist in some apps and + # libraries, but we can't rely on this in + # general. - preferLocalBuild = true; - passAsFile = [ "value" ]; - value = builtins.toJSON value; - nativeBuildInputs = [ - pkgs.jq - pkgs.libiconvReal - ]; + preferLocalBuild = true; + passAsFile = [ "value" ]; + value = builtins.toJSON value; + nativeBuildInputs = [ + jq + libiconvReal + ]; - jqCode = - let - main = '' - to_entries - | .[] - | "\( - .key - | ${commonEscapes} - | gsub(" "; "\\ ") - | gsub("="; "\\=") - ) = \( - .value - | ${commonEscapes} - | gsub("^ "; "\\ ") - | gsub("\\n "; "\n\\ ") - )" - ''; - # Most escapes are equal for both keys and values. - commonEscapes = '' - gsub("\\\\"; "\\\\") - | gsub("\\n"; "\\n\\\n") - | gsub("#"; "\\#") - | gsub("!"; "\\!") - | gsub("\\t"; "\\t") - | gsub("\r"; "\\r") - ''; - in - main; + jqCode = + let + main = '' + to_entries + | .[] + | "\( + .key + | ${commonEscapes} + | gsub(" "; "\\ ") + | gsub("="; "\\=") + ) = \( + .value + | ${commonEscapes} + | gsub("^ "; "\\ ") + | gsub("\\n "; "\n\\ ") + )" + ''; + # Most escapes are equal for both keys and values. + commonEscapes = '' + gsub("\\\\"; "\\\\") + | gsub("\\n"; "\\n\\\n") + | gsub("#"; "\\#") + | gsub("!"; "\\!") + | gsub("\\t"; "\\t") + | gsub("\r"; "\\r") + ''; + in + main; - inputEncoding = "UTF-8"; + inputEncoding = "UTF-8"; - inherit comment; + inherit comment; - } - '' - ( - echo "$comment" | while read -r ln; do echo "# $ln"; done - echo - jq -r --arg hash '#' "$jqCode" "$valuePath" \ - | iconv --from-code "$inputEncoding" --to-code JAVA \ - ) > "$out" - ''; + } + '' + ( + echo "$comment" | while read -r ln; do echo "# $ln"; done + echo + jq -r --arg hash '#' "$jqCode" "$valuePath" \ + | iconv --from-code "$inputEncoding" --to-code JAVA \ + ) > "$out" + '' + ) { }; }; }