fetchedMavenDeps: honor HTTP[S]_PROXY, NO_PROXY env vars

Maven doesn't honor HTTP[S]_PROXY and NO_PROXY env vars out of the box.
Instead, it expects the user to configure a settings.xml file.
We however impurely pass only these env vars in FODs.
This creates the XML file on demand, if one or more env vars is set.

+103 -4
+17 -4
pkgs/by-name/ma/maven/build-maven-package.nix
··· 3 3 stdenv, 4 4 jdk, 5 5 maven, 6 + writers, 6 7 }: 7 8 8 9 { ··· 28 29 29 30 let 30 31 mvnSkipTests = lib.optionalString (!doCheck) "-DskipTests"; 32 + 33 + writeProxySettings = writers.writePython3 "write-proxy-settings" { } ./maven-proxy.py; 34 + 31 35 fetchedMavenDeps = stdenv.mkDerivation ( 32 36 { 33 37 name = "${pname}-${version}-maven-deps"; ··· 44 48 buildPhase = 45 49 '' 46 50 runHook preBuild 51 + 52 + MAVEN_EXTRA_ARGS="" 53 + 54 + # handle proxy 55 + if [[ -n "''${HTTP_PROXY-}" ]] || [[ -n "''${HTTPS_PROXY-}" ]] || [[ -n "''${NO_PROXY-}" ]];then 56 + mvnSettingsFile="$(mktemp -d)/settings.xml" 57 + ${writeProxySettings} $mvnSettingsFile 58 + MAVEN_EXTRA_ARGS="-s=$mvnSettingsFile" 59 + fi 47 60 '' 48 61 + lib.optionalString buildOffline '' 49 - mvn de.qaware.maven:go-offline-maven-plugin:1.2.8:resolve-dependencies -Dmaven.repo.local=$out/.m2 ${mvnDepsParameters} 62 + mvn $MAVEN_EXTRA_ARGS de.qaware.maven:go-offline-maven-plugin:1.2.8:resolve-dependencies -Dmaven.repo.local=$out/.m2 ${mvnDepsParameters} 50 63 51 64 for artifactId in ${builtins.toString manualMvnArtifacts} 52 65 do 53 66 echo "downloading manual $artifactId" 54 - mvn dependency:get -Dartifact="$artifactId" -Dmaven.repo.local=$out/.m2 67 + mvn $MAVEN_EXTRA_ARGS dependency:get -Dartifact="$artifactId" -Dmaven.repo.local=$out/.m2 55 68 done 56 69 57 70 for artifactId in ${builtins.toString manualMvnSources} ··· 59 72 group=$(echo $artifactId | cut -d':' -f1) 60 73 artifact=$(echo $artifactId | cut -d':' -f2) 61 74 echo "downloading manual sources $artifactId" 62 - mvn dependency:sources -DincludeGroupIds="$group" -DincludeArtifactIds="$artifact" -Dmaven.repo.local=$out/.m2 75 + mvn $MAVEN_EXTRA_ARGS dependency:sources -DincludeGroupIds="$group" -DincludeArtifactIds="$artifact" -Dmaven.repo.local=$out/.m2 63 76 done 64 77 '' 65 78 + lib.optionalString (!buildOffline) '' 66 - mvn package -Dmaven.repo.local=$out/.m2 ${mvnSkipTests} ${mvnParameters} 79 + mvn $MAVEN_EXTRA_ARGS package -Dmaven.repo.local=$out/.m2 ${mvnSkipTests} ${mvnParameters} 67 80 '' 68 81 + '' 69 82 runHook postBuild
+86
pkgs/by-name/ma/maven/maven-proxy.py
··· 1 + """ 2 + Maven doesn't honor HTTP[S]_PROXY and NO_PROXY env vars out of the box. 3 + Instead, it expects the user to configure a settings.xml file. 4 + We however impurely pass only these env vars in FODs. 5 + This creates the XML file on demand, if one or more env vars is set. 6 + """ 7 + 8 + import os 9 + import sys 10 + from urllib.parse import urlparse 11 + 12 + 13 + def parse_proxy_url(url): 14 + if url is None: 15 + return None 16 + parsed = urlparse(url) 17 + 18 + if parsed.hostname is None: 19 + print(f"Failed to parse proxy URL {url}, ignoring", file=sys.stderr) 20 + return None 21 + 22 + return { 23 + 'protocol': parsed.scheme or 'http', 24 + 'host': parsed.hostname, 25 + 'port': parsed.port or (443 if parsed.scheme == 'https' else 80), 26 + 'username': parsed.username, 27 + 'password': parsed.password 28 + } 29 + 30 + 31 + def format_proxy_block(proxy, id_suffix, non_proxy_hosts): 32 + auth = "" 33 + if proxy.get("username"): 34 + auth += f" <username>{proxy['username']}</username>\n" 35 + if proxy.get("password"): 36 + auth += f" <password>{proxy['password']}</password>\n" 37 + 38 + np_hosts = "" 39 + if non_proxy_hosts: 40 + np_hosts = f" <nonProxyHosts>{non_proxy_hosts}</nonProxyHosts>\n" 41 + 42 + return f""" <proxy> 43 + <id>{id_suffix}-proxy</id> 44 + <active>true</active> 45 + <protocol>{proxy['protocol']}</protocol> 46 + <host>{proxy['host']}</host> 47 + <port>{proxy['port']}</port> 48 + {auth}{np_hosts} </proxy>""" 49 + 50 + 51 + def main(output_path): 52 + http_proxy = parse_proxy_url(os.environ.get("HTTP_PROXY")) 53 + https_proxy = parse_proxy_url(os.environ.get("HTTPS_PROXY")) 54 + non_proxy_hosts = os.environ.get("NO_PROXY", "").replace(",", "|") 55 + 56 + proxy_blocks = [] 57 + 58 + if http_proxy: 59 + proxy_blocks.append( 60 + format_proxy_block(http_proxy, "http", non_proxy_hosts) 61 + ) 62 + if https_proxy and https_proxy != http_proxy: 63 + proxy_blocks.append( 64 + format_proxy_block(https_proxy, "https", non_proxy_hosts) 65 + ) 66 + 67 + settings_xml = f"""<?xml version="1.0" encoding="UTF-8"?> 68 + <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 69 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 70 + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 71 + http://maven.apache.org/xsd/settings-1.0.0.xsd"> 72 + <proxies> 73 + {'\n'.join(proxy_blocks)} 74 + </proxies> 75 + </settings> 76 + """ 77 + 78 + with open(output_path, "w") as f: 79 + f.write(settings_xml) 80 + 81 + print(f"Generated Maven settings.xml at {output_path}") 82 + 83 + 84 + if __name__ == "__main__": 85 + output_file = sys.argv[1] if len(sys.argv) > 1 else "settings.xml" 86 + main(output_file)