Skip to content

Commit 1521b76

Browse files
committed
Check hashes before linking store references
1 parent 45be5a6 commit 1521b76

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Using the builder function should feel idiomatic to Nix. It should provide the same experience as `buildPythonPackage` or `buildPerlPackage` but with fewer options.
1313
- The rules imposed on the Gradle build should be idiomatic to Gradle and ideally promote Gradle best practices.
1414
- Support automatic updates with tools such as renovate.
15-
- All dependencies (jars) should be packaged into discrete derivations to facilitate efficient deployments and [layered OIC images](https://ryantm.github.io/nixpkgs/builders/images/dockertools/#ssec-pkgs-dockerTools-buildLayeredImage).
15+
- All dependencies (jars) should be packaged into discrete derivations (and linked in the final result) to facilitate efficient deployments and [layered OIC images](https://ryantm.github.io/nixpkgs/builders/images/dockertools/#ssec-pkgs-dockerTools-buildLayeredImage).
1616
- This project should be small and simple.
1717

1818
## Non-Goals

buildGradleApplication/default.nix

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,34 @@
2626

2727
# Prepare a script that will replace that jars with references into the NIX store.
2828
linkScript = writeShellScript "link-to-jars" ''
29-
declare -A depsByName
29+
declare -A fileByName
30+
declare -A hashByName
3031
${
3132
lib.concatMapStringsSep "\n"
32-
(dep: "depsByName[\"${dep.name}\"]=\"${builtins.toString dep.jar}\"")
33+
(dep: "fileByName[\"${dep.name}\"]=\"${builtins.toString dep.jar}\"\nhashByName[\"${dep.name}\"]=\"${builtins.toString dep.hash}\"")
3334
(builtins.filter (dep: (lib.strings.hasSuffix ".jar" dep.name && !lib.strings.hasSuffix "-javadoc.jar" dep.name && !lib.strings.hasSuffix "-sources.jar" dep.name)) m2Repository.dependencies)
3435
}
3536
3637
for jar in "$1"/*.jar; do
37-
dep=''${depsByName[$(basename "$jar")]}
38+
dep=''${fileByName[$(basename "$jar")]}
3839
if [[ -n "$dep" ]]; then
39-
echo "Replacing $jar with nix store reference $dep"
40-
rm "$jar"
41-
ln -s "$dep" "$jar"
40+
jarHash=$(sha256sum "$jar" | cut -c -64)
41+
sriHash=''${hashByName[$(basename "$jar")]}
42+
if [[ $sriHash == sha256-* ]]; then
43+
referenceHash="$(echo ''${sriHash#sha256-} | base64 -d | ${pkgs.hexdump}/bin/hexdump -v -e '/1 "%02x"')"
44+
else
45+
referenceHash=$(sha256sum "$dep" | cut -c -64)
46+
fi
47+
48+
if [[ "$referenceHash" == "$jarHash" ]]; then
49+
echo "Replacing $jar with nix store reference $dep"
50+
rm "$jar"
51+
ln -s "$dep" "$jar"
52+
else
53+
echo "Hash of $jar differs from expected store reference $dep"
54+
fi
55+
else
56+
echo "No linking candidate found for $jar"
4257
fi
4358
done
4459
'';
@@ -71,6 +86,7 @@
7186
7287
mkdir -p $out/lib/
7388
mv lib/*.jar $out/lib/
89+
echo ${linkScript} $out/lib/
7490
${linkScript} $out/lib/
7591
7692
if [ -d agent-libs/ ]; then

0 commit comments

Comments
 (0)