|
2 | 2 | title: Elixir
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -Elixir can be configured to install Hex and Rebar dependencies in a local directory. This will keep Elixir from trying to install in your immutable Nix Store: |
| 5 | +Elixir can be installed by simply running `devbox add elixir`. This will automatically include the Elixir Plugin to isolate Mix/Hex artifacts and enable shell history in `iex`. |
6 | 6 |
|
7 | 7 | [**Example Repo**](https://github.com/jetify-com/devbox/tree/main/examples/development/elixir/elixir_hello)
|
8 | 8 |
|
9 | 9 | [](https://auth.jetify.com/devspace/templates/elixir/)
|
10 | 10 |
|
11 | 11 | ## Adding Elixir to your project
|
12 | 12 |
|
13 |
| -`devbox add elixir bash`, or add the following to your `devbox.json` |
| 13 | +`devbox add elixir`, or add the following to your `devbox.json` |
14 | 14 |
|
15 | 15 | ```json
|
16 | 16 | "packages": [
|
17 |
| - "elixir@latest", |
18 |
| - "bash@latest" |
| 17 | + "elixir@latest" |
19 | 18 | ],
|
20 | 19 | ```
|
21 | 20 |
|
22 |
| -This will install the latest version of Elixir available. You can find other installable versions of Elixir by running `devbox search elixir`. You can also search for Elixir on [Nixhub](https://www.nixhub.io/packages/elixir) |
| 21 | +This will install the latest version of Elixir. You can find other installable versions of Elixir by running `devbox search elixir`. You can also search for Elixir on [Nixhub](https://www.nixhub.io/packages/elixir) |
23 | 22 |
|
24 |
| -## Installing Hex and Rebar locally |
| 23 | +## Elixir Plugin Support |
25 | 24 |
|
26 |
| -Since you are unable to install Elixir Deps directly into the Nix store, you will need to configure mix to install your dependencies globally. You can do this by adding the following lines to your `devbox.json` init_hook: |
| 25 | +Devbox will automatically use the following configuration when you install Elixir with `devbox add`. |
| 26 | + |
| 27 | +### Environment Variables |
| 28 | + |
| 29 | +`$MIX_HOME` and `$HEX_HOME` configure Mix/Hex to install artifacts locally, while `$ERL_AFLAGS` enables shell history in `iex`: |
| 30 | + |
| 31 | +```bash |
| 32 | +MIX_HOME={PROJECT_DIR}/.devbox/virtenv/elixir/mix |
| 33 | +HEX_HOME={PROJECT_DIR}/.devbox/virtenv/elixir/hex |
| 34 | +ERL_AFLAGS="-kernel shell_history enabled" |
| 35 | +``` |
| 36 | + |
| 37 | +### Disabling the Elixir Plugin |
| 38 | + |
| 39 | +You can disable the Elixir plugin by running `devbox add elixir --disable-plugin`, or by setting the `disable_plugin` field in your `devbox.json`: |
27 | 40 |
|
28 | 41 | ```json
|
29 |
| - "shell": { |
30 |
| - "init_hook": [ |
31 |
| - "mkdir -p .nix-mix", |
32 |
| - "mkdir -p .nix-hex", |
33 |
| - "export MIX_HOME=$PWD/.nix-mix", |
34 |
| - "export HEX_HOME=$PWD/.nix-hex", |
35 |
| - "export ERL_AFLAGS='-kernel shell_history enabled'", |
36 |
| - "mix local.hex --force", |
37 |
| - "mix local.rebar --force" |
38 |
| - ] |
39 |
| - } |
| 42 | +{ |
| 43 | + "packages": { |
| 44 | + "elixir": { |
| 45 | + "version": "latest", |
| 46 | + "disable_plugin": true |
| 47 | + } |
| 48 | + }, |
| 49 | +} |
40 | 50 | ```
|
41 | 51 |
|
42 |
| -This will create local folders and force mix to install your Hex and Rebar packages to those folders. Now when you are in `devbox shell`, you can install using `mix deps`. |
| 52 | +Note that disabling the plugin will cause Mix and Hex to cache artifacts globally in the user's home directory (at `~/.mix/` and `~/.hex/`). This might actually be preferable if you're developing several Elixir projects and want to benefit from caching, but does defeat the isolation guarantees of Devbox. |
| 53 | + |
| 54 | +If the plugin is disabled, it's recommended to manually set `$ERL_AFLAGS` to preserve `iex` shell history: |
| 55 | + |
| 56 | +```json |
| 57 | + "env": { |
| 58 | + "ERL_AFLAGS": "-kernel shell_history enabled" |
| 59 | + } |
| 60 | +``` |
0 commit comments