···11+---
22+title: buildGleamApplication
33+description: gleam2nix command line arguments and usage
44+---
55+66+<!--
77+SPDX-FileCopyrightText: 2025 Ruby Iris Juric <ruby@srxl.me>
88+99+SPDX-License-Identifier: 0BSD
1010+-->
1111+1212+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.
1313+1414+## output
1515+1616+the built derivation wil include two folders:
1717+1818+- `bin/`: contains a script to run the application, named the same as the `pname` argument
1919+- `lib/`: contains the compiled application and all of it's dependencies, each in their own folder
2020+2121+the `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:
2222+2323+1. ensures all OTP applications required to run the program are started with [`application:ensure_all_started/1`](https://www.erlang.org/doc/apps/kernel/application#ensure_all_started/1)
2424+2. runs the `main` method defined by the gleam project
2525+3. shuts down the erlang vm when `main` returns
2626+2727+by 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/).
2828+2929+## overriding dependencies
3030+3131+it'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.
3232+3333+for example, if you have a `path` dependency named `doohickey` in your `gleam.toml`, but you need to fetch it from a git repo in your nix builds, you can define `gleamNixOverrides` as:
3434+3535+```nix
3636+gleamNixOverrides = final: prev: {
3737+ doohickey = prev.doohickey.override {
3838+ src = pkgs.fetchgit {
3939+ ...
4040+ }
4141+ }
4242+}
4343+```
4444+4545+## arguments
4646+4747+required arguments:
4848+4949+- `pname`: the derivation's name
5050+- `version`: the derivation's version
5151+- `src`: the location of the project source code. must be the root directory of a gleam project (ie. contains a `gleam.toml` and `manifest.toml`)
5252+- `gleamNix`: a function returning an attrset with all of the project's dependencies. note that this _must_ be the function itself, and not just a path to the `gleam.nix` - make sure you `import` the file.
5353+5454+optional arguments:
5555+5656+- `erlang`: the erlang derivation to be used as the application runtime. defaults to `erlang` in nixpkgs
5757+- `gleam`: the gleam derivation used to compile your project. defaults to `gleam` in nixpkgs
5858+- `buildGleamArgs`: extra arguments to be passed to the `buildGleam` call for your project (not dependencies).
5959+- `gleamNixOverrides`: an overlay-style function used to modify the dependencies specified in `gleamNix`. see the section on [overriding dependencies](#overriding-dependencies) for details.
6060+6161+any extra arguments not listed here will be passed to the underlying `stdenv.mkDerivation` call.
6262+6363+## examples
6464+6565+a basic application build:
6666+6767+```nix
6868+buildGleamApplication {
6969+ pname = "basic";
7070+ version = "1.0.0";
7171+ src = ./.;
7272+ gleamNix = import ./gleam.nix;
7373+}
7474+```
7575+7676+build an application that runs under a specific version of erlang:
7777+7878+```nix
7979+buildGleamApplication {
8080+ pname = "twenty-eight";
8181+ version = "1.0.0";
8282+ src = ./.;
8383+ gleamNix = import ./gleam.nix;
8484+8585+ erlang = pkgs.erlang_28;
8686+}
8787+```
8888+8989+add an extra script to the output:
9090+9191+```nix
9292+buildGleamApplication {
9393+ pname = "extra-bin-script";
9494+ version = "1.0.0";
9595+ src = ./.;
9696+ gleamNix = import ./gleam.nix;
9797+9898+ postInstall = ''
9999+ cat > $out/bin/colon-three <<EOF
100100+ #!/usr/bin/env bash
101101+ echo ":3"
102102+ EOF
103103+ chmod +x $out/bin/colon-three
104104+ '';
105105+}
106106+```