Unverified Commit d1972278 authored by Adam C. Stephens's avatar Adam C. Stephens
Browse files

beamPackages.buildMix: move phases to hooks

parent 8bdef3ae
Loading
Loading
Loading
Loading
+33 −73
Original line number Diff line number Diff line
@@ -2,8 +2,13 @@
  elixir,
  erlang,
  hex,
  beamCopySourceHook,
  beamModuleInstallHook,
  mixBuildDirHook,
  mixCompileHook,
  mixAppConfigPatchHook,

  lib,
  mixConfigureHook,
  stdenv,
  writeText,
}:
@@ -16,42 +21,46 @@ lib.extendMkDerivation {
  extendDrvArgs =
    finalAttrs:
    {
      nativeBuildInputs ? [ ],
      propagatedBuildInputs ? [ ],
      passthru ? { },
      beamDeps ? [ ],
      mixEnv ? "prod",
      mixTarget ? "host",
      enableDebugInfo ? false,
      # A handful of libraries require compile time configuration

      # Allow passing compile time config instead of an empty config
      appConfigPath ? null,

      enableDebugInfo ? false,
      erlangCompilerOptions ? [ ],
      # Deterministic Erlang builds remove full system paths from debug information
      # among other things to keep builds more reproducible. See their docs for more:
      # https://www.erlang.org/doc/man/compile
      erlangDeterministicBuilds ? true,
      beamDeps ? [ ],
      ...
    }@args:
    {
      name = "erlang-${erlang.version}-${args.name}-${finalAttrs.version}";
      name = "erlang${erlang.version}-${args.name}-${finalAttrs.version}";

      env = {
        MIX_ENV = mixEnv;
        MIX_TARGET = mixTarget;
        MIX_BUILD_PREFIX = (if mixTarget == "host" then "" else "${mixTarget}_") + "${mixEnv}";
        MIX_DEBUG = if enableDebugInfo then 1 else 0;
        HEX_OFFLINE = 1;
        ERL_COMPILER_OPTIONS =
          let
            options = erlangCompilerOptions ++ lib.optionals erlangDeterministicBuilds [ "deterministic" ];
          in
          "[${lib.concatStringsSep "," options}]";

        MIX_ENV = mixEnv;
        MIX_TARGET = mixTarget;
        MIX_BUILD_PREFIX = (if mixTarget == "host" then "" else "${mixTarget}_") + "${mixEnv}";
        MIX_DEBUG = if enableDebugInfo then 1 else 0;
        HEX_OFFLINE = 1;

        LANG = if stdenv.hostPlatform.isLinux then "C.UTF-8" else "C";
        LC_CTYPE = if stdenv.hostPlatform.isLinux then "C.UTF-8" else "UTF-8";
        __darwinAllowLocalNetworking = true;

        # some hooks need name-version, but we've overridden name above for the nix package
        beamModuleName = args.name;
      };

      __darwinAllowLocalNetworking = true;

      # add to ERL_LIBS so other modules can find at runtime.
      # http://erlang.org/doc/man/code.html#code-path
      # Mix also searches the code path when compiling with the --no-deps-check flag
@@ -60,70 +69,21 @@ lib.extendMkDerivation {
        addToSearchPath ERL_LIBS "$1/lib/erlang/lib"
      '';

      nativeBuildInputs = nativeBuildInputs ++ [
      nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
        elixir
        hex
        mixConfigureHook
      ];
      propagatedBuildInputs = propagatedBuildInputs ++ beamDeps;

      # We don't want to include whatever config a dependency brings; per
      # https://hexdocs.pm/elixir/main/Config.html, config is application specific.
      patchPhase =
        args.patchPhase or (
          ''
            runHook prePatch

            rm -rf config
          ''
          + lib.optionalString (!isNull appConfigPath) ''
            cp -r ${appConfigPath} config
          ''
          + ''
            runHook postPatch
          ''
        );

      buildPhase =
        args.buildPhase or ''
          runHook preBuild

          export HEX_HOME="$TEMPDIR/hex"
          export MIX_HOME="$TEMPDIR/mix"
          mix compile --no-deps-check

          runHook postBuild
        '';

      installPhase =
        args.installPhase or ''
          runHook preInstall

          # This uses the install path convention established by nixpkgs maintainers
          # for all beam packages. Changing this will break compatibility with other
          # builder functions like buildRebar3 and buildErlangMk.
          mkdir -p "$out/lib/erlang/lib/${finalAttrs.name}-${finalAttrs.version}-${args.version}"

          # Some packages like db_connection will use _build/shared instead of
          # honoring the $MIX_ENV variable.
          for reldir in _build/{$MIX_BUILD_PREFIX,shared}/lib/${finalAttrs.name}-${finalAttrs.version}/{src,ebin,priv,include} ; do
            if test -d $reldir ; then
              # Some builds produce symlinks (eg: phoenix priv dircetory). They must
              # be followed with -H flag.
              cp  -Hrt "$out/lib/erlang/lib/${finalAttrs.name}-${finalAttrs.version}" "$reldir"
            fi
          done

          runHook postInstall
        '';
        beamCopySourceHook
        beamModuleInstallHook
        mixBuildDirHook
        mixCompileHook
        mixAppConfigPatchHook
      ];

      # stripping does not have any effect on beam files
      # it is however needed for dependencies with NIFs like bcrypt for example
      dontStrip = args.dontStrip or false;
      propagatedBuildInputs = (args.propagatedBuildInputs or [ ]) ++ beamDeps;

      passthru = {
      passthru = (args.passthru or { }) // {
        inherit beamDeps;
      }
      // passthru;
      };
    };
}
+5 −1
Original line number Diff line number Diff line
@@ -101,7 +101,11 @@ let
      webdriver = callPackage ./webdriver { };

      inherit (callPackages ./hooks { })
        mixConfigureHook
        beamCopySourceHook
        beamModuleInstallHook
        mixBuildDirHook
        mixCompileHook
        mixAppConfigPatchHook
        ;
    };
