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$ cd sensu
16$ cat > Gemfile
17source 'https://rubygems.org'
18gem 'sensu'
19$ nix-shell -p bundler --command "bundler package --path /tmp/vendor/bundle"
20$ $(nix-build '<nixpkgs>' -A bundix)/bin/bundix
21$ cat > default.nix
22{ lib, bundlerEnv, ruby }:
23
24bundlerEnv rec {
25 name = "sensu-${version}";
26
27 version = (import gemset).sensu.version;
28 inherit ruby;
29 gemfile = ./Gemfile;
30 lockfile = ./Gemfile.lock;
31 gemset = ./gemset.nix;
32
33 meta = with lib; {
34 description = "A monitoring framework that aims to be simple, malleable, and scalable";
35 homepage = http://sensuapp.org/;
36 license = with licenses; mit;
37 maintainers = with maintainers; [ theuni ];
38 platforms = platforms.unix;
39 };
40}]]>
41</screen>
42
43<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.
44</para>
45
46<para>Resulting derivations also have two helpful items, <literal>env</literal> and <literal>wrapper</literal>. The first one allows one to quickly drop into
47<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
48so 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
49<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.
50For 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
51run <command>bundix</command> as specified above and then use <literal>bundlerEnv</literal> lile this:</para>
52
53<programlisting>
54<![CDATA[let env = bundlerEnv {
55 name = "my-script-env";
56
57 inherit ruby;
58 gemfile = ./Gemfile;
59 lockfile = ./Gemfile.lock;
60 gemset = ./gemset.nix;
61};
62
63in stdenv.mkDerivation {
64 name = "my-script";
65
66 buildInputs = [ env.wrapper ];
67
68 script = ./my-script.rb;
69
70 buildCommand = ''
71 mkdir -p $out/bin
72 install -D -m755 $script $out/bin/my-script
73 patchShebangs $out/bin/my-script
74 '';
75}]]>
76</programlisting>
77
78</section>
79