nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1# Adaptation of the MIT-licensed work on `sbt2nix` done by Charles O'Farrell
2
3{
4 lib,
5 fetchurl,
6 stdenv,
7}:
8let
9 defaultRepos = [
10 "https://repo1.maven.org/maven2"
11 "https://oss.sonatype.org/content/repositories/releases"
12 "https://oss.sonatype.org/content/repositories/public"
13 "https://repo.typesafe.com/typesafe/releases"
14 ];
15in
16
17args@{
18 # Example: "org.apache.httpcomponents"
19 groupId,
20 # Example: "httpclient"
21 artifactId,
22 # Example: "4.3.6"
23 version,
24 # Example: "jdk11"
25 classifier ? null,
26 # List of maven repositories from where to fetch the artifact.
27 # Example: [ http://oss.sonatype.org/content/repositories/public ].
28 repos ? defaultRepos,
29 # The `url` and `urls` parameters, if specified should point to the JAR
30 # file and will take precedence over the `repos` parameter. Only one of `url`
31 # and `urls` can be specified, not both.
32 url ? "",
33 urls ? [ ],
34 # The rest of the arguments are just forwarded to `fetchurl`.
35 ...
36}:
37
38# only one of url and urls can be specified at a time.
39assert (url == "") || (urls == [ ]);
40# if repos is empty, then url or urls must be specified.
41assert (repos != [ ]) || (url != "") || (urls != [ ]);
42
43let
44 pname =
45 (lib.replaceStrings [ "." ] [ "_" ] groupId)
46 + "_"
47 + (lib.replaceStrings [ "." ] [ "_" ] artifactId);
48 suffix = lib.optionalString (classifier != null) "-${classifier}";
49 filename = "${artifactId}-${version}${suffix}.jar";
50 mkJarUrl =
51 repoUrl:
52 lib.concatStringsSep "/" [
53 (lib.removeSuffix "/" repoUrl)
54 (lib.replaceStrings [ "." ] [ "/" ] groupId)
55 artifactId
56 version
57 filename
58 ];
59 urls_ =
60 if url != "" then
61 [ url ]
62 else if urls != [ ] then
63 urls
64 else
65 map mkJarUrl repos;
66 jar = fetchurl (
67 builtins.removeAttrs args [
68 "groupId"
69 "artifactId"
70 "version"
71 "classifier"
72 "repos"
73 "url"
74 ]
75 // {
76 urls = urls_;
77 name = "${pname}-${version}.jar";
78 }
79 );
80in
81stdenv.mkDerivation {
82 inherit pname version;
83 dontUnpack = true;
84 # By moving the jar to $out/share/java we make it discoverable by java
85 # packages packages that mention this derivation in their buildInputs.
86 installPhase = ''
87 mkdir -p $out/share/java
88 ln -s ${jar} $out/share/java/${filename}
89 '';
90 # We also add a `jar` attribute that can be used to easily obtain the path
91 # to the downloaded jar file.
92 passthru.jar = jar;
93}