Move Gradle build logic to a setup hook

This commit is contained in:
Tad Fisher
2024-06-13 15:21:58 -07:00
parent a4ef499401
commit b32bf21a6c
26 changed files with 768 additions and 470 deletions

View File

@@ -0,0 +1,145 @@
# This file is generated by gradle2nix.
#
# Example usage (e.g. in default.nix):
#
# with (import <nixpkgs> {});
# let
# buildGradle = callPackage ./gradle.nix {};
# in
# buildGradle {
# lockFile = ./gradle.lock;
#
# src = ./.;
#
# gradleFlags = [ "installDist" ];
#
# installPhase = ''
# mkdir -p $out
# cp -r app/build/install/myproject $out
# '';
# }
{
lib,
stdenv,
gradle,
buildMavenRepo,
gradleSetupHook,
writeText,
}:
{
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
lockFile ? null,
pname ? "project",
version ? null,
# The Gradle package to use. Default is 'pkgs.gradle'.
gradlePackage ? gradle,
# Override the default JDK used to run Gradle itself.
buildJdk ? null,
# Override functions which fetch dependency artifacts.
# Names in this set are URL schemes such as "https" or "s3".
# Values are functions which take a dependency in the form
# `{ urls, hash }` and fetch into the Nix store. For example:
#
# {
# s3 = { name, urls, hash }: fetchs3 {
# s3url = builtins.head urls;
# # TODO This doesn't work without patching fetchs3 to accept SRI hashes
# inherit name hash;
# region = "us-west-2";
# credentials = {
# access_key_id = "foo";
# secret_access_key = "bar";
# };
# };
# }
fetchers ? { },
# Override artifacts in the offline Maven repository.
#
# This is an attrset is of the form:
#
# {
# "${group}:${module}:${version}" = {
# "${filename}" = <override function>;
# }
# }
#
# The override function takes the original derivation from 'fetchers' (e.g. the result of
# 'fetchurl') and produces a new derivation to replace it.
#
# Examples:
#
# 1. Replace a dependency's JAR artifact:
#
# {
# "com.squareup.okio:okio:3.9.0"."okio-3.9.0.jar" = _: fetchurl {
# url = "https://repo.maven.apache.org/maven2/com/squareup/okio/okio/3.9.0/okio-3.9.0.jar";
# hash = "...";
# downloadToTemp = true;
# postFetch = "install -Dt $out/com/squareup/okio/okio/3.9.0/ $downloadedFile"
# };
# }
#
# 2. Patch a JAR containing native binaries:
#
# {
# "com.android.tools.build:aapt2:8.5.0-rc02-11315950" = {
# "aapt2-8.5.0-rc02-11315950-linux.jar" = src: runCommandCC src.name {
# nativeBuildInputs = [ jdk autoPatchelfHook ];
# dontAutoPatchelf = true;
# } ''
# cp ${src} aapt2.jar
# jar xf aapt2.jar aapt2
# chmod +x aapt2
# autoPatchelf aapt2
# jar uf aapt2.jar aapt2
# cp aapt2.jar $out
# '';
# }
# }
overrides ? { },
...
}@args:
let
inherit (builtins) removeAttrs;
gradleSetupHook' = gradleSetupHook.overrideAttrs (_: {
propagatedBuildInputs = [ gradlePackage ];
});
offlineRepo =
if lockFile != null then buildMavenRepo { inherit lockFile fetchers overrides; } else null;
buildGradlePackage = stdenv.mkDerivation (
finalAttrs:
{
inherit buildJdk pname version;
inherit (offlineRepo) gradleInitScript;
dontStrip = true;
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ gradleSetupHook' ];
gradleFlags =
[ "--console=plain" ]
++ lib.optional (finalAttrs.buildJdk != null) "-Dorg.gradle.java.home=${finalAttrs.buildJdk.home}";
passthru =
lib.optionalAttrs (offlineRepo != null) { inherit offlineRepo; } // (args.passthru or { });
}
// removeAttrs args [
"gradle"
"gradleInitScript"
"lockFile"
"fetchers"
"nativeBuildInputs"
"overlays"
"passthru"
]
);
in
buildGradlePackage

189
nix/build-maven-repo.nix Normal file
View File

