2021-05-28 09:39:13 +00:00
|
|
|
{ stdenv, erlang, rebar3WithPlugins, openssl,
|
2020-04-24 23:36:52 +00:00
|
|
|
lib }:
|
|
|
|
|
|
|
|
{ name, version
|
|
|
|
, src
|
2021-05-28 09:39:13 +00:00
|
|
|
, beamDeps ? []
|
|
|
|
, buildPlugins ? []
|
2020-04-24 23:36:52 +00:00
|
|
|
, checkouts ? null
|
|
|
|
, releaseType
|
|
|
|
, buildInputs ? []
|
|
|
|
, setupHook ? null
|
|
|
|
, profile ? "default"
|
|
|
|
, installPhase ? null
|
|
|
|
, buildPhase ? null
|
|
|
|
, configurePhase ? null
|
|
|
|
, meta ? {}
|
|
|
|
, ... }@attrs:
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
with lib;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
shell = drv: stdenv.mkDerivation {
|
|
|
|
name = "interactive-shell-${drv.name}";
|
|
|
|
buildInputs = [ drv ];
|
|
|
|
};
|
|
|
|
|
|
|
|
customPhases = filterAttrs
|
|
|
|
(_: v: v != null)
|
|
|
|
{ inherit setupHook configurePhase buildPhase installPhase; };
|
|
|
|
|
2021-05-28 09:39:13 +00:00
|
|
|
# When using the `beamDeps` argument, it is important that we use
|
|
|
|
# `rebar3WithPlugins` here even when there are no plugins. The vanilla
|
|
|
|
# `rebar3` package is an escript archive with bundled dependencies which can
|
|
|
|
# interfere with those in the app we are trying to build. `rebar3WithPlugins`
|
|
|
|
# doesn't have this issue since it puts its own deps last on the code path.
|
|
|
|
rebar3 = rebar3WithPlugins {
|
|
|
|
plugins = buildPlugins;
|
|
|
|
};
|
|
|
|
|
|
|
|
pkg =
|
|
|
|
assert beamDeps != [] -> checkouts == null;
|
|
|
|
self: stdenv.mkDerivation (attrs // {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
name = "${name}-${version}";
|
|
|
|
inherit version;
|
|
|
|
|
2021-05-28 09:39:13 +00:00
|
|
|
buildInputs = buildInputs ++ [ erlang rebar3 openssl ] ++ beamDeps;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
# ensure we strip any native binaries (eg. NIFs, ports)
|
|
|
|
stripDebugList = lib.optional (releaseType == "release") "rel";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
inherit src;
|
|
|
|
|
2021-06-28 23:13:55 +00:00
|
|
|
REBAR_IGNORE_DEPS = beamDeps != [ ];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
configurePhase = ''
|
|
|
|
runHook preConfigure
|
2021-05-28 09:39:13 +00:00
|
|
|
${lib.optionalString (checkouts != null)
|
|
|
|
"cp --no-preserve=all -R ${checkouts}/_checkouts ."}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
|
|
|
HOME=. DEBUG=1 rebar3 as ${profile} ${if releaseType == "escript"
|
2021-02-05 17:12:51 +00:00
|
|
|
then "escriptize"
|
|
|
|
else "release"}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
dir=${if releaseType == "escript"
|
2021-02-05 17:12:51 +00:00
|
|
|
then "bin"
|
|
|
|
else "rel"}
|
2021-06-06 07:54:09 +00:00
|
|
|
mkdir -p "$out/$dir" "$out/bin"
|
2020-04-24 23:36:52 +00:00
|
|
|
cp -R --preserve=mode "_build/${profile}/$dir" "$out"
|
2021-06-06 07:54:09 +00:00
|
|
|
${lib.optionalString (releaseType == "release")
|
|
|
|
"find $out/rel/*/bin -type f -executable -exec ln -s -t $out/bin {} \\;"}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
2021-06-06 07:54:09 +00:00
|
|
|
postInstall = ''
|
|
|
|
for dir in $out/rel/*/erts-*; do
|
|
|
|
echo "ERTS found in $dir - removing references to erlang to reduce closure size"
|
|
|
|
for f in $dir/bin/{erl,start}; do
|
|
|
|
substituteInPlace "$f" --replace "${erlang}/lib/erlang" "''${dir/\/erts-*/}"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
meta = {
|
|
|
|
inherit (erlang.meta) platforms;
|
|
|
|
} // meta;
|
|
|
|
|
|
|
|
passthru = {
|
|
|
|
packageName = name;
|
|
|
|
env = shell self;
|
|
|
|
};
|
|
|
|
} // customPhases);
|
|
|
|
in
|
|
|
|
fix pkg
|