gradle-env.nix: Support fetchers per URL scheme

This commit is contained in:
Tad Fisher
2021-02-03 17:01:28 -08:00
parent 56c4bb435d
commit 7f7d6a888b

View File

@@ -19,7 +19,15 @@
# '';
# }
{ stdenv, buildEnv, fetchurl, gradleGen, writeText, writeTextDir }:
{ lib
, stdenv
, buildEnv
, fetchs3
, fetchurl
, gradleGen
, writeText
, writeTextDir
}:
{
# Path to the environment spec generated by gradle2nix (e.g. gradle-env.json).
@@ -38,29 +46,68 @@
, 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, sha256 }` and fetch into the Nix store. For example:
#
# {
# s3 = { urls, sha256 }: fetchs3 {
# s3url = builtins.head urls;
# inherit sha256;
# region = "us-west-2";
# credentials = {
# access_key_id = "foo";
# secret_access_key = "bar";
# };
# };
# }
, fetchers ? { }
, ... } @ args:
let
inherit (builtins)
attrValues concatStringsSep filter fromJSON match replaceStrings sort;
attrValues concatStringsSep filter fromJSON getAttr head match
replaceStrings sort;
inherit (stdenv.lib)
assertMsg concatMapStringsSep groupBy' hasSuffix last mapAttrs
inherit (lib)
assertMsg concatMapStringsSep groupBy' hasSuffix hasPrefix last mapAttrs
mapAttrsToList optionalString readFile removeSuffix unique versionAtLeast
versionOlder;
mkDep = depSpec: stdenv.mkDerivation {
inherit (depSpec) name;
fetchers' = {
http = fetchurl;
https = fetchurl;
s3 = { urls, sha256 }: fetchs3 {
s3url = head urls;
inherit sha256;
};
} // fetchers;
src = fetchurl {
inherit (depSpec) urls sha256;
# 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 = { urls, sha256 }:
let
first = head urls;
scheme = head (builtins.match "([a-z0-9+.-]+)://.*" first);
fetch' = getAttr scheme fetchers';
urls' = filter (hasPrefix scheme) urls;
in
fetch' { urls = urls'; inherit sha256; };
mkDep = { name, path, urls, sha256, ... }: stdenv.mkDerivation {
inherit name;
src = fetch {
inherit urls sha256;
};
phases = "installPhase";
installPhase = ''
mkdir -p $out/${depSpec.path}
ln -s $src $out/${depSpec.path}/${depSpec.name}
mkdir -p $out/${path}
ln -s $src $out/${path}/${name}
'';
};