@@ -0,0 +1,189 @@
{
lib,
stdenv,
fetchurl,
substitute,
symlinkJoin,
}:
{
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
lockFile,
# Override functions which fetch dependency artifacts.
# Names in this set are URL schemes such as "https" or "s3".
# Values are functions which take a dependency in the form
# `{ urls, hash }` and fetch into the Nix store. For example:
#
# {
# s3 = { name, urls, hash }: fetchs3 {
# s3url = builtins.head urls;
# # TODO This doesn't work without patching fetchs3 to accept SRI hashes
# inherit name hash;
# region = "us-west-2";
# credentials = {
# access_key_id = "foo";
# secret_access_key = "bar";
# };
# };
# }
fetchers ? { },
# Override artifacts in the offline Maven repository.
#
# This is an attrset is of the form:
#
# {
# "${group}:${module}:${version}" = {
# "${filename}" = <override function>;
# }
# }
#
# The override function takes the original derivation from 'fetchers' (e.g. the result of
# 'fetchurl') and produces a new derivation to replace it.
#
# Examples:
#
# 1. Replace a dependency's JAR artifact:
#
# {
# "com.squareup.okio:okio:3.9.0"."okio-3.9.0.jar" = _: fetchurl {
# url = "https://repo.maven.apache.org/maven2/com/squareup/okio/okio/3.9.0/okio-3.9.0.jar";
# hash = "...";
# downloadToTemp = true;
# postFetch = "install -Dt $out/com/squareup/okio/okio/3.9.0/ $downloadedFile"
# };
# }
#
# 2. Patch a JAR containing native binaries:
#
# {
# "com.android.tools.build:aapt2:8.5.0-rc02-11315950" = {
# "aapt2-8.5.0-rc02-11315950-linux.jar" = src: runCommandCC src.name {
# nativeBuildInputs = [ jdk autoPatchelfHook ];
# dontAutoPatchelf = true;
# } ''
# cp ${src} aapt2.jar
# jar xf aapt2.jar aapt2
# chmod +x aapt2
# autoPatchelf aapt2
# jar uf aapt2.jar aapt2
# cp aapt2.jar $out
# '';
# }
# }
overrides ? { },
}:
let
inherit (builtins)
attrValues
elemAt
filter
fromJSON
getAttr
head
length
mapAttrs
replaceStrings
;
inherit (lib) mapAttrsToList readFile;
inherit (lib.strings) sanitizeDerivationName;
toCoordinates =
id:
let
coords = builtins.split ":" id;
parseVersion =
version:
let
parts = builtins.split ":" version;
base = elemAt parts 0;
in
if length parts >= 2 then
let
snapshot = elemAt parts 2;
in
replaceStrings [ "-SNAPSHOT" ] [ "-${snapshot}" ] base
else
base;
in
rec {
group = elemAt coords 0;
module = elemAt coords 2;
version = elemAt coords 4;
uniqueVersion = parseVersion version;
};
fetchers' = {
http = fetchurl;
https = fetchurl;
} // fetchers;
fetch =
coords: overrides: name:
{ url, hash }:
let
scheme = head (builtins.match "([a-z0-9+.-]+)://.*" url);
fetch' = getAttr scheme fetchers';
artifact = fetch' { inherit url hash; };
override = overrides.name or lib.id;
in
override artifact;
mkModule =
id: artifacts:
let
coords = toCoordinates id;
modulePath = "${replaceStrings [ "." ] [ "/" ] coords.group}/${coords.module}/${coords.version}";
moduleOverrides = overrides.${id} or { };
in
stdenv.mkDerivation {
pname = sanitizeDerivationName "${coords.group}-${coords.module}";
version = coords.uniqueVersion;
srcs = mapAttrsToList (fetch coords moduleOverrides) artifacts;
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
dontInstall = true;
preUnpack = ''
mkdir -p "$out/${modulePath}"
'';
unpackCmd = ''
cp "$curSrc" "$out/${modulePath}/$(stripHash "$curSrc")"
'';
sourceRoot = ".";
preferLocalBuild = true;
allowSubstitutes = false;
};
modulePaths =
let
dependencies = fromJSON (readFile lockFile);
modules = mapAttrs mkModule dependencies;
in
filter lib.isDerivation (attrValues modules);
mavenRepo = symlinkJoin {
name = "gradle-maven-repo";
paths = modulePaths;
passthru.gradleInitScript = substitute {
src = ./init.gradle;
substitutions = [
"--replace"
"@mavenRepo@"
"${mavenRepo}"
];
};
};
in
mavenRepo

42
nix/init.gradle Normal file
View File

