Miscellaneous cleanup

This commit is contained in:
Tad Fisher
2024-05-29 17:47:31 -07:00
parent d13b7b0d6d
commit 301c64fa2f
46 changed files with 1512 additions and 1441 deletions

View File

@@ -19,123 +19,143 @@
# '';
# }
{ lib
, stdenv
, buildEnv
, fetchs3
, fetchurl
, gradle
, maven
, runCommandLocal
, symlinkJoin
, writeText
, writeTextDir
{
lib,
stdenv,
buildEnv,
fetchs3,
fetchurl,
gradle,
maven,
runCommandLocal,
symlinkJoin,
writeText,
writeTextDir,
}:
let defaultGradle = gradle; in
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:
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)
attrValues concatStringsSep elemAt filter fromJSON getAttr head length mapAttrs removeAttrs
replaceStrings;
attrValues
concatStringsSep
elemAt
filter
fromJSON
getAttr
head
length
mapAttrs
removeAttrs
replaceStrings
;
inherit (lib)
mapAttrsToList optionalString readFile versionAtLeast versionOlder;
mapAttrsToList
optionalString
readFile
versionAtLeast
versionOlder
;
inherit (lib.strings) sanitizeDerivationName;
toCoordinates = id:
toCoordinates =
id:
let
coords = builtins.split ":" id;
parseVersion = version:
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 {
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;
@@ -150,43 +170,46 @@ let
# Fetch urls using the scheme for the first entry only; there isn't a
# straightforward way to tell Nix to try multiple fetchers in turn
# and short-circuit on the first successful fetch.
fetch = name: { url, hash }:
fetch =
name:
{ url, hash }:
let
scheme = head (builtins.match "([a-z0-9+.-]+)://.*" url);
fetch' = getAttr scheme fetchers';
in
fetch' { inherit url hash; };
fetch' { inherit url hash; };
mkModule = id: artifacts:
mkModule =
id: artifacts:
let
coords = toCoordinates id;
modulePath = "${replaceStrings ["."] ["/"] coords.group}/${coords.module}/${coords.version}";
modulePath = "${replaceStrings [ "." ] [ "/" ] coords.group}/${coords.module}/${coords.version}";
in
stdenv.mkDerivation {
pname = sanitizeDerivationName "${coords.group}-${coords.module}";
version = coords.uniqueVersion;
stdenv.mkDerivation {
pname = sanitizeDerivationName "${coords.group}-${coords.module}";
version = coords.uniqueVersion;
srcs = mapAttrsToList fetch artifacts;
srcs = mapAttrsToList fetch artifacts;
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
dontInstall = true;
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
dontInstall = true;
preUnpack = ''
mkdir -p "$out/${modulePath}"
'';
preUnpack = ''
mkdir -p "$out/${modulePath}"
'';
unpackCmd = ''
cp "$curSrc" "$out/${modulePath}/$(stripHash "$curSrc")"
'';
unpackCmd = ''
cp "$curSrc" "$out/${modulePath}/$(stripHash "$curSrc")"
'';
sourceRoot = ".";
sourceRoot = ".";
preferLocalBuild = true;
allowSubstitutes = false;
};
preferLocalBuild = true;
allowSubstitutes = false;
};
# Intermediate dependency spec.
#
@@ -195,11 +218,15 @@ let
# 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;
lockedDependencies =
final:
if lockFile == null then
{ }
else
let
lockedDependencySpecs = fromJSON (readFile lockFile);
in
mapAttrs mkModule lockedDependencySpecs;
finalDependencies =
let
@@ -207,7 +234,7 @@ let
extended = lib.extends composedExtension lockedDependencies;
fixed = lib.fix extended;
in
filter lib.isDerivation (attrValues fixed);
filter lib.isDerivation (attrValues fixed);
offlineRepo = symlinkJoin {
name = if version != null then "${pname}-${version}-gradle-repo" else "${pname}-gradle-repo";
@@ -261,54 +288,68 @@ let
${extraInit}
'';
buildGradlePackage = stdenv.mkDerivation (finalAttrs: {
buildGradlePackage = stdenv.mkDerivation (
finalAttrs:
{
inherit buildJdk enableParallelBuilding enableDebug gradle gradleFlags pname version;
inherit
buildJdk
enableParallelBuilding
enableDebug
gradle
gradleFlags
pname
version
;
dontStrip = true;
dontStrip = true;
nativeBuildInputs = [ finalAttrs.gradle ]
++ lib.optional (finalAttrs.buildJdk != null) finalAttrs.buildJdk;
nativeBuildInputs = [
finalAttrs.gradle
] ++ lib.optional (finalAttrs.buildJdk != null) finalAttrs.buildJdk;
buildPhase = ''
runHook preBuild
buildPhase = ''
runHook preBuild
(
set -eux
(
set -eux
export NIX_OFFLINE_REPO='${offlineRepo}'
export NIX_OFFLINE_REPO='${offlineRepo}'
${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/
''}
${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/
''}
gradle --offline --no-daemon --no-build-cache --no-watch-fs \
--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}
)
gradle --offline --no-daemon --no-build-cache --no-watch-fs \
--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}
)
runHook postBuild
'';
passthru = {
inherit offlineRepo;
};
} // removeAttrs args [
"lockFile"
"extraInit"
"fetchers"
"overlays"
]);
runHook postBuild
'';
passthru = {
inherit offlineRepo;
};
}
// removeAttrs args [
"lockFile"
"extraInit"
"fetchers"
"overlays"
]
);
in
buildGradlePackage