in
+16 −0
Original line number Diff line number Diff line
# shellcheck shell=bash
#
# Copy the source so it can be used by mix projects to assemble `deps`
# do this before building to avoid build artifacts but after patching
# to include any user modifications to the source

beamCopySourceHook() {
  echo "Executing beamCopySourceHook"

  mkdir -p "$out/src"
  cp -r "." "$out/src"

  echo "Finished beamCopySourceHook"
}

postPatchHooks+=(beamCopySourceHook)
+25 −0
Original line number Diff line number Diff line
# shellcheck shell=bash
#
# This uses the install path convention established by nixpkgs maintainers
# for all beam packages. Changing this will break compatibility with other
# builder functions like buildRebar3 and buildErlangMk.

beamModuleInstallHook() {
  echo "Executing beamModuleInstallHook"

  mkdir -p "$out/lib/erlang/lib/${beamModuleName}-${version}"

  for reldir in _build/{$MIX_BUILD_PREFIX,shared}/lib/${beamModuleName}/{src,ebin,priv,include}; do
    if test -d "$reldir"; then
      # Some builds produce symlinks (eg: phoenix priv directory). They must
      # be followed with -H flag.
      cp -vHrt "$out/lib/erlang/lib/${beamModuleName}-${version}" "$reldir"
    fi
  done

  echo "Finished beamModuleInstallHook"
}

if [ -z "${dontBeamModuleInstall-}" ] && [ -z "${installPhase-}" ]; then
  installPhase=beamModuleInstallHook
fi
+19 −5
Original line number Diff line number Diff line
{ makeSetupHook }:
{
  makeSetupHook,
}:
{
  mixConfigureHook = makeSetupHook {
  beamCopySourceHook = makeSetupHook {
    name = "beam-copy-source-hook.sh";
  } ./beam-copy-source-hook.sh;

  beamModuleInstallHook = makeSetupHook {
    name = "beam-module-install-hook.sh";
  } ./beam-module-install-hook.sh;

  mixBuildDirHook = makeSetupHook {
    name = "mix-configure-hook.sh";
  } ./mix-configure-hook.sh;
  } ./mix-build-dir-hook.sh;

  mixCompileHook = makeSetupHook {
    name = "mix-compile-hook.sh";
  } ./mix-compile-hook.sh;

  mixAppConfigPatchHook = makeSetupHook {
    name = "mix-config-patch-hook.sh";
  } ./mix-app-config-patch-hook.sh;
}
Loading