Skip to content

Instantly share code, notes, and snippets.

@NobbZ
Created November 20, 2022 16:37
Show Gist options
  • Save NobbZ/1603ba65e135bf293a50c4b98eb41f71 to your computer and use it in GitHub Desktop.
Save NobbZ/1603ba65e135bf293a50c4b98eb41f71 to your computer and use it in GitHub Desktop.
Minimal elixir dockerTools image
pkgs: let
beamPkgs = with pkgs.beam_minimal; packagesWith interpreters.erlangR25;
erlang = beamPkgs.erlang;
elixir = beamPkgs.elixir_1_14;
fetchMixDeps = beamPkgs.fetchMixDeps.override {inherit elixir;};
buildMix' = beamPkgs.buildMix'.override {inherit fetchMixDeps;};
mixRelease = beamPkgs.mixRelease.override {inherit elixir erlang fetchMixDeps;};
in {inherit erlang elixir buildMix' mixRelease fetchMixDeps;}
{
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixpkgs-unstable";
inputs.nobbz.url = "github:nobbz/nixos-config?ref=main";
outputs = {
self,
nixpkgs,
nobbz,
}: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
npkgs = nobbz.packages.x86_64-linux;
spkgs = self.packages.x86_64-linux;
bpkgs = import ./.nix/beams.nix pkgs;
callPackage = pkgs.lib.callPackageWith (pkgs
// npkgs
// spkgs
// {
inherit callPackage;
inherit (bpkgs) mixRelease fetchMixDeps;
});
in {
inherit (nobbz) formatter;
devShells.x86_64-linux.default = callPackage ./.nix/shell.nix {};
packages.x86_64-linux = {
inherit (bpkgs) erlang elixir;
yamlfmt = callPackage ./.nix/yamlfmt.nix {};
crosslink = callPackage ./.nix/cross_link.nix {};
image = callPackage ./.nix/image.nix {};
};
};
}
{
dockerTools,
crosslink,
}:
dockerTools.buildLayeredImage {
name = "${crosslink.pname}";
tag = "${crosslink.version}";
contents = crosslink;
}
@NobbZ
Copy link
Author

NobbZ commented Aug 2, 2023

Oh, wait!

This is as expected!

The "minimal" Erlang is indeed ~300 to 350 MiB in its closure size ("full" is about twice), only the final applications closure will actually benefit from the partially copied OTP. As that then will only copy relevant OTP-apps.

In hindsight, using beam_minimal and beam_nox is not to remove wx, as the application will be built anyway, but no hard linking to the wx libraries happens.

The actual reason for using beam_minimal is to exclude systemd from the closure, which would be kept referenced in the final closure otherwise due to it being "wrapped" into the EPMD for which I have so far not found a way to exclude.

So for checking the size of your deployment, please build the final closure and check its size, not the intermediate erlang artifact, for which we take steps to get rid of it anyway.

If the final closure though still exceeds your expectations, further debugging has to be done on that.

@NobbZ
Copy link
Author

NobbZ commented Aug 2, 2023

And another one: I briefly checked your mix.exs, which loads the :runtime_tools, which again require the :wx application.

Remove that and your final app will not include wx anymore.

@camelpunch
Copy link

Thanks for the advice! Makes sense about the elimination of erlang. I see that the elimination does happen in the closure, but when dockerising I see the erlang stuff return with a vengeance. I'll dig a bit deeper and report!

@NobbZ
Copy link
Author

NobbZ commented Aug 3, 2023

As I have trouble following discussions on a gist, I'd suggest to move any further questions to the official discourse, which I can more actively watch, and where your problem is also more visible to other users.

https://discourse.nixos.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment