···25252626optional arguments:
27272828+- `target`: the compilation target language, either `erlang` (default) or `javascript`
2829- `beamDeps`: a list of derivations containing compiled dependencies of this project
2930- `erlang`: the erlang derivation to be used as the application runtime. defaults to `erlang` in nixpkgs
3031- `gleam`: the gleam derivation used to compile your project. defaults to `gleam` in nixpkgs
···34353536## output
36373737-the built derivation contains the compiled erlang bytecode and support files under `/lib/erlang/lib/<name>-<version>`, substituting `name` and `version` with the values passed as arguments.
3838+the built derivation contains the compiled code and support files under the following directory, based on the provided `target`:
3939+4040+- erlang: `/lib/erlang/lib/<name>-<version>`
4141+- javascript: `/<name>`
···99SPDX-License-Identifier: 0BSD
1010-->
11111212-the `buildGleamApplication` function compiles and builds a gleam application project with all it's dependencies, and produces an application package similar to what `gleam export erlang-shipment` creates.
1212+the `buildGleamApplication` function compiles and builds a gleam application project with all it's dependencies, and produces an application package with a script that can run the application.
13131414## arguments
1515···22222323optional arguments:
24242525+- `target`: the compilation target language, either `erlang` (default) or `javascript`
2526- `erlang`: the erlang derivation to be used as the application runtime. defaults to `erlang` in nixpkgs
2627- `gleam`: the gleam derivation used to compile your project. defaults to `gleam` in nixpkgs
2828+- `jsRuntime`: a derivation containing the javascript runtime used to run the application (one of nodejs, deno or bun). defaults to `null` (no runtime)
2729- `buildGleamArgs`: extra arguments to be passed to the `buildGleam` call for your project (not dependencies).
2830- `gleamNixOverrides`: an overlay-style function used to modify the dependencies specified in `gleamNix`. see the section on [overriding dependencies](#overriding-dependencies) for details.
2931···35373638- `bin/`: contains a script to run the application, named the same as the `pname` argument
3739- `lib/`: contains the compiled application and all of it's dependencies, each in their own folder
4040+4141+the specific contents of these folders depend on the `target` being compiled for.
4242+4343+### erlang
38443945the `lib` directory also holds an `entrypoint` folder, which contains a very simple erlang module with a function used to start the app. the script in the `bin` directory will start the erlang vm with all compiled erlang files in `lib` loaded, and call the entrypoint function to do the following:
4046···44504551by default, every dependency of your gleam project will be started in step 1. if you need to load in additional applications, such as ones included in OTP, specify them in the `[erlang]` section of your [`gleam.toml` file](https://gleam.run/writing-gleam/gleam-toml/).
46525353+### javascript
5454+5555+the `lib` directory contains the compiled application and each dependency in it's own folder, as well as a `gleam_entrypoint.mjs` file that imports and calls the `main` function in the application.
5656+5757+by default, the `bin` directory is not created, and the built application is comprised solely of compiled javascript code. this is useful if your application is not meant to be run directly, and instead is meant to be loaded into a web application or imported as a library.
5858+5959+if your project is intended to be run as an application, you can pass a javascript runtime package to `jsRuntime` to generate a script for running the app. the script will execute the `gleam_entrypoint.mjs` file (ie. the application's `main` function) using the provided runtime.
6060+4761## overriding dependencies
48624963it's not recommended to modify the `gleam.nix` file directly, as it's automatically generated and any changes will be lost when it needs to be generated. instead, you can pass a function to `gleamNixOverrides` to make modifications to project dependencies. the function works similarly to a [nixpkgs overlay](https://nixos.org/manual/nixpkgs/unstable/#sec-overlays-definition) - it accepts two arguments, `final` and `prev`, and returns an attribute set containing any changed/additional dependencies.
···8397 gleamNix = import ./gleam.nix;
84988599 erlang = pkgs.erlang_28;
100100+}
101101+```
102102+103103+build an application that targets javascript, and runs on deno:
104104+105105+```nix
106106+buildGleamApplication {
107107+ pname = "twenty-eight";
108108+ version = "1.0.0";
109109+ src = ./.;
110110+ gleamNix = import ./gleam.nix;
111111+112112+ target = "javascript";
113113+ jsRuntime = pkgs.deno;
86114}
87115```
88116