From 7f7d6a888bca28a96593ad98405fe799b21368c2 Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Wed, 3 Feb 2021 17:01:28 -0800 Subject: [PATCH] gradle-env.nix: Support fetchers per URL scheme --- gradle-env.nix | 67 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/gradle-env.nix b/gradle-env.nix index b3deb57..b512f90 100644 --- a/gradle-env.nix +++ b/gradle-env.nix @@ -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} ''; };