about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/beam-modules/mix-release.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nixpkgs/pkgs/development/beam-modules/mix-release.nix')
-rw-r--r--nixpkgs/pkgs/development/beam-modules/mix-release.nix65
1 files changed, 38 insertions, 27 deletions
diff --git a/nixpkgs/pkgs/development/beam-modules/mix-release.nix b/nixpkgs/pkgs/development/beam-modules/mix-release.nix
index 320fcaa9c9b7..80e8721302e8 100644
--- a/nixpkgs/pkgs/development/beam-modules/mix-release.nix
+++ b/nixpkgs/pkgs/development/beam-modules/mix-release.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, elixir, erlang, findutils, hex, rebar, rebar3, fetchMixDeps, makeWrapper, git }:
+{ stdenv, lib, elixir, erlang, findutils, hex, rebar, rebar3, fetchMixDeps, makeWrapper, git, ripgrep }:
 
 { pname
 , version
@@ -8,38 +8,48 @@
 , enableDebugInfo ? false
 , mixEnv ? "prod"
 , compileFlags ? [ ]
-, mixDeps ? null
+  # mix fixed output derivation dependencies
+, mixFodDeps ? null
+  # 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 ? { }
 , ...
 }@attrs:
 let
-  overridable = builtins.removeAttrs attrs [ "compileFlags" ];
-
+  # remove non standard attributes that cannot be coerced to strings
+  overridable = builtins.removeAttrs attrs [ "compileFlags" "mixNixDeps" ];
 in
+assert mixNixDeps != { } -> mixFodDeps == null;
 stdenv.mkDerivation (overridable // {
-  nativeBuildInputs = nativeBuildInputs ++ [ erlang hex elixir makeWrapper git ];
+  # rg is used as a better grep to search for erlang references in the final release
+  nativeBuildInputs = nativeBuildInputs ++ [ erlang hex elixir makeWrapper git ripgrep ];
+  buildInputs = builtins.attrValues mixNixDeps;
 
   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
+  # some older dependencies still use rebar
   MIX_REBAR = "${rebar}/bin/rebar";
   MIX_REBAR3 = "${rebar3}/bin/rebar3";
 
   postUnpack = ''
     export HEX_HOME="$TEMPDIR/hex"
     export MIX_HOME="$TEMPDIR/mix"
-    # 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"
 
     # Rebar
     export REBAR_GLOBAL_CONFIG_DIR="$TEMPDIR/rebar3"
     export REBAR_CACHE_DIR="$TEMPDIR/rebar3.cache"
 
-    ${lib.optionalString (mixDeps != null) ''
-      cp --no-preserve=mode -R "${mixDeps}" "$MIX_DEPS_PATH"
+    ${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"
     ''
     }
 
@@ -74,8 +84,10 @@ stdenv.mkDerivation (overridable // {
     runHook postInstall
   '';
 
-  fixupPhase = ''
-    runHook preFixup
+  # Stripping of the binary is intentional
+  # even though it does not affect beam files
+  # it is necessary for NIFs binaries
+  postFixup = ''
     if [ -e "$out/bin/${pname}.bat" ]; then # absent in special cases, i.e. elixir-ls
       rm "$out/bin/${pname}.bat" # windows file
     fi
@@ -86,21 +98,20 @@ stdenv.mkDerivation (overridable // {
     if [ -e $out/releases/COOKIE ]; then # absent in special cases, i.e. elixir-ls
       rm $out/releases/COOKIE
     fi
-    # TODO remove the uneeded reference too erlang
-    # one possible way would be
-    # for f in $(${findutils}/bin/find $out -name start); do
-    #   substituteInPlace $f \
-    #     --replace 'ROOTDIR=${erlang}/lib/erlang' 'ROOTDIR=""'
-    # done
-    # What is left to do is to check that erlang is not required on
-    # the host
-
-    patchShebangs $out
-    runHook postFixup
+    # removing unused erlang reference from resulting derivation to reduce
+    # closure size
+    if [ -e $out/erts-* ]; then
+      echo "ERTS found in $out - removing references to erlang to reduce closure size"
+      # there is a link in $out/erts-*/bin/start always
+      # sometimes there are links in dependencies like bcrypt compiled binaries
+      for file in $(rg "${erlang}/lib/erlang" "$out" --text --files-with-matches); do
+        substituteInPlace "$file" --replace "${erlang}/lib/erlang" "$out"
+      done
+    fi
   '';
-  # TODO figure out how to do a Fixed Output Derivation and add the output hash
-  # This doesn't play well at the moment with Phoenix projects
-  # for example that have frontend dependencies
 
+  # TODO investigate why the resulting closure still has
+  # a reference to erlang.
+  # uncommenting the following will fail the build
   # disallowedReferences = [ erlang ];
 })