nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, fetchFromGitHub, fetchgit,
2 fetchHex, erlang, makeWrapper,
3 writeScript, common-updater-scripts, coreutils, git, gnused, nix, rebar3-nix }:
4
5let
6 version = "3.22.0";
7 owner = "erlang";
8 deps = import ./rebar-deps.nix { inherit fetchFromGitHub fetchgit fetchHex; };
9 rebar3 = stdenv.mkDerivation rec {
10 pname = "rebar3";
11 inherit version erlang;
12
13 # How to obtain `sha256`:
14 # nix-prefetch-url --unpack https://github.com/erlang/rebar3/archive/${version}.tar.gz
15 src = fetchFromGitHub {
16 inherit owner;
17 repo = pname;
18 rev = version;
19 sha256 = "OCd9wGwnwOuv/Ojf1S4ALLn73AGKuXlRtukIiTSE2rs=";
20 };
21
22 buildInputs = [ erlang ];
23
24 postPatch = ''
25 mkdir -p _checkouts _build/default/lib/
26
27 ${toString (lib.mapAttrsToList (k: v: ''
28 cp -R --no-preserve=mode ${v} _checkouts/${k}
29 '') deps)}
30
31 # Bootstrap script expects the dependencies in _build/default/lib
32 # TODO: Make it accept checkouts?
33 for i in _checkouts/* ; do
34 ln -s $(pwd)/$i $(pwd)/_build/default/lib/
35 done
36 '';
37
38 buildPhase = ''
39 HOME=. escript bootstrap
40 '';
41
42 checkPhase = ''
43 HOME=. escript ./rebar3 ct
44 '';
45
46 doCheck = true;
47
48 installPhase = ''
49 mkdir -p $out/bin
50 cp rebar3 $out/bin/rebar3
51 '';
52
53 meta = {
54 homepage = "https://github.com/rebar/rebar3";
55 description = "Erlang build tool that makes it easy to compile and test Erlang applications, port drivers and releases";
56
57 longDescription = ''
58 rebar is a self-contained Erlang script, so it's easy to distribute or
59 even embed directly in a project. Where possible, rebar uses standard
60 Erlang/OTP conventions for project structures, thus minimizing the amount
61 of build configuration work. rebar also provides dependency management,
62 enabling application writers to easily re-use common libraries from a
63 variety of locations (hex.pm, git, hg, and so on).
64 '';
65
66 platforms = lib.platforms.unix;
67 maintainers = lib.teams.beam.members;
68 license = lib.licenses.asl20;
69 };
70
71 passthru.updateScript = writeScript "update.sh" ''
72 #!${stdenv.shell}
73 set -ox errexit
74 PATH=${
75 lib.makeBinPath [
76 common-updater-scripts
77 coreutils
78 git
79 gnused
80 nix
81 (rebar3WithPlugins { globalPlugins = [rebar3-nix]; })
82 ]
83 }
84 latest=$(list-git-tags | sed -n '/[\d\.]\+/p' | sort -V | tail -1)
85 if [ "$latest" != "${version}" ]; then
86 nixpkgs="$(git rev-parse --show-toplevel)"
87 nix_path="$nixpkgs/pkgs/development/tools/build-managers/rebar3"
88 update-source-version rebar3 "$latest" --version-key=version --print-changes --file="$nix_path/default.nix"
89 tmpdir=$(mktemp -d)
90 cp -R $(nix-build $nixpkgs --no-out-link -A rebar3.src)/* "$tmpdir"
91 (cd "$tmpdir" && rebar3 as test nix lock -o "$nix_path/rebar-deps.nix")
92 else
93 echo "rebar3 is already up-to-date"
94 fi
95 '';
96 };
97
98 # Alias rebar3 so we can use it as default parameter below
99 _rebar3 = rebar3;
100
101 rebar3WithPlugins = { plugins ? [ ], globalPlugins ? [ ], rebar3 ? _rebar3 }:
102 let
103 pluginLibDirs = map (p: "${p}/lib/erlang/lib") (lib.unique (plugins ++ globalPlugins));
104 globalPluginNames = lib.unique (map (p: p.packageName) globalPlugins);
105 rebar3Patched = (rebar3.overrideAttrs (old: {
106
107 # skip-plugins.patch is necessary because otherwise rebar3 will always
108 # try to fetch plugins if they are not already present in _build.
109 #
110 # global-deps.patch makes it possible to use REBAR_GLOBAL_PLUGINS to
111 # instruct rebar3 to always load a certain plugin. It is necessary since
112 # REBAR_GLOBAL_CONFIG_DIR doesn't seem to work for this.
113 patches = [ ./skip-plugins.patch ./global-plugins.patch ];
114
115 # our patches cause the tests to fail
116 doCheck = false;
117 }));
118 in stdenv.mkDerivation {
119 pname = "rebar3-with-plugins";
120 inherit (rebar3) version;
121 nativeBuildInputs = [ erlang makeWrapper ];
122 unpackPhase = "true";
123
124 # Here we extract the rebar3 escript (like `rebar3_prv_local_install.erl`) and
125 # add plugins to the code path.
126
127 installPhase = ''
128 erl -noshell -eval '
129 {ok, Escript} = escript:extract("${rebar3Patched}/bin/rebar3", []),
130 {archive, Archive} = lists:keyfind(archive, 1, Escript),
131 {ok, _} = zip:extract(Archive, [{cwd, "'$out/lib'"}]),
132 init:stop(0)
133 '
134 cp ${./rebar_ignore_deps.erl} rebar_ignore_deps.erl
135 erlc -o $out/lib/rebar/ebin rebar_ignore_deps.erl
136 mkdir -p $out/bin
137 makeWrapper ${erlang}/bin/erl $out/bin/rebar3 \
138 --set REBAR_GLOBAL_PLUGINS "${toString globalPluginNames} rebar_ignore_deps" \
139 --suffix-each ERL_LIBS ":" "$out/lib ${toString pluginLibDirs}" \
140 --add-flags "+sbtu +A1 -noshell -boot start_clean -s rebar3 main -extra"
141 '';
142 };
143in { inherit rebar3 rebar3WithPlugins; }