@@ -0,0 +1,288 @@
1+<chapter xmlns="http://docbook.org/ns/docbook"
2+ xmlns:xlink="http://www.w3.org/1999/xlink"
3+ xml:id="users-guide-to-the-erlang-infrastructure">
4+
5+<title>User's Guide to the Erlang Infrastructure</title>
6+
7+<section xml:id="how-to-install-erlang-packages">
8+ <title>How to install Erlang packages</title>
9+ <para>
10+ Erlang packages are not registered in the top level simply because
11+ they are not relevant to the vast majority of Nix users. They are
12+ installable using the <literal>erlangPackages</literal> attribute set.
13+
14+ You can list the avialable packages in the
15+ <literal>erlangPackages</literal> with the following command:
16+ </para>
17+
18+ <programlisting>
19+$ nix-env -f "<nixpkgs>" -qaP -A erlangPackages
20+erlangPackages.esqlite esqlite-0.2.1
21+erlangPackages.goldrush goldrush-0.1.7
22+erlangPackages.ibrowse ibrowse-4.2.2
23+erlangPackages.jiffy jiffy-0.14.5
24+erlangPackages.lager lager-3.0.2
25+erlangPackages.meck meck-0.8.3
26+erlangPackages.rebar3-pc pc-1.1.0
27+ </programlisting>
28+ <para>
29+ To install any of those packages into your profile, refer to them by
30+ their attribute path (first column):
31+ </para>
32+ <programlisting>
33+$ nix-env -f "<nixpkgs>" -iA erlangPackages.ibrowse
34+ </programlisting>
35+ <para>
36+ The attribute path of any Erlang packages corresponds to the name
37+ of that particular package in Hex or its OTP Application/Release name.
38+ </para>
39+</section>
40+<section xml:id="packaging-erlang-applications">
41+ <title>Packaging Erlang Applications</title>
42+ <section xml:id="rebar3-packages">
43+ <title>Rebar3 Packages</title>
44+ <para>
45+ There is a Nix functional called
46+ <literal>buildRebar3</literal>. We use this function to make a
47+ derivation that understands how to build the rebar3 project. For
48+ example, the epression we use to build the <link
49+ xlink:href="https://github.com/erlang-nix/hex2nix">hex2nix</link>
50+ project follows.
51+ </para>
52+ <programlisting>
53+{stdenv, fetchFromGitHub, buildRebar3, ibrowse, jsx, erlware_commons }:
54+
55+buildRebar3 rec {
56+ name = "hex2nix";
57+ version = "0.0.1";
58+
59+ src = fetchFromGitHub {
60+ owner = "ericbmerritt";
61+ repo = "hex2nix";
62+ rev = "${version}";
63+ sha256 = "1w7xjidz1l5yjmhlplfx7kphmnpvqm67w99hd2m7kdixwdxq0zqg";
64+ };
65+
66+ erlangDeps = [ ibrowse jsx erlware_commons ];
67+}
68+ </programlisting>
69+ <para>
70+ The only visible difference between this derivation and
71+ something like <literal>stdenv.mkDerivation</literal> is that we
72+ have added <literal>erlangDeps</literal> to the derivation. If
73+ you add your Erlang dependencies here they will be correctly
74+ handled by the system.
75+ </para>
76+ <para>
77+ If your package needs to compile native code via Rebar's port
78+ compilation mechenism. You should add <literal>compilePort =
79+ true;</literal> to the derivation.
80+ </para>
81+ </section>
82+
83+ <section xml:id="hex-packages">
84+ <title>Hex Packages</title>
85+ <para>
86+ Hex packages are based on Rebar packages. In fact, at the moment
87+ we can only compile Hex packages that are buildable with
88+ Rebar3. Packages that use Mix and other build systems are not
89+ supported. That being said, we know a lot more about Hex and can
90+ do more for you.
91+ </para>
92+ <programlisting>
93+{ buildHex }:
94+ buildHex {
95+ name = "esqlite";
96+ version = "0.2.1";
97+ sha256 = "1296fn1lz4lz4zqzn4dwc3flgkh0i6n4sydg501faabfbv8d3wkr";
98+ compilePort = true;
99+}
100+ </programlisting>
101+ <para>
102+ For Hex packages you need to provide the name, the version, and
103+ the Sha 256 digest of the package and use
104+ <literal>buildHex</literal> to build it. Obviously, the package
105+ needs to have already been published to Hex.
106+ </para>
107+ </section>
108+</section>
109+<section xml:id="how-to-develop">
110+ <title>How to develop</title>
111+ <section xml:id="accessing-an-environment">
112+ <title>Accessing an Environment</title>
113+ <para>
114+ Often, all you want to do is be able to access a valid
115+ environment that contains a specific package and its
116+ dependencies. we can do that with the <literal>env</literal>
117+ part of a derivation. For example, lets say we want to access an
118+ erlang repl with ibrowse loaded up. We could do the following.
119+ </para>
120+ <programlisting>
121+ ~/w/nixpkgs ❯❯❯ nix-shell -A erlangPackages.ibrowse.env --run "erl"
122+ Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
123+
124+ Eshell V7.0 (abort with ^G)
125+ 1> m(ibrowse).
126+ Module: ibrowse
127+ MD5: 3b3e0137d0cbb28070146978a3392945
128+ Compiled: January 10 2016, 23:34
129+ Object file: /nix/store/g1rlf65rdgjs4abbyj4grp37ry7ywivj-ibrowse-4.2.2/lib/erlang/lib/ibrowse-4.2.2/ebin/ibrowse.beam
130+ Compiler options: [{outdir,"/tmp/nix-build-ibrowse-4.2.2.drv-0/hex-source-ibrowse-4.2.2/_build/default/lib/ibrowse/ebin"},
131+ debug_info,debug_info,nowarn_shadow_vars,
132+ warn_unused_import,warn_unused_vars,warnings_as_errors,
133+ {i,"/tmp/nix-build-ibrowse-4.2.2.drv-0/hex-source-ibrowse-4.2.2/_build/default/lib/ibrowse/include"}]
134+ Exports:
135+ add_config/1 send_req_direct/7
136+ all_trace_off/0 set_dest/3
137+ code_change/3 set_max_attempts/3
138+ get_config_value/1 set_max_pipeline_size/3
139+ get_config_value/2 set_max_sessions/3
140+ get_metrics/0 show_dest_status/0
141+ get_metrics/2 show_dest_status/1
142+ handle_call/3 show_dest_status/2
143+ handle_cast/2 spawn_link_worker_process/1
144+ handle_info/2 spawn_link_worker_process/2
145+ init/1 spawn_worker_process/1
146+ module_info/0 spawn_worker_process/2
147+ module_info/1 start/0
148+ rescan_config/0 start_link/0
149+ rescan_config/1 stop/0
150+ send_req/3 stop_worker_process/1
151+ send_req/4 stream_close/1
152+ send_req/5 stream_next/1
153+ send_req/6 terminate/2
154+ send_req_direct/4 trace_off/0
155+ send_req_direct/5 trace_off/2
156+ send_req_direct/6 trace_on/0
157+ trace_on/2
158+ ok
159+ 2>
160+ </programlisting>
161+ <para>
162+ Notice the <literal>-A erlangPackages.ibrowse.env</literal>.That
163+ is the key to this functionality.
164+ </para>
165+ </section>
166+ <section xml:id="creating-a-shell">
167+ <title>Creating a Shell</title>
168+ <para>
169+ Getting access to an environment often isn't enough to do real
170+ development. Many times we need to create a
171+ <literal>shell.nix</literal> file and do our development inside
172+ of the environment specified by that file. This file looks a lot
173+ like the packageing described above. The main difference is that
174+ <literal>src</literal> points to project root and we call the
175+ package directly.
176+ </para>
177+ <programlisting>
178+{ pkgs ? import "<nixpkgs"> {} }:
179+
180+with pkgs;
181+
182+let
183+
184+ f = { buildHex, ibrowse, jsx, erlware_commons }:
185+ buildHex {
186+ name = "hex2nix";
187+ version = "0.1.0";
188+ src = ./.;
189+ erlangDeps = [ ibrowse jsx erlware_commons ];
190+ };
191+ drv = erlangPackages.callPackage f {};
192+
193+in
194+ drv
195+ </programlisting>
196+ <section xml:id="building-in-a-shell">
197+ <title>Building in a shell</title>
198+ <para>
199+ Unfortunatly for us users of Nix, Rebar isn't very cooperative
200+ with us from the standpoint of building a hermetic
201+ environment. When building the rebar3 support we had to do some
202+ sneaky things to get it not to go out and pull packages on its
203+ own. Also unfortunately, you have to do some of the same things
204+ when building a project inside of a Nix shell.
205+
206+ <orderedlist numeration="arabic">
207+ <listitem>
208+ <para>Run <literal>rebar3-nix-bootstrap</literal> every time
209+ dependencies change</para>
210+ </listitem>
211+ <listitem>
212+ <para>Set Home to the current directory.</para>
213+ </listitem>
214+ </orderedlist>
215+
216+ If you do these two things then Rebar will be happy with you. I
217+ codify these into a makefile. Forunately, rebar3-nix-bootstrap
218+ is idempotent and fairly quick. so you can run it as often as
219+ you like.
220+ </para>
221+ <programlisting>
222+# =============================================================================
223+# Rules
224+# =============================================================================
225+.PHONY= all test clean repl shell build test analyze bootstrap
226+
227+all: test
228+
229+clean:
230+ rm -rf _build
231+ rm -rf .cache
232+
233+repl:
234+ nix-shell --run "erl"
235+
236+shell:
237+ nix-shell --run "bash"
238+
239+bootstrap:
240+ nix-shell --pure --run "rebar3-nix-bootstrap"
241+
242+build: bootstrap
243+ nix-shell --pure --run "HOME=$(CURDIR) rebar3 compile"
244+
245+analyze: bootstrap
246+ nix-shell --pure --run "HOME=$(CURDIR) rebar3 do compile,dialyzer"
247+
248+test: bootstrap
249+ nix-shell --pure --run "HOME=$(CURDIR) rebar3 do compile,dialyzer,eunit"
250+
251+ </programlisting>
252+ <para>
253+ If you add the <literal>shell.nix</literal> as described and
254+ user rebar as follows things should simply work.
255+ </para>
256+ </section>
257+</section>
258+</section>
259+<section xml:id="generating-packages-from-hex-with-hex2nix">
260+ <title>Generating Packages from Hex with Hex2Nix</title>
261+ <para>
262+ Updating the Hex packages requires the use of the
263+ <literal>hex2nix</literal> tool. Given the path to the Erlang
264+ modules (usually
265+ <literal>pkgs/development/erlang-modules</literal>). It will
266+ happily dump a file called
267+ <literal>hex-packages.nix</literal>. That file will contain all
268+ the packages that use a recognized build system in Hex. However,
269+ it can't know whether or not all those packages are buildable.
270+ </para>
271+ <para>
272+ To make life easier for our users, it makes good sense to go
273+ ahead and attempt to build all those packages and remove the
274+ ones that don't build. To do that, simply run the command (in
275+ the root of your <literal>nixpkgs</literal> repository). that follows.
276+ </para>
277+ <programlisting>
278+$ nix-build -A erlangPackages
279+ </programlisting>
280+ <para>
281+ That will build every package in
282+ <literal>erlangPackages</literal>. Then you can go through and
283+ manually remove the ones that fail. Hopefully, someone will
284+ improve <literal>hex2nix</literal> in the future to automate
285+ that.
286+ </para>
287+</section>
288+</chapter>