@@ -0,0 +1,42 @@
import org.gradle.util.GradleVersion
static boolean versionAtLeast(String version) {
return GradleVersion.current() >= GradleVersion.version(version)
}
static void configureRepos(RepositoryHandler repositories) {
repositories.configureEach { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
repo.setArtifactUrls(new HashSet<URI>())
repo.url 'file:@mavenRepo@'
repo.metadataSources {
gradleMetadata()
mavenPom()
artifact()
}
} else if (repo instanceof IvyArtifactRepository) {
repo.url 'file:@mavenRepo@'
repo.layout('maven')
repo.metadataSources {
gradleMetadata()
ivyDescriptor()
artifact()
}
} else if (repo instanceof UrlArtifactRepository) {
repo.url 'file:/homeless-shelter'
}
}
}
beforeSettings { settings ->
configureRepos(settings.pluginManagement.repositories)
configureRepos(settings.buildscript.repositories)
if (versionAtLeast("6.8")) {
configureRepos(settings.dependencyResolutionManagement.repositories)
}
}
beforeProject { project ->
configureRepos(project.buildscript.repositories)
configureRepos(project.repositories)
}

125
nix/setup-hook.sh Normal file
View File

@@ -0,0 +1,125 @@
# shellcheck shell=bash disable=SC2206,SC2155
gradleConfigurePhase() {
runHook preConfigure
if ! [[ -v enableParallelBuilding ]]; then
enableParallelBuilding=1
echo "gradle: enabled parallel building"
fi
if ! [[ -v enableParallelChecking ]]; then
enableParallelChecking=1
echo "gradle: enabled parallel checking"
fi
if ! [[ -v enableParallelInstalling ]]; then
enableParallelInstalling=1
echo "gradle: enabled parallel installing"
fi
export GRADLE_USER_HOME="$(mktemp -d)"
if [ -n "$gradleInitScript" ]; then
if [ ! -f "$gradleInitScript" ]; then
echo "gradleInitScript is not a file path: $gradleInitScript"
exit 1
fi
mkdir -p "$GRADLE_USER_HOME/init.d"
ln -s "$gradleInitScript" "$GRADLE_USER_HOME/init.d"
fi
runHook postConfigure
}
gradleBuildPhase() {
runHook preBuild
if [ -z "${gradleBuildFlags:-}" ] && [ -z "${gradleBuildFlagsArray[*]}" ]; then
echo "gradleBuildFlags is not set, doing nothing"
else
local flagsArray=(
$gradleFlags "${gradleFlagsArray[@]}"
$gradleBuildFlags "${gradleBuildFlagsArray[@]}"
)
if [ -n "$enableParallelBuilding" ]; then
flagsArray+=(--parallel --max-workers ${NIX_BUILD_CORES})
else
flagsArray+=(--no-parallel)
fi
echoCmd 'gradleBuildPhase flags' "${flagsArray[@]}"
gradle "${flagsArray[@]}"
fi
runHook postBuild
}
gradleCheckPhase() {
runHook preCheck
if [ -z "${gradleCheckFlags:-}" ] && [ -z "${gradleCheckFlagsArray[*]}" ]; then
echo "gradleCheckFlags is not set, doing nothing"
else
local flagsArray=(
$gradleFlags "${gradleFlagsArray[@]}"
$gradleCheckFlags "${gradleCheckFlagsArray[@]}"
${gradleCheckTasks:-check}
)
if [ -n "$enableParallelChecking" ]; then
flagsArray+=(--parallel --max-workers ${NIX_BUILD_CORES})
else
flagsArray+=(--no-parallel)
fi
echoCmd 'gradleCheckPhase flags' "${flagsArray[@]}"
gradle "${flagsArray[@]}"
fi
runHook postCheck
}
gradleInstallPhase() {
runHook preInstall
if [ -z "${gradleInstallFlags:-}" ] && [ -z "${gradleInstallFlagsArray[*]}" ]; then
echo "gradleInstallFlags is not set, doing nothing"
else
local flagsArray=(
$gradleFlags "${gradleFlagsArray[@]}"
$gradleInstallFlags "${gradleInstallFlagsArray[@]}"
)
if [ -n "$enableParallelInstalling" ]; then
flagsArray+=(--parallel --max-workers ${NIX_BUILD_CORES})
else
flagsArray+=(--no-parallel)
fi
echoCmd 'gradleInstallPhase flags' "${flagsArray[@]}"
gradle "${flagsArray[@]}"
fi
runHook postInstall
}
if [ -z "${dontUseGradleConfigure-}" ] && [ -z "${configurePhase-}" ]; then
configurePhase=gradleConfigurePhase
fi
if [ -z "${dontUseGradleBuild-}" ] && [ -z "${buildPhase-}" ]; then
buildPhase=gradleBuildPhase
fi
if [ -z "${dontUseGradleCheck-}" ] && [ -z "${checkPhase-}" ]; then
checkPhase=gradleCheckPhase
fi
if [ -z "${dontUseGradleInstall-}" ] && [ -z "${installPhase-}" ]; then
installPhase=gradleInstallPhase
fi