Unverified Commit d4b80dcd authored by Yt's avatar Yt Committed by GitHub
Browse files

beamPackages.buildRebar3: update to use extendMkDerivation and hooks (#493147)

parents 92cb785f 405d11fc
Loading
Loading
Loading
Loading
+69 −116
Original line number Diff line number Diff line
{
  stdenv,
  writeText,
  erlang,
  beamCopySourceHook,
  beamModuleInstallHook,
  rebar3CompileHook,
  rebar3WithPlugins,
  openssl,
  rebarDevendorPatchHook,

  libyaml,
  openssl,

  lib,
  stdenv,
  writeText,
}:

lib.extendMkDerivation {
  constructDrv = stdenv.mkDerivation;
  excludeDrvArgNames = [
    "beamDeps"
    "buildPlugins"
  ];
  extendDrvArgs =
    finalAttrs:
    {
  name,
  version,
  src,
  setupHook ? null,
  buildInputs ? [ ],
      beamDeps ? [ ],
      buildPlugins ? [ ],
  postPatch ? "",
  installPhase ? null,
  buildPhase ? null,
  configurePhase ? null,
  meta ? { },

      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,
      ...
}@attrs:

    }@args:
    let
  rebar3 = rebar3WithPlugins {
      rebar3Custom = rebar3WithPlugins {
        plugins = buildPlugins;
      };
    in
    {
      pname = args.name;
      name = "erlang${erlang.version}-${args.name}-${finalAttrs.version}";

      nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
        rebarDevendorPatchHook
        beamCopySourceHook
        beamModuleInstallHook
        rebar3CompileHook
      ];

  shell =
    drv:
    stdenv.mkDerivation {
      name = "interactive-shell-${drv.name}";
      buildInputs = [ drv ];
    };

  customPhases = lib.filterAttrs (_: v: v != null) {
    inherit
      setupHook
      configurePhase
      buildPhase
      installPhase
      ;
  };

  pkg =
    self:
    stdenv.mkDerivation (
      attrs
      // {

        pname = name;
        name = "${name}-${version}";
        inherit version;

        buildInputs = buildInputs ++ [
      buildInputs = (args.buildInputs or [ ]) ++ [
        erlang
          rebar3
        rebar3Custom
        openssl
        libyaml
      ];
        propagatedBuildInputs = lib.unique beamDeps;

        inherit src;
      propagatedBuildInputs = lib.unique beamDeps;

      env = {
        ERL_COMPILER_OPTIONS =
          let
            options = erlangCompilerOptions ++ lib.optionals erlangDeterministicBuilds [ "deterministic" ];
          in
          "[${lib.concatStringsSep "," options}]";

        # stripping does not have any effect on beam files
        # it is however needed for dependencies with NIFs
        # false is the default but we keep this for readability
        dontStrip = false;
        beamModuleName = args.name;
      };

      setupHook = writeText "setupHook.sh" ''
        addToSearchPath ERL_LIBS "$1/lib/erlang/lib/"
      '';

        postPatch = ''
          rm -f rebar rebar3
        ''
        + postPatch;

        preConfigure = ''
          # Copy the source so it can be used by mix projects
          # do this before building to avoid build artifacts but after patching
          # to include any modifications
          mkdir -p $out/src
          cp -r "." "$out/src"
        '';

        buildPhase = ''
          runHook preBuild
          HOME=. rebar3 bare compile --paths "."
          runHook postBuild
        '';

        installPhase = ''
          runHook preInstall
          mkdir -p "$out/lib/erlang/lib/${name}-${version}"
          for reldir in src ebin priv include; do
            [ -d "$reldir" ] || continue
            # $out/lib/erlang/lib is a convention used in nixpkgs for compiled BEAM packages
            cp -Hrt "$out/lib/erlang/lib/${name}-${version}" "$reldir"
          done
          runHook postInstall
        '';

      meta = {
        inherit (erlang.meta) platforms;
      }
        // meta;
      // (args.meta or { });

      passthru = {
          packageName = name;
          env = shell self;
        inherit beamDeps;
      };
    };
}
      // customPhases
    );
in
lib.fix pkg
+2 −0
Original line number Diff line number Diff line
@@ -105,6 +105,8 @@ let
        mixBuildDirHook
        mixCompileHook
        mixAppConfigPatchHook
        rebar3CompileHook
        rebarDevendorPatchHook
        ;
    };
in
+9 −1
Original line number Diff line number Diff line
@@ -9,7 +9,15 @@ beamModuleInstallHook() {

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

  for reldir in _build/{$MIX_BUILD_PREFIX,shared}/lib/${beamModuleName}/{src,ebin,priv,include}; do
  # default to rebar3
  local fromDir=(./{ebin,priv,include})

  # replace if mix project
  if [ -n "$MIX_BUILD_PREFIX" ]; then
    fromDir=(_build/{$MIX_BUILD_PREFIX,shared}/lib/"${beamModuleName}"/{src,ebin,priv,include})
  fi

  for reldir in "${fromDir[@]}"; do
    if test -d "$reldir"; then
      # Some builds produce symlinks (eg: phoenix priv directory). They must
      # be followed with -H flag.
+8 −0
Original line number Diff line number Diff line
@@ -19,4 +19,12 @@
  mixAppConfigPatchHook = makeSetupHook {
    name = "mix-config-patch-hook.sh";
  } ./mix-app-config-patch-hook.sh;

  rebar3CompileHook = makeSetupHook {
    name = "rebar3-compile-hook.sh";
  } ./rebar3-compile-hook.sh;

  rebarDevendorPatchHook = makeSetupHook {
    name = "rebar-devendor-patch-hook.sh";
  } ./rebar-devendor-patch-hook.sh;
}
+13 −0
Original line number Diff line number Diff line
# shellcheck shell=bash
#
# It's common to vendor a copy of rebar/rebar3 in repos, but we want to remove those

rebarDevendorPatchHook() {
  echo "Executing rebarDevendorPatchHook"

  rm -f rebar rebar

  echo "Finished rebarDevendorPatchHook"
}

prePatchHooks+=(rebarDevendorPatchHook)
Loading