Skip to content

Commit 5fa89f8

Browse files
authored
Add simple elixir plugin (#2462)
## Summary Adds a very basic elixir plugin to simplify installation to a simple `devbox add elixir`. Also now properly stores Mix/Hex artifacts in `.devbox/virtenv`. ## How was it tested? - Tested it against the existing elixir example in the repo. Seems to work fine. - Tested on my own projects as well, separately on Mac + Ubuntu.
1 parent 199dc92 commit 5fa89f8

File tree

6 files changed

+69
-43
lines changed

6 files changed

+69
-43
lines changed

docs/app/docs/devbox_examples/languages/elixir.md

+37-19
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,59 @@
22
title: Elixir
33
---
44

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`.
66

77
[**Example Repo**](https://github.com/jetify-com/devbox/tree/main/examples/development/elixir/elixir_hello)
88

99
[![Open In Devspace](../../../static/img/open-in-devspace.svg)](https://auth.jetify.com/devspace/templates/elixir/)
1010

1111
## Adding Elixir to your project
1212

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`
1414

1515
```json
1616
"packages": [
17-
"elixir@latest",
18-
"bash@latest"
17+
"elixir@latest"
1918
],
2019
```
2120

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)
2322

24-
## Installing Hex and Rebar locally
23+
## Elixir Plugin Support
2524

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`:
2740

2841
```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+
}
4050
```
4151

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+
```

docs/app/docs/guides/plugins.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This doc describes how to use Devbox Plugins with your project. **Plugins** prov
88

99
### Built-in Plugins
1010

11-
If you add one of the packages listed above to your project using `devbox add <pkg>`, Devbox will automatically activate the plugin for that package.
11+
If you add one of the packages listed below to your project using `devbox add <pkg>`, Devbox will automatically activate the plugin for that package.
1212

