@@ -2,30 +2,29 @@
22 xmlns:xlink="http://www.w3.org/1999/xlink"
33 xml:id="sec-beam">
44
5− <title>Beam Languages (Erlang, Elixir & LFE)</title>
5+ <title>BEAM Languages (Erlang, Elixir & LFE)</title>
66 <section xml:id="beam-introduction">
77 <title>Introduction</title>
88 <para>
9− In this document and related Nix expressions we use the term
10− <emphasis>Beam</emphasis> to describe the environment. BEAM is
11− the name of the Erlang Virtial Machine and, as far as we know,
12− from a packaging perspective all languages that run on BEAM are
13− interchangable. The things that do change, like the build
14− system, are transperant to the users of the package. So we make
15− no distinction.
9+ In this document and related Nix expressions, we use the term,
10+ <emphasis>BEAM</emphasis>, to describe the environment. BEAM is the name
11+ of the Erlang Virtual Machine and, as far as we're concerned, from a
12+ packaging perspective, all languages that run on the BEAM are
13+ interchangeable. That which varies, like the build system, is transparent
14+ to users of any given BEAM package, so we make no distinction.
1615 </para>
1716 </section>
1817 <section xml:id="beam-structure">
1918 <title>Structure</title>
2019 <para>
21− All Beam-related things are available via top-level
20+ All BEAM-related expressions are available via the top-level
2221 <literal>beam</literal> attribute, which includes:
2322 </para>
2423 <itemizedlist>
2524 <listitem>
2625 <para>
27− <literal>interpreters</literal>: contains a set of compilers running
28− on Beam, including multiple Erlang/OTP versions
26+ <literal>interpreters</literal>: a set of compilers running on the
27+ BEAM, including multiple Erlang/OTP versions
2928 (<literal>beam.interpreters.erlangR19</literal>, etc), Elixir
3029 (<literal>beam.interpreters.elixir</literal>) and LFE
3130 (<literal>beam.interpreters.lfe</literal>).
@@ -33,36 +32,37 @@
3332 </listitem>
3433 <listitem>
3534 <para>
36− <literal>packages</literal>: contains a set of package sets, each
37− compiled with a specific Erlang/OTP version, e.g.
35+ <literal>packages</literal>: a set of package sets, each compiled with
36+ a specific Erlang/OTP version, e.g.
3837 <literal>beam.packages.erlangR19</literal>.
3938 </para>
4039 </listitem>
4140 </itemizedlist>
4241 <para>
43− Default Erlang compiler is defined by
44− <literal>beam.interpreters.erlang</literal> and aliased as
45− <literal>erlang</literal>. Default package set with Beam packages is
46− defined by <literal>beam.packages.erlang</literal> and aliased at
47− top-level as <literal>beamPackages</literal>.
42+ The default Erlang compiler, defined by
43+ <literal>beam.interpreters.erlang</literal>, is aliased as
44+ <literal>erlang</literal>. The default BEAM package set is defined by
45+ <literal>beam.packages.erlang</literal> and aliased at the top level as
46+ <literal>beamPackages</literal>.
4847 </para>
4948 <para>
50− If you want to create a package set built with a custom Erlang version,
51− use lambda <literal>beam.packagesWith</literal>, which accepts an
52− Erlang/OTP derivative and produces a package set similar to
49+ To create a package set built with a custom Erlang version, use the
50+ lambda, <literal>beam.packagesWith</literal>, which accepts an Erlang/OTP
51+ derivation and produces a package set similar to
5352 <literal>beam.packages.erlang</literal>.
5453 </para>
5554 <para>
5655 Many Erlang/OTP distributions available in
57− <literal>beam.interpreters</literal> have their versions with ODBC and/or
58− Java enabled. For example there's
59− <literal>beam.interpreters.erlangR19_odbc_javac</literal> which
56+ <literal>beam.interpreters</literal> have versions with ODBC and/or Java
57+ enabled. For example, there's
58+ <literal>beam.interpreters.erlangR19_odbc_javac</literal>, which
6059 corresponds to <literal>beam.interpreters.erlangR19</literal>.
6160 </para>
62− <para>
63− We also provide <literal>beam.packages.erlang.callPackage</literal>, which
64− simplifies writing Beam package definitions, by injecting all packages from
65− <literal>beam.packages.erlang</literal> into top-level context.
61+ <para xml:id="erlang-call-package">
62+ We also provide the lambda,
63+ <literal>beam.packages.erlang.callPackage</literal>, which simplifies
64+ writing BEAM package definitions by injecting all packages from
65+ <literal>beam.packages.erlang</literal> into the top-level context.
6666 </para>
6767 </section>
6868 <section xml:id="build-tools">
@@ -70,42 +70,52 @@
7070 <section xml:id="build-tools-rebar3">
7171 <title>Rebar3</title>
7272 <para>
73− By default Rebar3 wants to manage it's own dependencies. This is perfectly
74− acceptable in the normal non-Nix setup. In the Nix world it is not. To
75− support this we have created two versions of rebar3,
76− <literal>rebar3</literal> and <literal>rebar3-open</literal>. The
77− <literal>rebar3</literal> version has been patched to remove the ability
78− to download anything from it. If you are not running it a nix-shell or a
79− nix-build then its probably not going to work for you.
80− <literal>rebar3-open</literal> is the normal, un-modified rebar3. It
81− should work exactly as would any other version of rebar3. Any Erlang
82− package should rely on <literal>rebar3</literal> and thats really what you
83− should be using too. See <literal>buildRebar3</literal> below.
73+ By default, Rebar3 wants to manage its own dependencies. This is perfectly
74+ acceptable in the normal, non-Nix setup, but in the Nix world, it is not.
75+ To rectify this, we provide two versions of Rebar3:
76+ <itemizedlist>
77+ <listitem>
78+ <para>
79+ <literal>rebar3</literal>: patched to remove the ability to download
80+ anything. When not running it via <literal>nix-shell</literal> or
81+ <literal>nix-build</literal>, it's probably not going to work as
82+ desired.
83+ </para>
84+ </listitem>
85+ <listitem>
86+ <para>
87+ <literal>rebar3-open</literal>: the normal, unmodified Rebar3. It
88+ should work exactly as would any other version of Rebar3. Any Erlang
89+ package should rely on <literal>rebar3</literal> instead. See <xref
90+ linkend="rebar3-packages"/>.
91+ </para>
92+ </listitem>
93+ </itemizedlist>
8494 </para>
8595 </section>
8696 <section xml:id="build-tools-other">
8797 <title>Mix & Erlang.mk</title>
8898 <para>
89− Both Mix and Erlang.mk work exactly as you would expect. There
90− is a bootstrap process that needs to be run for both of
91− them. However, that is supported by the
92− <literal>buildMix</literal> and <literal>buildErlangMk</literal> derivations.
99+ Both Mix and Erlang.mk work exactly as expected. There is a bootstrap
100+ process that needs to be run for both, however, which is supported by the
101+ <literal>buildMix</literal> and <literal>buildErlangMk</literal>
102+ derivations, respectively.
93103 </para>
94104 </section>
95105</section>
96106
97107<section xml:id="how-to-install-beam-packages">
98− <title>How to install Beam packages</title>
108+ <title>How to Install BEAM Packages</title>
99109 <para>
100− Beam packages are not registered in the top level simply because they are
110+ BEAM packages are not registered at the top level, simply because they are
101111 not relevant to the vast majority of Nix users. They are installable using
102− the <literal>beam.packages.erlang</literal> attribute set and aliased as
103− <literal>beamPackages</literal>. This attribute points at packages built by
104− the default Erlang/OTP version in Nixpkgs as defined by
112+ the <literal>beam.packages.erlang</literal> attribute set (aliased as
113+ <literal>beamPackages</literal>), which points to packages built by the
114+ default Erlang/OTP version in Nixpkgs, as defined by
105115 <literal>beam.interpreters.erlang</literal>.
106116
107− You can list the avialable packages in the
108− <literal>beamPackages</literal> with the following command:
117+ To list the available packages in
118+ <literal>beamPackages</literal>, use the following command:
109119 </para>
110120
111121 <programlisting>
@@ -119,145 +129,152 @@ beamPackages.meck meck-0.8.3
119129beamPackages.rebar3-pc pc-1.1.0
120130 </programlisting>
121131 <para>
122− To install any of those packages into your profile, refer to them by
123− their attribute path (first column):
132+ To install any of those packages into your profile, refer to them by their
133+ attribute path (first column):
124134 </para>
125135 <programlisting>
126136$ nix-env -f "<nixpkgs>" -iA beamPackages.ibrowse
127137 </programlisting>
128138 <para>
129− The attribute path of any Beam packages corresponds to the name
130− of that particular package in Hex or its OTP Application/Release name.
139+ The attribute path of any BEAM package corresponds to the name of that
140+ particular package in <link xlink:href="https://hex.pm">Hex</link> or its
141+ OTP Application/Release name.
131142 </para>
132143</section>
133144<section xml:id="packaging-beam-applications">
134− <title>Packaging Beam Applications</title>
145+ <title>Packaging BEAM Applications</title>
135146 <section xml:id="packaging-erlang-applications">
136147 <title>Erlang Applications</title>
137148 <section xml:id="rebar3-packages">
138149 <title>Rebar3 Packages</title>
139150 <para>
140− There is a Nix function called <literal>buildRebar3</literal> (defined
141− in <literal>beam.packages.erlang.buildRebar3</literal> and aliased at
142− top-level). We use this function to make a derivation that understands
143− how to build the rebar3 project. For example, the expression we use to
144− build the <link
145− xlink:href="https://github.com/erlang-nix/hex2nix">hex2nix</link>
146− project follows.
151+ The Nix function, <literal>buildRebar3</literal>, defined in
152+ <literal>beam.packages.erlang.buildRebar3</literal> and aliased at the
153+ top level, can be used to build a derivation that understands how to
154+ build a Rebar3 project. For example, we can build <link
155+ xlink:href="https://github.com/erlang-nix/hex2nix">hex2nix</link> as
156+ follows:
147157 </para>
148158 <programlisting>
149− {stdenv, fetchFromGitHub, buildRebar3, ibrowse, jsx, erlware_commons }:
159+ { stdenv, fetchFromGitHub, buildRebar3, ibrowse, jsx, erlware_commons }:
150160
151− buildRebar3 rec {
152− name = "hex2nix";
153− version = "0.0.1";
161+ buildRebar3 rec {
162+ name = "hex2nix";
163+ version = "0.0.1";
154164
155− src = fetchFromGitHub {
156− owner = "ericbmerritt";
157− repo = "hex2nix";
158− rev = "${version}";
159− sha256 = "1w7xjidz1l5yjmhlplfx7kphmnpvqm67w99hd2m7kdixwdxq0zqg";
160− };
165+ src = fetchFromGitHub {
166+ owner = "ericbmerritt";
167+ repo = "hex2nix";
168+ rev = "${version}";
169+ sha256 = "1w7xjidz1l5yjmhlplfx7kphmnpvqm67w99hd2m7kdixwdxq0zqg";
170+ };
161171
162172 beamDeps = [ ibrowse jsx erlware_commons ];
163173 }
164174 </programlisting>
165175 <para>
166− This derivation is callable with
167− <literal>beam.packages.erlang.callPackage</literal> (see above). If you
168− want to call this package using normal <literal>callPackage</literal>,
169− you should refer to dependency packages via
176+ Such derivations are callable with
177+ <literal>beam.packages.erlang.callPackage</literal> (see <xref
178+ linkend="erlang-call-package"/>). To call this package using the normal
179+ <literal>callPackage</literal>, refer to dependency packages via
170180 <literal>beamPackages</literal>, e.g.
171181 <literal>beamPackages.ibrowse</literal>.
172182 </para>
173183 <para>
174− The only visible difference between this derivation and
175− something like <literal>stdenv.mkDerivation</literal> is that we
176− have added <literal>beamDeps</literal> to the derivation. If
177− you add your Beam dependencies here they will be correctly
178− handled by the system.
184+ Notably, <literal>buildRebar3</literal> includes
185+ <literal>beamDeps</literal>, while
186+ <literal>stdenv.mkDerivation</literal> does not. BEAM dependencies added
187+ there will be correctly handled by the system.
179188 </para>
180189 <para>
181− If your package needs to compile native code via Rebar's port
182− compilation mechenism. You should add <literal>compilePort =
183− true;</literal> to the derivation.
190+ If a package needs to compile native code via Rebar3's port compilation
191+ mechanism, add <literal>compilePort = true;</literal> to the derivation.
184192 </para>
185193 </section>
186194 <section xml:id="erlang-mk-packages">
187195 <title>Erlang.mk Packages</title>
188196 <para>
189− Erlang.mk functions almost identically to Rebar. The only real
190− difference is that <literal>buildErlangMk</literal> is called
191− instead of <literal>buildRebar3</literal>
197+ Erlang.mk functions similarly to Rebar3, except we use
198+ <literal>buildErlangMk</literal> instead of
199+ <literal>buildRebar3</literal>.
192200 </para>
193201 <programlisting>
194− { buildErlangMk, fetchHex, cowlib, ranch }:
195− buildErlangMk {
196− name = "cowboy";
202+ { buildErlangMk, fetchHex, cowlib, ranch }:
203+
204+ buildErlangMk {
205+ name = "cowboy";
206+ version = "1.0.4";
207+
208+ src = fetchHex {
209+ pkg = "cowboy";
197210 version = "1.0.4";
198− src = fetchHex {
199− pkg = "cowboy";
200− version = "1.0.4";
201− sha256 =
202− "6a0edee96885fae3a8dd0ac1f333538a42e807db638a9453064ccfdaa6b9fdac";
203− };
204− beamDeps = [ cowlib ranch ];
205−
206− meta = {
207− description = ''Small, fast, modular HTTP server written in
208− Erlang.'';
209− license = stdenv.lib.licenses.isc;
210− homepage = "https://github.com/ninenines/cowboy";
211− };
211+ sha256 = "6a0edee96885fae3a8dd0ac1f333538a42e807db638a9453064ccfdaa6b9fdac";
212+ };
213+
214+ beamDeps = [ cowlib ranch ];
215+
216+ meta = {
217+ description = ''
218+ Small, fast, modular HTTP server written in Erlang
219+ '';
220+ license = stdenv.lib.licenses.isc;
221+ homepage = https://github.com/ninenines/cowboy;
222+ };
212223 }
213224 </programlisting>
214225 </section>
215226 <section xml:id="mix-packages">
216227 <title>Mix Packages</title>
217228 <para>
218− Mix functions almost identically to Rebar. The only real
219− difference is that <literal>buildMix</literal> is called
220− instead of <literal>buildRebar3</literal>
229+ Mix functions similarly to Rebar3, except we use
230+ <literal>buildMix</literal> instead of <literal>buildRebar3</literal>.
221231 </para>
222232 <programlisting>
223233 { buildMix, fetchHex, plug, absinthe }:
234+
224235 buildMix {
225236 name = "absinthe_plug";
226237 version = "1.0.0";
238+
227239 src = fetchHex {
228240 pkg = "absinthe_plug";
229241 version = "1.0.0";
230− sha256 =
231− "08459823fe1fd4f0325a8bf0c937a4520583a5a26d73b193040ab30a1dfc0b33";
242+ sha256 = "08459823fe1fd4f0325a8bf0c937a4520583a5a26d73b193040ab30a1dfc0b33";
232243 };
233− beamDeps = [ plug absinthe ];
244+
245+ beamDeps = [ plug absinthe ];
234246
235247 meta = {
236− description = ''A plug for Absinthe, an experimental GraphQL
237− toolkit'';
248+ description = ''
249+ A plug for Absinthe, an experimental GraphQL toolkit
250+ '';
238251 license = stdenv.lib.licenses.bsd3;
239− homepage = "https://github.com/CargoSense/absinthe_plug";
240− };
241− }
252+ homepage = https://github.com/CargoSense/absinthe_plug;
253+ };
254+ }
242255 </programlisting>
243256 <para>
244− Alternatively one can use <literal>buildHex</literal> as a shortcut for the above:
257+ Alternatively, we can use <literal>buildHex</literal> as a shortcut:
245258 </para>
246259 <programlisting>
247260 { buildHex, buildMix, plug, absinthe }:
261+
248262 buildHex {
249263 name = "absinthe_plug";
250264 version = "1.0.0";
251− sha256 =
252− "08459823fe1fd4f0325a8bf0c937a4520583a5a26d73b193040ab30a1dfc0b33";
265+
266+ sha256 = "08459823fe1fd4f0325a8bf0c937a4520583a5a26d73b193040ab30a1dfc0b33";
267+
253268 builder = buildMix;
254− beamDeps = [ plug absinthe];
269+
270+ beamDeps = [ plug absinthe ];
255271
256272 meta = {
257− description = ''A plug for Absinthe, an experimental GraphQL
258− toolkit'';
273+ description = ''
274+ A plug for Absinthe, an experimental GraphQL toolkit
275+ '';
259276 license = stdenv.lib.licenses.bsd3;
260− homepage = "https://github.com/CargoSense/absinthe_plug";
277+ homepage = https://github.com/CargoSense/absinthe_plug;
261278 };
262279 }
263280 </programlisting>
@@ -265,18 +282,18 @@ $ nix-env -f "<nixpkgs>" -iA beamPackages.ibrowse
265282 </section>
266283</section>
267284<section xml:id="how-to-develop">
268− <title>How to develop</title>
285+ <title>How to Develop</title>
269286 <section xml:id="accessing-an-environment">
270287 <title>Accessing an Environment</title>
271288 <para>
272− Often, all you want to do is be able to access a valid
273− environment that contains a specific package and its
274− dependencies. we can do that with the <literal>env</literal>
275− part of a derivation. For example, lets say we want to access an
276− erlang repl with ibrowse loaded up. We could do the following.
289+ Often, we simply want to access a valid environment that contains a
290+ specific package and its dependencies. We can accomplish that with the
291+ <literal>env</literal> attribute of a derivation. For example, let's say
292+ we want to access an Erlang REPL with <literal>ibrowse</literal> loaded
293+ up. We could do the following:
277294 </para>
278295 <programlisting>
279− ~/w/nixpkgs ❯❯❯ nix-shell -A beamPackages.ibrowse.env --run "erl"
296+ $ nix-shell -A beamPackages.ibrowse.env --run "erl"
280297 Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
281298
282299 Eshell V7.0 (abort with ^G)
@@ -317,20 +334,19 @@ $ nix-env -f "<nixpkgs>" -iA beamPackages.ibrowse
317334 2>
318335 </programlisting>
319336 <para>
320− Notice the <literal>-A beamPackages.ibrowse.env</literal>.That
321− is the key to this functionality.
337+ Notice the <literal>-A beamPackages.ibrowse.env</literal>. That is the key
338+ to this functionality.
322339 </para>
323340 </section>
324341 <section xml:id="creating-a-shell">
325342 <title>Creating a Shell</title>
326343 <para>
327344 Getting access to an environment often isn't enough to do real
328− development. Many times we need to create a
329− <literal>shell.nix</literal> file and do our development inside
330− of the environment specified by that file. This file looks a lot
331− like the packaging described above. The main difference is that
332− <literal>src</literal> points to project root and we call the
333− package directly.
345+ development. Usually, we need to create a <literal>shell.nix</literal>
346+ file and do our development inside of the environment specified therein.
347+ This file looks a lot like the packaging described above, except that
348+ <literal>src</literal> points to the project root and we call the package
349+ directly.
334350 </para>
335351 <programlisting>
336352{ pkgs ? import "<nixpkgs"> {} }:
@@ -349,13 +365,14 @@ let
349365 drv = beamPackages.callPackage f {};
350366
351367in
352− drv
368+
369+ drv
353370 </programlisting>
354371 <section xml:id="building-in-a-shell">
355− <title>Building in a shell (for mix projects)</title>
372+ <title>Building in a Shell (for Mix Projects)</title>
356373 <para>
357− We can leverage the support of the Derivation, regardless of
358− which build Derivation is called by calling the commands themselves.
374+ We can leverage the support of the derivation, irrespective of the build
375+ derivation, by calling the commands themselves.
359376 </para>
360377 <programlisting>
361378# =============================================================================
@@ -415,42 +432,43 @@ analyze: build plt
415432
416433 </programlisting>
417434 <para>
418− If you add the <literal>shell.nix</literal> as described and
419− user rebar as follows things should simply work. Aside from the
435+ Using a <literal>shell.nix</literal> as described (see <xref
436+ linkend="creating-a-shell"/>) should just work. Aside from
420437 <literal>test</literal>, <literal>plt</literal>, and
421− <literal>analyze</literal> the talks work just fine for all of
422− the build Derivations.
438+ <literal>analyze</literal>, the Make targets work just fine for all of the
439+ build derivations.
423440 </para>
424441 </section>
425442</section>
426443</section>
427444<section xml:id="generating-packages-from-hex-with-hex2nix">
428− <title>Generating Packages from Hex with Hex2Nix</title>
445+ <title>Generating Packages from Hex with <literal>hex2nix</literal></title>
429446 <para>
430− Updating the Hex packages requires the use of the
431− <literal>hex2nix</literal> tool. Given the path to the Erlang
432− modules (usually
433− <literal>pkgs/development/erlang-modules</literal>). It will
434− dump a file called
435− <literal>hex-packages.nix</literal>. That file will contain all
436− the packages that use a recognized build system in Hex. However,
437− it can't know whether or not all those packages are buildable.
447+ Updating the <link xlink:href="https://hex.pm">Hex</link> package set
448+ requires <link
449+ xlink:href="https://github.com/erlang-nix/hex2nix">hex2nix</link>. Given the
450+ path to the Erlang modules (usually
451+ <literal>pkgs/development/erlang-modules</literal>), it will dump a file
452+ called <literal>hex-packages.nix</literal>, containing all the packages that
453+ use a recognized build system in <link
454+ xlink:href="https://hex.pm">Hex</link>. It can't be determined, however,
455+ whether every package is buildable.
438456 </para>
439457 <para>
440− To make life easier for our users, it makes good sense to go
441− ahead and attempt to build all those packages and remove the
442− ones that don't build. To do that, simply run the command (in
443− the root of your <literal>nixpkgs</literal> repository). that follows.
458+ To make life easier for our users, try to build every <link
459+ xlink:href="https://hex.pm">Hex</link> package and remove those that fail.
460+ To do that, simply run the following command in the root of your
461+ <literal>nixpkgs</literal> repository:
444462 </para>
445463 <programlisting>
446464$ nix-build -A beamPackages
447465 </programlisting>
448466 <para>
449− That will build every package in
450− <literal>beamPackages</literal>. Then you can go through and
451− manually remove the ones that fail. Hopefully, someone will
452− improve <literal>hex2nix</literal> in the future to automate
453− that.
467+ That will attempt to build every package in
468+ <literal>beamPackages</literal>. Then manually remove those that fail.
469+ Hopefully, someone will improve <link
470+ xlink:href="https://github.com/erlang-nix/hex2nix">hex2nix</link> in the
471+ future to automate the process.
454472 </para>
455473</section>
456474</section>