mirror of
https://github.com/tadfisher/gradle2nix.git
synced 2026-01-11 15:30:38 -05:00
239 lines
6.5 KiB
Nix
239 lines
6.5 KiB
Nix
# 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,
|
|
writeText,
|
|
}:
|
|
|
|
let
|
|
defaultGradle = gradle;
|
|
in
|
|
|
|
{
|
|
# Path to the lockfile generated by gradle2nix (e.g. gradle.lock).
|
|
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.
|
|
enableDebug ? false,
|
|
# Additional code to run in the Gradle init script (init.gradle).
|
|
extraInit ? "",
|
|
# Override the default JDK used to run Gradle itself.
|
|
buildJdk ? null,
|
|
# Override functions which fetch dependency artifacts.
|
|
# Keys 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 ? { },
|
|
# 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) removeAttrs;
|
|
|
|
inherit (lib) versionAtLeast versionOlder;
|
|
|
|
offlineRepo = buildMavenRepo {
|
|
inherit
|
|
lockFile
|
|
pname
|
|
version
|
|
fetchers
|
|
overlays
|
|
;
|
|
};
|
|
|
|
initScript = writeText "init.gradle" ''
|
|
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:${offlineRepo}'
|
|
repo.metadataSources {
|
|
gradleMetadata()
|
|
mavenPom()
|
|
artifact()
|
|
}
|
|
} else if (repo instanceof IvyArtifactRepository) {
|
|
repo.url 'file:${offlineRepo}'
|
|
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)
|
|
}
|
|
|
|
${extraInit}
|
|
'';
|
|
|
|
buildGradlePackage = stdenv.mkDerivation (
|
|
finalAttrs:
|
|
{
|
|
|
|
inherit
|
|
buildJdk
|
|
enableParallelBuilding
|
|
enableDebug
|
|
gradle
|
|
gradleFlags
|
|
pname
|
|
version
|
|
;
|
|
|
|
dontStrip = true;
|
|
|
|
nativeBuildInputs = [ finalAttrs.gradle ];
|
|
|
|
buildPhase =
|
|
let
|
|
finalGradleFlags =
|
|
[
|
|
"--console=plain"
|
|
"--no-build-cache"
|
|
"--no-configuration-cache"
|
|
"--no-daemon"
|
|
"--no-watch-fs"
|
|
"--offline"
|
|
]
|
|
++ lib.optional (finalAttrs.buildJdk != null) "-Dorg.gradle.java.home=${finalAttrs.buildJdk.home}"
|
|
++ lib.optional finalAttrs.enableDebug "-Dorg.gradle.debug=true"
|
|
++ lib.optional finalAttrs.enableParallelBuilding "--parallel"
|
|
++ lib.optional (versionAtLeast finalAttrs.gradle.version "8.0") "--init-script=${initScript}"
|
|
++ finalAttrs.gradleFlags;
|
|
in
|
|
''
|
|
runHook preBuild
|
|
|
|
(
|
|
set -eux
|
|
|
|
export NIX_OFFLINE_REPO='${offlineRepo}'
|
|
export GRADLE_USER_HOME="$(mktemp -d)"
|
|
|
|
${lib.optionalString (versionOlder finalAttrs.gradle.version "8.0") ''
|
|
# Work around https://github.com/gradle/gradle/issues/1055
|
|
mkdir -p "$GRADLE_USER_HOME/init.d"
|
|
ln -s ${initScript} "$GRADLE_USER_HOME/init.d/nix-init.gradle"
|
|
''}
|
|
|
|
gradle ${builtins.toString finalGradleFlags}
|
|
)
|
|
|
|
runHook postBuild
|
|
'';
|
|
|
|
passthru = {
|
|
inherit offlineRepo;
|
|
};
|
|
}
|
|
// removeAttrs args [
|
|
"lockFile"
|
|
"extraInit"
|
|
"fetchers"
|
|
"overlays"
|
|
]
|
|
);
|
|
in
|
|
buildGradlePackage
|