1313
You can also explicitly add a built-in plugin in your project by adding it to the [`include` section](../configuration.md#include) of your `devbox.json` file. For example, to explicitly add the plugin for Nginx, you can add the following to your `devbox.json` file:
1414

@@ -34,6 +34,7 @@ Built-in plugins are available for the following packages. You can activate the
3434
* [PHP](../devbox_examples/languages/php.md) (php, php80, php81, php82...)
3535
* [Python](../devbox_examples/languages/python.md) (python, python-full, python-minimal...)
3636
* [Ruby](../devbox_examples/languages/ruby.md)(ruby, ruby_3_1, ruby_3_0...)
37+
* [Elixir](../devbox_examples/languages/elixir.md)(elixir, elixir_1_16, elixir_1_15...)
3738

3839

3940
### Local Plugins

examples/development/elixir/elixir_hello/devbox.json

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
{
2-
"packages": {
3-
"elixir": "latest"
4-
},
5-
"env": {
6-
"MIX_HOME": "$PWD/.nix-mix",
7-
"HEX_HOME": "$PWD/.nix-hex",
8-
"ERL_AFLAGS": "-kernel shell_history enabled"
9-
},
2+
"packages": [
3+
"elixir@latest"
4+
],
105
"shell": {
116
"init_hook": [
12-
"mkdir -p .nix-mix",
13-
"mkdir -p .nix-hex",
14-
"mix local.hex --force",
15-
"mix local.rebar --force",
167
"mix deps.get"
178
],
189
"scripts": {

examples/development/elixir/elixir_hello/devbox.lock

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,51 @@
22
"lockfile_version": "1",
33
"packages": {
44
"elixir@latest": {
5-
"last_modified": "2024-02-10T18:15:24Z",
6-
"resolved": "github:NixOS/nixpkgs/10b813040df67c4039086db0f6eaf65c536886c6#elixir",
5+
"last_modified": "2024-11-28T07:51:56Z",
6+
"plugin_version": "0.0.1",
7+
"resolved": "github:NixOS/nixpkgs/226216574ada4c3ecefcbbec41f39ce4655f78ef#elixir",
78
"source": "devbox-search",
8-
"version": "1.15.7",
9+
"version": "1.17.3",
910
"systems": {
1011
"aarch64-darwin": {
1112
"outputs": [
1213
{
1314
"name": "out",
14-
"path": "/nix/store/kf294q6pwmqv8hb5ivis509y1wjjgm09-elixir-1.15.7",
15+
"path": "/nix/store/91w79z55qsjkhnbs3a21l3h27va98mf6-elixir-1.17.3",
1516
"default": true
1617
}
1718
],
18-
"store_path": "/nix/store/kf294q6pwmqv8hb5ivis509y1wjjgm09-elixir-1.15.7"
19+
"store_path": "/nix/store/91w79z55qsjkhnbs3a21l3h27va98mf6-elixir-1.17.3"
1920
},
2021
"aarch64-linux": {
2122
"outputs": [
2223
{
2324
"name": "out",
24-
"path": "/nix/store/f5x2w7df45vahjcpjf76dv101il8xq98-elixir-1.15.7",
25+
"path": "/nix/store/pz4hk0sp4zj76waaprlfdmvc4xdblz55-elixir-1.17.3",
2526
"default": true
2627
}
2728
],
28-
"store_path": "/nix/store/f5x2w7df45vahjcpjf76dv101il8xq98-elixir-1.15.7"
29+
"store_path": "/nix/store/pz4hk0sp4zj76waaprlfdmvc4xdblz55-elixir-1.17.3"
2930
},
3031
"x86_64-darwin": {
3132
"outputs": [
3233
{
3334
"name": "out",
34-
"path": "/nix/store/6vv8qlvhpi62nk5d06lhi5isvlg7bcj9-elixir-1.15.7",
35+
"path": "/nix/store/gnjg57wv71svvqw7s3rxyjc6lkps2r95-elixir-1.17.3",
3536
"default": true
3637
}
3738
],
38-
"store_path": "/nix/store/6vv8qlvhpi62nk5d06lhi5isvlg7bcj9-elixir-1.15.7"
39+
"store_path": "/nix/store/gnjg57wv71svvqw7s3rxyjc6lkps2r95-elixir-1.17.3"
3940
},
4041
"x86_64-linux": {
4142
"outputs": [
4243
{
4344
"name": "out",
44-
"path": "/nix/store/dlq14nswvq479pmnrwlyn2hyk6cwp65a-elixir-1.15.7",
45+
"path": "/nix/store/rx7qr8bar4qldx6yg3njvm8hn84d3yyk-elixir-1.17.3",
4546
"default": true
4647
}
4748
],
48-
"store_path": "/nix/store/dlq14nswvq479pmnrwlyn2hyk6cwp65a-elixir-1.15.7"
49+
"store_path": "/nix/store/rx7qr8bar4qldx6yg3njvm8hn84d3yyk-elixir-1.17.3"
4950
}
5051
}
5152
}

plugins/builtins.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type BuiltIn struct{}
2727
var builtInMap = map[*regexp.Regexp]string{
2828
regexp.MustCompile(`^(apache|apacheHttpd)$`): "apacheHttpd",
2929
regexp.MustCompile(`^(gradle|gradle_[0-9])$`): "gradle",
30+
regexp.MustCompile(`^elixir_?([0-9_]*[0-9]+)?$`): "elixir",
3031
regexp.MustCompile(`^(ghc|haskell\.compiler\.(.*))$`): "haskell",
3132
regexp.MustCompile(`(^mariadb(-embedded)?_?[0-9]*$|^mysql$)`): "mariadb",
3233
regexp.MustCompile(`^mysql(80|57|50)$`): "mysql",

plugins/elixir.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "elixir",
3+
"version": "0.0.1",
4+
"packages": {
5+
"darwin.apple_sdk.frameworks.CoreServices": {
6+
"platforms": ["aarch64-darwin", "x86_64-darwin"]
7+
}
8+
},
9+
"env": {
10+
"MIX_HOME": "{{ .Virtenv }}/mix",
11+
"HEX_HOME": "{{ .Virtenv }}/hex",
12+
"ERL_AFLAGS": "-kernel shell_history enabled"
13+
}
14+
}

0 commit comments

Comments
 (0)