Bundle a Gleam project into an AppImage with Nix
Source: Dev.to
nix bundle
Nix has a convenient, albeit unstable, command called nix bundle.
Documentation: nix3‑bundle manual
This command packages Nix dependencies into a single binary, allowing you to generate a standalone executable.
Example
nix bundle nixpkgs#python314
bundler
By specifying the --bundler option, you can change the bundling algorithm.
For instance, using github:ralismark/nix-appimage generates a single binary as an AppImage.
Repository: nix‑appimage
To bundle the aforementioned Python 3.14 as an AppImage
nix bundle --bundler github:ralismark/nix-appimage nixpkgs#python314
AppImage works by self‑extracting only the necessary files on the fly, offering faster startup speeds compared to traditional bundling methods. Therefore, using AppImage for bundling is generally recommended.
Bundling Gleam
Previously, generating a single binary with Gleam required tools like garnet, which relied on Deno and Bun and were limited to the JavaScript target.
The method introduced here does not depend on the runtime features Gleam uses, making it possible to turn Erlang targets into single binaries as well.
A trial version is available here; running nix build should generate a single binary:
The project uses gleam2nix to build the Gleam code with Nix:
Summary
- Using
nix bundleallows you to turn any package into a single binary. - You can switch the bundling method with the
--bundleroption. - Using AppImage provides a single binary with fast startup times.
- This also enables single binaries for Gleam projects targeting Erlang.
- It’s convenient, but the binary size can become quite large (e.g., several hundred MB).