Merge pull request #296549 from TomaSajt/strip-java-archives-hook

add stripJavaArchivesHook and use treewide
This commit is contained in:
Pol Dellaiera 2024-03-19 05:41:08 +01:00 committed by GitHub
commit 186c3e34a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 89 additions and 129 deletions

View file

@ -4,12 +4,31 @@ Ant-based Java packages are typically built from source as follows:
```nix ```nix
stdenv.mkDerivation { stdenv.mkDerivation {
name = "..."; pname = "...";
version = "...";
src = fetchurl { ... }; src = fetchurl { ... };
nativeBuildInputs = [ jdk ant ]; nativeBuildInputs = [
ant
jdk
stripJavaArchivesHook # removes timestamp metadata from jar files
];
buildPhase = "ant"; buildPhase = ''
runHook preBuild
ant # build the project using ant
runHook postBuild
'';
installPhase = ''
runHook preInstall
# copy generated jar file(s) to an appropriate location in $out
install -Dm644 build/foo.jar $out/share/java/foo.jar
runHook postInstall
'';
} }
``` ```
@ -17,6 +36,10 @@ Note that `jdk` is an alias for the OpenJDK (self-built where available,
or pre-built via Zulu). Platforms with OpenJDK not (yet) in Nixpkgs or pre-built via Zulu). Platforms with OpenJDK not (yet) in Nixpkgs
(`Aarch32`, `Aarch64`) point to the (unfree) `oraclejdk`. (`Aarch32`, `Aarch64`) point to the (unfree) `oraclejdk`.
Also note that not using `stripJavaArchivesHook` will likely cause the
generated `.jar` files to be non-deterministic, which is not optimal.
Using it, however, does not always guarantee reproducibility.
JAR files that are intended to be used by other packages should be JAR files that are intended to be used by other packages should be
installed in `$out/share/java`. JDKs have a stdenv setup hook that add installed in `$out/share/java`. JDKs have a stdenv setup hook that add
any JARs in the `share/java` directories of the build inputs to the any JARs in the `share/java` directories of the build inputs to the

View file

@ -9,7 +9,7 @@
, dpkg , dpkg
, writeScript , writeScript
, bash , bash
, strip-nondeterminism , stripJavaArchivesHook
, tor , tor
, zip , zip
, xz , xz
@ -50,7 +50,7 @@ stdenv.mkDerivation rec {
dpkg dpkg
imagemagick imagemagick
makeWrapper makeWrapper
strip-nondeterminism stripJavaArchivesHook
xz xz
zip zip
findutils findutils
@ -89,7 +89,6 @@ stdenv.mkDerivation rec {
tar --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cJf native/linux/x64/tor.tar.xz tor tar --sort=name --mtime="@$SOURCE_DATE_EPOCH" -cJf native/linux/x64/tor.tar.xz tor
tor_jar_file=$(find ./opt/bisq/lib/app -name "tor-binary-linux64-*.jar") tor_jar_file=$(find ./opt/bisq/lib/app -name "tor-binary-linux64-*.jar")
zip -r $tor_jar_file native zip -r $tor_jar_file native
strip-nondeterminism ./opt/bisq/lib/app/*.jar
''; '';
installPhase = '' installPhase = ''

View file

@ -2,7 +2,7 @@
, stdenv , stdenv
, fetchzip , fetchzip
, ant , ant
, canonicalize-jars-hook , stripJavaArchivesHook
, jdk , jdk
, makeWrapper , makeWrapper
}: }:
@ -18,7 +18,7 @@ stdenv.mkDerivation (finalAttrs: {
nativeBuildInputs = [ nativeBuildInputs = [
ant ant
canonicalize-jars-hook stripJavaArchivesHook
jdk jdk
makeWrapper makeWrapper
]; ];

View file

@ -6,6 +6,7 @@
, jre , jre
, ant , ant
, makeWrapper , makeWrapper
, stripJavaArchivesHook
, doCheck ? true , doCheck ? true
, withExamples ? false , withExamples ? false
}: }:
@ -30,10 +31,6 @@ stdenv.mkDerivation rec {
]; ];
postPatch = with deps; '' postPatch = with deps; ''
# Fix the output jar timestamps for reproducibility
substituteInPlace build.xml \
--replace-fail '<jar ' '<jar modificationtime="0" '
# Manually create version properties file for reproducibility # Manually create version properties file for reproducibility
mkdir -p build/classes mkdir -p build/classes
cat > build/classes/mkgmap-version.properties << EOF cat > build/classes/mkgmap-version.properties << EOF
@ -61,7 +58,7 @@ stdenv.mkDerivation rec {
'') testInputs} '') testInputs}
''; '';
nativeBuildInputs = [ jdk ant makeWrapper ]; nativeBuildInputs = [ jdk ant makeWrapper stripJavaArchivesHook ];
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild

View file

@ -6,6 +6,7 @@
, jre , jre
, ant , ant
, makeWrapper , makeWrapper
, stripJavaArchivesHook
, doCheck ? true , doCheck ? true
}: }:
let let
@ -30,10 +31,6 @@ stdenv.mkDerivation rec {
]; ];
postPatch = with deps; '' postPatch = with deps; ''
# Fix the output jar timestamps for reproducibility
substituteInPlace build.xml \
--replace-fail '<jar ' '<jar modificationtime="0" '
# Manually create version properties file for reproducibility # Manually create version properties file for reproducibility
mkdir -p build/classes mkdir -p build/classes
cat > build/classes/splitter-version.properties << EOF cat > build/classes/splitter-version.properties << EOF
@ -58,7 +55,7 @@ stdenv.mkDerivation rec {
'') testInputs} '') testInputs}
''; '';
nativeBuildInputs = [ jdk ant makeWrapper ]; nativeBuildInputs = [ jdk ant makeWrapper stripJavaArchivesHook ];
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild

View file

@ -7,7 +7,7 @@
, wrapGAppsHook , wrapGAppsHook
, makeDesktopItem , makeDesktopItem
, copyDesktopItems , copyDesktopItems
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -27,7 +27,7 @@ stdenv.mkDerivation (finalAttrs: {
makeWrapper makeWrapper
wrapGAppsHook wrapGAppsHook
copyDesktopItems copyDesktopItems
canonicalize-jars-hook stripJavaArchivesHook
]; ];
dontWrapGApps = true; dontWrapGApps = true;

View file

@ -6,7 +6,7 @@
, jre , jre
, makeWrapper , makeWrapper
, copyDesktopItems , copyDesktopItems
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -25,7 +25,7 @@ stdenv.mkDerivation (finalAttrs: {
jdk jdk
makeWrapper makeWrapper
copyDesktopItems copyDesktopItems
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -4,6 +4,7 @@
, makeDesktopItem , makeDesktopItem
, makeWrapper , makeWrapper
, wrapGAppsHook , wrapGAppsHook
, stripJavaArchivesHook
, ant , ant
, jdk , jdk
, jre , jre
@ -46,13 +47,7 @@ stdenv.mkDerivation rec {
hash = "sha256-MSVSd5DyVL+dcfTDv1M99hxickPwT2Pt6QGNsu6DGZI="; hash = "sha256-MSVSd5DyVL+dcfTDv1M99hxickPwT2Pt6QGNsu6DGZI=";
}; };
postPatch = '' nativeBuildInputs = [ ant jdk wrapGAppsHook makeWrapper stripJavaArchivesHook ];
# Fix jar timestamps for reproducibility
substituteInPlace build/build.xml \
--replace-fail '<jar ' '<jar modificationtime="0" '
'';
nativeBuildInputs = [ ant jdk wrapGAppsHook makeWrapper ];
buildInputs = lib.optionals stdenv.isLinux [ gtk2 glib libXtst ] buildInputs = lib.optionals stdenv.isLinux [ gtk2 glib libXtst ]
++ lib.optional stdenv.isDarwin Cocoa; ++ lib.optional stdenv.isDarwin Cocoa;

View file

@ -5,7 +5,7 @@
, jdk , jdk
, jre , jre
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -29,7 +29,7 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -1,9 +0,0 @@
{ substituteAll, unzip, zip }:
substituteAll {
name = "canonicalize-jar";
src = ./canonicalize-jar.sh;
unzip = "${unzip}/bin/unzip";
zip = "${zip}/bin/zip";
}

View file

@ -1,29 +0,0 @@
# Canonicalize the manifest & repack with deterministic timestamps.
canonicalizeJar() {
local input='' outer=''
input="$(realpath -sm -- "$1")"
outer="$(pwd)"
# -qq: even quieter
@unzip@ -qq "$input" -d "$input-tmp"
canonicalizeJarManifest "$input-tmp/META-INF/MANIFEST.MF"
# Sets all timestamps to Jan 1 1980, the earliest mtime zips support.
find -- "$input-tmp" -exec touch -t 198001010000.00 {} +
rm "$input"
pushd "$input-tmp" 2>/dev/null
# -q|--quiet, -r|--recurse-paths
# -o|--latest-time: canonicalizes overall archive mtime
# -X|--no-extra: don't store platform-specific extra file attribute fields
@zip@ -qroX "$outer/tmp-out.jar" . 2> /dev/null
popd 2>/dev/null
rm -rf "$input-tmp"
mv "$outer/tmp-out.jar" "$input"
}
# See also the Java specification's JAR requirements:
# https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Notes_on_Manifest_and_Signature_Files
canonicalizeJarManifest() {
local input=''
input="$(realpath -sm -- "$1")"
(head -n 1 "$input" && tail -n +2 "$input" | sort | grep -v '^\s*$') > "$input-tmp"
mv "$input-tmp" "$input"
}

View file

@ -1,16 +0,0 @@
# This setup hook causes the fixup phase to repack all JAR files in a
# canonical & deterministic fashion, e.g. resetting mtimes (like with normal
# store files) and avoiding impure metadata.
fixupOutputHooks+=('if [ -z "$dontCanonicalizeJars" -a -e "$prefix" ]; then canonicalizeJarsIn "$prefix"; fi')
canonicalizeJarsIn() {
local dir="$1"
echo "canonicalizing jars in $dir"
dir="$(realpath -sm -- "$dir")"
while IFS= read -rd '' f; do
canonicalizeJar "$f"
done < <(find -- "$dir" -type f -name '*.jar' -print0)
}
source @canonicalize_jar@

View file

@ -0,0 +1,16 @@
# This setup hook makes the fixup phase to repack all java archives in a
# deterministic fashion. The most important change being done is the resetting
# of the modification times of the archive entries
fixupOutputHooks+=('stripJavaArchivesIn $prefix')
stripJavaArchivesIn() {
local dir="$1"
echo "stripping java archives in $dir"
find $dir -type f -regextype posix-egrep -regex ".*\.(jar|war|hpi|apk)$" -print0 |
while IFS= read -rd '' f; do
echo "stripping java archive $f"
strip-nondeterminism --type jar "$f"
done
}

View file

@ -5,6 +5,7 @@
, jdk11 , jdk11
, git , git
, xmlstarlet , xmlstarlet
, stripJavaArchivesHook
, xcbuild , xcbuild
, udev , udev
, xorg , xorg
@ -42,13 +43,6 @@ stdenv.mkDerivation {
substituteInPlace gluegen/src/java/com/jogamp/common/util/IOUtil.java \ substituteInPlace gluegen/src/java/com/jogamp/common/util/IOUtil.java \
--replace-fail '#!/bin/true' '#!${coreutils}/bin/true' --replace-fail '#!/bin/true' '#!${coreutils}/bin/true'
'' ''
# set timestamp of files in jar to a fixed point in time
+ ''
xmlstarlet ed --inplace \
--append //jar --type attr -n modificationtime --value 1980-01-01T00:00Z \
gluegen/make/{build.xml,gluegen-cpptasks-base.xml} \
jogl/make/{build.xml,build-nativewindow.xml,build-jogl.xml}
''
# prevent looking for native libraries in /usr/lib # prevent looking for native libraries in /usr/lib
+ '' + ''
substituteInPlace jogl/make/build-*.xml \ substituteInPlace jogl/make/build-*.xml \
@ -72,6 +66,7 @@ stdenv.mkDerivation {
jdk11 jdk11
git git
xmlstarlet xmlstarlet
stripJavaArchivesHook
] ++ lib.optionals stdenv.isDarwin [ ] ++ lib.optionals stdenv.isDarwin [
xcbuild xcbuild
]; ];

View file

@ -6,7 +6,7 @@
, jdk , jdk
, jre , jre
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
let let
@ -30,7 +30,7 @@ stdenv.mkDerivation (finalAttrs: {
jdk jdk
fakeHostname fakeHostname
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -5,7 +5,7 @@
, ant , ant
, jdk8 , jdk8
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
, callPackage , callPackage
}: }:
@ -27,7 +27,7 @@ in stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -4,6 +4,7 @@
, ant , ant
, jdk8 , jdk8
, sharutils , sharutils
, stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -20,16 +21,11 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk8 jdk8
sharutils sharutils
stripJavaArchivesHook
]; ];
sourceRoot = "${finalAttrs.src.name}/freetts-${finalAttrs.version}"; sourceRoot = "${finalAttrs.src.name}/freetts-${finalAttrs.version}";
postPatch = ''
# Fix jar timestamps for reproducibility
substituteInPlace build.xml demo.xml \
--replace-fail '<jar ' '<jar modificationtime="0" '
'';
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild

View file

@ -4,7 +4,7 @@
, ant , ant
, jdk , jdk
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -24,7 +24,7 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -3,7 +3,7 @@
, fetchFromGitHub , fetchFromGitHub
, ant , ant
, jdk , jdk
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation { stdenv.mkDerivation {
@ -20,7 +20,7 @@ stdenv.mkDerivation {
nativeBuildInputs = [ nativeBuildInputs = [
ant ant
jdk jdk
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -1,6 +1,6 @@
{ lib { lib
, stdenv , stdenv
, canonicalize-jars-hook , stripJavaArchivesHook
, fetchzip , fetchzip
, pkg-config , pkg-config
, atk , atk
@ -58,7 +58,7 @@ in stdenv.mkDerivation rec {
}; };
nativeBuildInputs = [ nativeBuildInputs = [
canonicalize-jars-hook stripJavaArchivesHook
pkg-config pkg-config
]; ];
buildInputs = [ buildInputs = [

View file

@ -4,7 +4,7 @@
, ant , ant
, jdk , jdk
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -22,7 +22,7 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -5,7 +5,7 @@
, jdk , jdk
, jre , jre
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -23,7 +23,7 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -1,7 +1,7 @@
{ lib { lib
, stdenv , stdenv
, fetchFromGitHub , fetchFromGitHub
, canonicalize-jars-hook , stripJavaArchivesHook
, cmake , cmake
, cmark , cmark
, Cocoa , Cocoa
@ -40,7 +40,7 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-4VsoxZzi/EfEsnDvvwzg2xhj7j5B+k3gvaSqwJFDweE="; hash = "sha256-4VsoxZzi/EfEsnDvvwzg2xhj7j5B+k3gvaSqwJFDweE=";
}; };
nativeBuildInputs = [ extra-cmake-modules cmake jdk17 ninja canonicalize-jars-hook ]; nativeBuildInputs = [ extra-cmake-modules cmake jdk17 ninja stripJavaArchivesHook ];
buildInputs = buildInputs =
[ [
qtbase qtbase

View file

@ -7,7 +7,7 @@
, makeWrapper , makeWrapper
, makeDesktopItem , makeDesktopItem
, copyDesktopItems , copyDesktopItems
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -28,7 +28,7 @@ stdenv.mkDerivation (finalAttrs: {
jdk8 jdk8
makeWrapper makeWrapper
copyDesktopItems copyDesktopItems
canonicalize-jars-hook stripJavaArchivesHook
]; ];
buildPhase = '' buildPhase = ''

View file

@ -5,7 +5,7 @@
, jdk8 , jdk8
, jre8 , jre8
, makeWrapper , makeWrapper
, canonicalize-jars-hook , stripJavaArchivesHook
}: }:
let let
@ -20,7 +20,7 @@ stdenv.mkDerivation (finalAttrs: {
ant ant
jdk jdk
makeWrapper makeWrapper
canonicalize-jars-hook stripJavaArchivesHook
]; ];
src = fetchFromGitHub { src = fetchFromGitHub {

View file

@ -5,6 +5,7 @@
, jdk , jdk
, jre , jre
, makeWrapper , makeWrapper
, stripJavaArchivesHook
}: }:
stdenv.mkDerivation (finalAttrs: { stdenv.mkDerivation (finalAttrs: {
@ -16,16 +17,11 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-b7Av17wu6Ar/npKOiwYqzlvBFSIuXTpqTacM1sxtBvc="; hash = "sha256-b7Av17wu6Ar/npKOiwYqzlvBFSIuXTpqTacM1sxtBvc=";
}; };
postPatch = ''
# Fix jar timestamps for reproducibility
substituteInPlace fop/build.xml \
--replace-fail '<jar ' '<jar modificationtime="0" '
'';
nativeBuildInputs = [ nativeBuildInputs = [
ant ant
jdk jdk
makeWrapper makeWrapper
stripJavaArchivesHook
]; ];
# Note: not sure if this is needed anymore # Note: not sure if this is needed anymore

View file

@ -147,6 +147,7 @@ mapAliases ({
callPackage_i686 = pkgsi686Linux.callPackage; callPackage_i686 = pkgsi686Linux.callPackage;
cadence = throw "cadence has been removed from nixpkgs, as it was archived upstream"; # Added 2023-10-28 cadence = throw "cadence has been removed from nixpkgs, as it was archived upstream"; # Added 2023-10-28
cask = emacs.pkgs.cask; # Added 2022-11-12 cask = emacs.pkgs.cask; # Added 2022-11-12
canonicalize-jars-hook = stripJavaArchivesHook; # Added 2024-03-17
cargo-embed = throw "cargo-embed is now part of the probe-rs package"; # Added 2023-07-03 cargo-embed = throw "cargo-embed is now part of the probe-rs package"; # Added 2023-07-03
cargo-espflash = espflash; cargo-espflash = espflash;
cargo-flash = throw "cargo-flash is now part of the probe-rs package"; # Added 2023-07-03 cargo-flash = throw "cargo-flash is now part of the probe-rs package"; # Added 2023-07-03

View file

@ -212,11 +212,10 @@ with pkgs;
inherit (darwin.apple_sdk.frameworks) Security; inherit (darwin.apple_sdk.frameworks) Security;
}; };
canonicalize-jar = callPackage ../build-support/java/canonicalize-jar.nix { }; stripJavaArchivesHook = makeSetupHook {
canonicalize-jars-hook = makeSetupHook { name = "strip-java-archives-hook";
name = "canonicalize-jars-hook"; propagatedBuildInputs = [ strip-nondeterminism ];
substitutions = { canonicalize_jar = canonicalize-jar; }; } ../build-support/setup-hooks/strip-java-archives.sh;
} ../build-support/setup-hooks/canonicalize-jars.sh;
ensureNewerSourcesHook = { year }: makeSetupHook { ensureNewerSourcesHook = { year }: makeSetupHook {
name = "ensure-newer-sources-hook"; name = "ensure-newer-sources-hook";