2021-11-02 12:57:13 +00:00
|
|
|
{ stdenv, lib, elixir, erlang, findutils, hex, rebar, rebar3, fetchMixDeps, makeWrapper, git, ripgrep }@inputs:
|
2021-04-08 11:54:48 +00:00
|
|
|
|
|
|
|
{ pname
|
|
|
|
, version
|
|
|
|
, src
|
|
|
|
, nativeBuildInputs ? [ ]
|
2022-01-23 14:13:53 +00:00
|
|
|
, buildInputs ? [ ]
|
2021-04-08 11:54:48 +00:00
|
|
|
, meta ? { }
|
|
|
|
, enableDebugInfo ? false
|
|
|
|
, mixEnv ? "prod"
|
|
|
|
, compileFlags ? [ ]
|
2023-05-16 08:50:34 +00:00
|
|
|
|
2021-05-19 04:59:52 +00:00
|
|
|
# mix fixed output derivation dependencies
|
|
|
|
, mixFodDeps ? null
|
2023-05-16 08:50:34 +00:00
|
|
|
|
2021-05-19 04:59:52 +00:00
|
|
|
# mix dependencies generated by mix2nix
|
|
|
|
# this assumes each dependency is built by buildMix or buildRebar3
|
|
|
|
# each dependency needs to have a setup hook to add the lib path to $ERL_LIBS
|
|
|
|
# this is how mix will find dependencies
|
|
|
|
, mixNixDeps ? { }
|
2023-05-16 08:50:34 +00:00
|
|
|
|
2021-11-02 12:57:13 +00:00
|
|
|
, elixir ? inputs.elixir
|
|
|
|
, hex ? inputs.hex.override { inherit elixir; }
|
2023-05-16 08:50:34 +00:00
|
|
|
|
|
|
|
# This reduces closure size, but can lead to some hard to understand runtime
|
|
|
|
# errors, so use with caution. See e.g.
|
|
|
|
# https://github.com/whitfin/cachex/issues/205
|
|
|
|
# https://framagit.org/framasoft/mobilizon/-/issues/1169
|
|
|
|
, stripDebug ? false
|
|
|
|
|
2021-04-08 11:54:48 +00:00
|
|
|
, ...
|
|
|
|
}@attrs:
|
|
|
|
let
|
2021-05-19 04:59:52 +00:00
|
|
|
# remove non standard attributes that cannot be coerced to strings
|
|
|
|
overridable = builtins.removeAttrs attrs [ "compileFlags" "mixNixDeps" ];
|
2021-04-08 11:54:48 +00:00
|
|
|
in
|
2021-05-19 04:59:52 +00:00
|
|
|
assert mixNixDeps != { } -> mixFodDeps == null;
|
2022-09-14 21:07:48 +00:00
|
|
|
assert stripDebug -> !enableDebugInfo;
|
|
|
|
|
2021-04-08 11:54:48 +00:00
|
|
|
stdenv.mkDerivation (overridable // {
|
2021-06-06 10:58:21 +00:00
|
|
|
# rg is used as a better grep to search for erlang references in the final release
|
|
|
|
nativeBuildInputs = nativeBuildInputs ++ [ erlang hex elixir makeWrapper git ripgrep ];
|
2022-01-23 14:13:53 +00:00
|
|
|
buildInputs = buildInputs ++ builtins.attrValues mixNixDeps;
|
2021-04-08 11:54:48 +00:00
|
|
|
|
|
|
|
MIX_ENV = mixEnv;
|
|
|
|
MIX_DEBUG = if enableDebugInfo then 1 else 0;
|
|
|
|
HEX_OFFLINE = 1;
|
|
|
|
DEBUG = if enableDebugInfo then 1 else 0; # for Rebar3 compilation
|
|
|
|
# the api with `mix local.rebar rebar path` makes a copy of the binary
|
2021-06-09 03:16:24 +00:00
|
|
|
# some older dependencies still use rebar
|
|
|
|
MIX_REBAR = "${rebar}/bin/rebar";
|
2021-04-08 11:54:48 +00:00
|
|
|
MIX_REBAR3 = "${rebar3}/bin/rebar3";
|
2023-09-02 11:38:29 +00:00
|
|
|
LC_ALL = "C.UTF-8";
|
2021-04-08 11:54:48 +00:00
|
|
|
|
|
|
|
postUnpack = ''
|
|
|
|
export HEX_HOME="$TEMPDIR/hex"
|
|
|
|
export MIX_HOME="$TEMPDIR/mix"
|
|
|
|
|
|
|
|
# Rebar
|
|
|
|
export REBAR_GLOBAL_CONFIG_DIR="$TEMPDIR/rebar3"
|
|
|
|
export REBAR_CACHE_DIR="$TEMPDIR/rebar3.cache"
|
|
|
|
|
2021-05-19 04:59:52 +00:00
|
|
|
${lib.optionalString (mixFodDeps != null) ''
|
|
|
|
# compilation of the dependencies will require
|
|
|
|
# that the dependency path is writable
|
|
|
|
# thus a copy to the TEMPDIR is inevitable here
|
|
|
|
export MIX_DEPS_PATH="$TEMPDIR/deps"
|
|
|
|
cp --no-preserve=mode -R "${mixFodDeps}" "$MIX_DEPS_PATH"
|
2021-04-08 11:54:48 +00:00
|
|
|
''
|
|
|
|
}
|
|
|
|
|
|
|
|
'' + (attrs.postUnpack or "");
|
|
|
|
|
|
|
|
configurePhase = attrs.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
2021-08-03 14:51:39 +00:00
|
|
|
${./mix-configure-hook.sh}
|
2021-04-08 11:54:48 +00:00
|
|
|
# this is needed for projects that have a specific compile step
|
|
|
|
# the dependency needs to be compiled in order for the task
|
|
|
|
# to be available
|
|
|
|
# Phoenix projects for example will need compile.phoenix
|
|
|
|
mix deps.compile --no-deps-check --skip-umbrella-children
|
|
|
|
|
2023-06-28 14:48:59 +00:00
|
|
|
# Symlink dependency sources. This is needed for projects that require
|
|
|
|
# access to the source of their dependencies. For example, Phoenix
|
|
|
|
# applications need javascript assets to build asset bundles.
|
|
|
|
${lib.optionalString (mixNixDeps != { }) ''
|
|
|
|
mkdir -p deps
|
|
|
|
|
|
|
|
${lib.concatMapStringsSep "\n" (dep: ''
|
|
|
|
dep_name=$(basename ${dep} | cut -d '-' -f2)
|
|
|
|
dep_path="deps/$dep_name"
|
|
|
|
if [ -d "${dep}/src" ]; then
|
|
|
|
ln -s ${dep}/src $dep_path
|
|
|
|
fi
|
|
|
|
'') (builtins.attrValues mixNixDeps)}
|
|
|
|
''}
|
|
|
|
|
|
|
|
# Symlink deps to build root. Similar to above, but allows for mixFodDeps
|
|
|
|
# Phoenix projects to find javascript assets.
|
|
|
|
${lib.optionalString (mixFodDeps != null) ''
|
|
|
|
ln -s ../deps ./
|
|
|
|
''}
|
|
|
|
|
2021-04-08 11:54:48 +00:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = attrs.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
|
|
|
mix compile --no-deps-check ${lib.concatStringsSep " " compileFlags}
|
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
|
|
installPhase = attrs.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
mix release --no-deps-check --path "$out"
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
2021-06-07 00:22:07 +00:00
|
|
|
# Stripping of the binary is intentional
|
|
|
|
# even though it does not affect beam files
|
|
|
|
# it is necessary for NIFs binaries
|
2021-06-06 12:03:44 +00:00
|
|
|
postFixup = ''
|
2021-04-08 11:54:48 +00:00
|
|
|
if [ -e "$out/bin/${pname}.bat" ]; then # absent in special cases, i.e. elixir-ls
|
|
|
|
rm "$out/bin/${pname}.bat" # windows file
|
|
|
|
fi
|
|
|
|
# contains secrets and should not be in the nix store
|
|
|
|
# TODO document how to handle RELEASE_COOKIE
|
|
|
|
# secrets should not be in the nix store.
|
|
|
|
# This is only used for connecting multiple nodes
|
|
|
|
if [ -e $out/releases/COOKIE ]; then # absent in special cases, i.e. elixir-ls
|
|
|
|
rm $out/releases/COOKIE
|
|
|
|
fi
|
2021-06-06 08:36:51 +00:00
|
|
|
# removing unused erlang reference from resulting derivation to reduce
|
|
|
|
# closure size
|
|
|
|
if [ -e $out/erts-* ]; then
|
2021-06-06 10:58:21 +00:00
|
|
|
echo "ERTS found in $out - removing references to erlang to reduce closure size"
|
2021-06-07 00:22:07 +00:00
|
|
|
# there is a link in $out/erts-*/bin/start always
|
2021-10-02 15:25:39 +00:00
|
|
|
# TODO:
|
2021-06-07 00:22:07 +00:00
|
|
|
# sometimes there are links in dependencies like bcrypt compiled binaries
|
2021-10-02 15:25:39 +00:00
|
|
|
# at the moment those are not removed since substituteInPlace will
|
|
|
|
# error on binaries
|
|
|
|
for file in $(rg "${erlang}/lib/erlang" "$out" --files-with-matches); do
|
|
|
|
echo "removing reference to erlang in $file"
|
2021-06-06 08:36:51 +00:00
|
|
|
substituteInPlace "$file" --replace "${erlang}/lib/erlang" "$out"
|
|
|
|
done
|
|
|
|
fi
|
2022-09-14 21:07:48 +00:00
|
|
|
'' + lib.optionalString stripDebug ''
|
|
|
|
# strip debug symbols to avoid hardreferences to "foreign" closures actually
|
|
|
|
# not needed at runtime, while at the same time reduce size of BEAM files.
|
|
|
|
erl -noinput -eval 'lists:foreach(fun(F) -> io:format("Stripping ~p.~n", [F]), beam_lib:strip(F) end, filelib:wildcard("'"$out"'/**/*.beam"))' -s init stop
|
2021-04-08 11:54:48 +00:00
|
|
|
'';
|
|
|
|
|
2021-06-06 08:36:51 +00:00
|
|
|
# TODO investigate why the resulting closure still has
|
|
|
|
# a reference to erlang.
|
|
|
|
# uncommenting the following will fail the build
|
2021-04-08 11:54:48 +00:00
|
|
|
# disallowedReferences = [ erlang ];
|
|
|
|
})
|