mirror of
https://github.com/tadfisher/gradle2nix.git
synced 2026-01-11 23:40:37 -05:00
Allow overriding dependencies in gradle.nix
This commit is contained in:
209
gradle.nix
209
gradle.nix
@@ -32,16 +32,20 @@
|
||||
, writeTextDir
|
||||
}:
|
||||
|
||||
let defaultGradle = gradle; in
|
||||
|
||||
{
|
||||
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
|
||||
lockFile
|
||||
lockFile ? null
|
||||
, pname ? "project"
|
||||
, version ? null
|
||||
, enableParallelBuilding ? true
|
||||
# The Gradle package to use. Default is 'pkgs.gradle'.
|
||||
, gradle ? defaultGradle
|
||||
# Arguments to Gradle used to build the project in buildPhase.
|
||||
, gradleFlags ? [ "build" ]
|
||||
# Enable debugging for the Gradle build; this will cause Gradle to run a debug server
|
||||
# and wait for a JVM debugging client to attach.
|
||||
# Enable debugging for the Gradle build; this will cause Gradle to run
|
||||
# a debug server and wait for a JVM debugging client to attach.
|
||||
, enableDebug ? false
|
||||
# Additional code to run in the Gradle init script (init.gradle).
|
||||
, extraInit ? ""
|
||||
@@ -65,52 +69,79 @@
|
||||
# };
|
||||
# }
|
||||
, fetchers ? { }
|
||||
# Overlays for dependencies in the offline Maven repository.
|
||||
#
|
||||
# Acceps an attrset of dependencies (usually parsed from 'lockFile'), and produces an attrset
|
||||
# containing dependencies to merge into the final set.
|
||||
#
|
||||
# The attrset is of the form:
|
||||
#
|
||||
# {
|
||||
# "${group}:${module}:${version}" = <derivation>;
|
||||
# # ...
|
||||
# }
|
||||
#
|
||||
# A dependency derivation unpacks multiple source files into a single Maven-style directory named
|
||||
# "${out}/${groupPath}/${module}/${version}/", where 'groupPath' is the dependency group ID with dot
|
||||
# characters ('.') replaced by the path separator ('/').
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# 1. Add or replace a dependency with a single JAR file:
|
||||
#
|
||||
# (_: {
|
||||
# "com.squareup.okio:okio:3.9.0" = fetchurl {
|
||||
# url = "https://repo.maven.apache.org/maven2/com/squareup/okio/okio/3.9.0/okio-3.9.0.jar";
|
||||
# hash = "...";
|
||||
# downloadToTemmp = true;
|
||||
# postFetch = "install -Dt $out/com/squareup/okio/okio/3.9.0/ $downloadedFile"
|
||||
# };
|
||||
# })
|
||||
#
|
||||
# 2. Remove a dependency entirely:
|
||||
#
|
||||
# # This works because the result is filtered for values that are derivations.
|
||||
# (_: {
|
||||
# "org.apache.log4j:core:2.23.1" = null;
|
||||
# })
|
||||
, overlays ? []
|
||||
, ... } @ args:
|
||||
|
||||
let
|
||||
inherit (builtins)
|
||||
attrValues concatStringsSep elemAt filter fromJSON getAttr hasAttr head length match
|
||||
removeAttrs replaceStrings sort;
|
||||
attrValues concatStringsSep elemAt filter fromJSON getAttr head length mapAttrs removeAttrs
|
||||
replaceStrings;
|
||||
|
||||
inherit (lib)
|
||||
assertMsg concatMapStringsSep findFirst foldl' groupBy' hasSuffix hasPrefix last mapAttrs
|
||||
mapAttrsToList optionalAttrs optionalString readFile removeSuffix unique versionAtLeast
|
||||
versionOlder;
|
||||
mapAttrsToList optionalString readFile versionAtLeast versionOlder;
|
||||
|
||||
inherit (lib.strings) sanitizeDerivationName;
|
||||
|
||||
lockedDeps = fromJSON (readFile lockFile);
|
||||
|
||||
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;
|
||||
versionParts = parseVersion version;
|
||||
uniqueVersion = parseVersion version;
|
||||
};
|
||||
|
||||
parseVersion = version:
|
||||
let
|
||||
parts = builtins.split ":" version;
|
||||
base = elemAt parts 0;
|
||||
in
|
||||
{
|
||||
inherit base;
|
||||
exact = base;
|
||||
}
|
||||
// optionalAttrs (length parts >= 2) (
|
||||
let
|
||||
snapshot = elemAt parts 2;
|
||||
exact = replaceStrings [ "-SNAPSHOT" ] [ "-${snapshot}" ] base;
|
||||
parts = builtins.split "-" timestamp;
|
||||
timestamp = findFirst (match "[0-9]{8}\.[0-9]{6}") parts;
|
||||
buildNumber = let lastPart = last parts; in if match "[0-9]+" lastPart then lastPart else null;
|
||||
in
|
||||
{ inherit snapshot exact timestamp buildNumber; }
|
||||
);
|
||||
|
||||
fetchers' = {
|
||||
http = fetchurl;
|
||||
https = fetchurl;
|
||||
@@ -133,7 +164,7 @@ let
|
||||
in
|
||||
stdenv.mkDerivation {
|
||||
pname = sanitizeDerivationName "${coords.group}-${coords.module}";
|
||||
version = coords.versionParts.exact;
|
||||
version = coords.uniqueVersion;
|
||||
|
||||
srcs = mapAttrsToList fetch artifacts;
|
||||
|
||||
@@ -157,9 +188,30 @@ let
|
||||
allowSubstitutes = false;
|
||||
};
|
||||
|
||||
# Intermediate dependency spec.
|
||||
#
|
||||
# We want to allow overriding dependencies via the 'dependencies' function,
|
||||
# so we pass an intermediate set that maps each Maven coordinate to the
|
||||
# derivation created with 'mkModule'. This allows users extra flexibility
|
||||
# to do things like patching native libraries with patchelf or replacing
|
||||
# artifacts entirely.
|
||||
lockedDependencies = final: if lockFile == null then {} else
|
||||
let
|
||||
lockedDependencySpecs = fromJSON (readFile lockFile);
|
||||
in mapAttrs mkModule lockedDependencySpecs;
|
||||
|
||||
|
||||
finalDependencies =
|
||||
let
|
||||
composedExtension = lib.composeManyExtensions overlays;
|
||||
extended = lib.extends composedExtension lockedDependencies;
|
||||
fixed = lib.fix extended;
|
||||
in
|
||||
filter lib.isDerivation (attrValues fixed);
|
||||
|
||||
offlineRepo = symlinkJoin {
|
||||
name = if version != null then "${pname}-${version}-gradle-repo" else "${pname}-gradle-repo";
|
||||
paths = mapAttrsToList mkModule lockedDeps;
|
||||
paths = finalDependencies;
|
||||
};
|
||||
|
||||
initScript =
|
||||
@@ -227,64 +279,53 @@ let
|
||||
${extraInit}
|
||||
'';
|
||||
|
||||
buildProject = flags: ''
|
||||
gradle --offline --no-daemon --no-build-cache \
|
||||
--info --full-stacktrace --warning-mode=all \
|
||||
--no-configuration-cache \
|
||||
-Dmaven.repo.local=${offlineRepo} \
|
||||
${optionalString enableParallelBuilding "--parallel"} \
|
||||
${optionalString enableDebug "-Dorg.gradle.debug=true"} \
|
||||
${optionalString (buildJdk != null) "-Dorg.gradle.java.home=${buildJdk.home}"} \
|
||||
--init-script ${initScript} \
|
||||
${concatStringsSep " " flags}
|
||||
'';
|
||||
|
||||
in stdenv.mkDerivation ({
|
||||
buildGradle = stdenv.mkDerivation (finalAttrs: {
|
||||
|
||||
dontStrip = true;
|
||||
inherit buildJdk enableParallelBuilding enableDebug gradle gradleFlags pname version;
|
||||
|
||||
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ gradle ];
|
||||
dontStrip = true;
|
||||
|
||||
buildPhase = args.buildPhase or ''
|
||||
runHook preBuild
|
||||
nativeBuildInputs = [ finalAttrs.gradle ]
|
||||
++ lib.optional (finalAttrs.buildJdk != null) finalAttrs.buildJdk;
|
||||
|
||||
(
|
||||
set -eux
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
|
||||
${optionalString (versionOlder gradle.version "8.0") ''
|
||||
# Work around https://github.com/gradle/gradle/issues/1055
|
||||
TMPHOME="$(mktemp -d)"
|
||||
mkdir -p "$TMPHOME/init.d"
|
||||
export GRADLE_USER_HOME="$TMPHOME"
|
||||
cp ${initScript} $TMPHOME/
|
||||
''}
|
||||
(
|
||||
set -eux
|
||||
|
||||
gradle --offline --no-daemon --no-build-cache \
|
||||
--info --full-stacktrace --warning-mode=all \
|
||||
--no-configuration-cache --console=plain \
|
||||
-Dmaven.repo.local=${offlineRepo} \
|
||||
${optionalString enableParallelBuilding "--parallel"} \
|
||||
${optionalString enableDebug "-Dorg.gradle.debug=true"} \
|
||||
${optionalString (buildJdk != null) "-Dorg.gradle.java.home=${buildJdk.home}"} \
|
||||
--init-script ${initScript} \
|
||||
${concatStringsSep " " gradleFlags}
|
||||
)
|
||||
${optionalString (versionOlder finalAttrs.gradle.version "8.0") ''
|
||||
# Work around https://github.com/gradle/gradle/issues/1055
|
||||
TMPHOME="$(mktemp -d)"
|
||||
mkdir -p "$TMPHOME/init.d"
|
||||
export GRADLE_USER_HOME="$TMPHOME"
|
||||
cp ${initScript} $TMPHOME/
|
||||
''}
|
||||
|
||||
runHook postBuild
|
||||
'';
|
||||
gradle --offline --no-daemon --no-build-cache \
|
||||
--info --full-stacktrace --warning-mode=all \
|
||||
--no-configuration-cache --console=plain \
|
||||
-Dmaven.repo.local=${offlineRepo} \
|
||||
${optionalString finalAttrs.enableParallelBuilding "--parallel"} \
|
||||
${optionalString finalAttrs.enableDebug "-Dorg.gradle.debug=true"} \
|
||||
${optionalString (finalAttrs.buildJdk != null) "-Dorg.gradle.java.home=${finalAttrs.buildJdk.home}"} \
|
||||
--init-script ${initScript} \
|
||||
${concatStringsSep " " finalAttrs.gradleFlags}
|
||||
)
|
||||
|
||||
passthru = (args.passthru or {}) // {
|
||||
inherit offlineRepo;
|
||||
};
|
||||
runHook postBuild
|
||||
'';
|
||||
|
||||
} // (removeAttrs args [
|
||||
"nativeBuildInputs"
|
||||
"passthru"
|
||||
"lockFile"
|
||||
"gradleFlags"
|
||||
"gradle"
|
||||
"enableDebug"
|
||||
"extraInit"
|
||||
"buildJdk"
|
||||
"fetchers"
|
||||
]))
|
||||
passthru = {
|
||||
inherit offlineRepo;
|
||||
};
|
||||
} // removeAttrs args [
|
||||
"lockFile"
|
||||
"extraInit"
|
||||
"fetchers"
|
||||
"overlays"
|
||||
]);
|
||||
|
||||
in
|
||||
buildGradle
|
||||
|
||||
Reference in New Issue
Block a user