1<section xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="sec-language-ruby">
4
5<title>Ruby</title>
6
7 <para>There currently is support to bundle applications that are packaged as Ruby gems. The utility "bundix" allows you to write a <filename>Gemfile</filename>, let bundler create a <filename>Gemfile.lock</filename>, and then convert
8 this into a nix expression that contains all Gem dependencies automatically.</para>
9
10 <para>For example, to package sensu, we did:</para>
11
12<screen>
13<![CDATA[$ cd pkgs/servers/monitoring
14$ mkdir sensu
15$ cat > Gemfile
16source 'https://rubygems.org'
17gem 'sensu'
18$ bundler package --path /tmp/vendor/bundle
19$ $(nix-build '<nixpkgs>' -A bundix)/bin/bundix
20$ cat > default.nix
21{ lib, bundlerEnv, ruby }:
22
23bundlerEnv {
24 name = "sensu-0.17.1";
25
26 inherit ruby;
27 gemfile = ./Gemfile;
28 lockfile = ./Gemfile.lock;
29 gemset = ./gemset.nix;
30
31 meta = with lib; {
32 description = "A monitoring framework that aims to be simple, malleable,
33and scalable.";
34 homepage = http://sensuapp.org/;
35 license = with licenses; mit;
36 maintainers = with maintainers; [ theuni ];
37 platforms = platforms.unix;
38 };
39}]]>
40</screen>
41
42<para>Please check in the <filename>Gemfile</filename>, <filename>Gemfile.lock</filename> and the <filename>gemset.nix</filename> so future updates can be run easily.
43</para>
44
45<para>Resulting derivations also have two helpful items, <literal>env</literal> and <literal>wrapper</literal>. The first one allows one to quickly drop into
46<command>nix-shell</command> with the specified environment present. E.g. <command>nix-shell -A sensu.env</command> would give you an environment with Ruby preset
47so it has all the libraries necessary for <literal>sensu</literal> in its paths. The second one can be used to make derivations from custom Ruby scripts which have
48<filename>Gemfile</filename>s with their dependencies specified. It is a derivation with <command>ruby</command> wrapped so it can find all the needed dependencies.
49For example, to make a derivation <literal>my-script</literal> for a <filename>my-script.rb</filename> (which should be placed in <filename>bin</filename>) you should
50run <command>bundix</command> as specified above and then use <literal>bundlerEnv</literal> lile this:</para>
51
52<programlisting>
53<![CDATA[let env = bundlerEnv {
54 name = "my-script-env";
55
56 inherit ruby;
57 gemfile = ./Gemfile;
58 lockfile = ./Gemfile.lock;
59 gemset = ./gemset.nix;
60};
61
62in stdenv.mkDerivation {
63 name = "my-script";
64
65 buildInputs = [ env.wrapper ];
66
67 script = ./my-script.rb;
68
69 buildCommand = ''
70 mkdir -p $out/bin
71 install -D -m755 $script $out/bin/my-script
72 patchShebangs $out/bin/my-script
73 '';
74}]]>
75</programlisting>
76
77</section>
78