1<section xmlns="http://docbook.org/ns/docbook" 2 xmlns:xlink="http://www.w3.org/1999/xlink" 3 xml:id="sec-language-ruby"> 4 <title>Ruby</title> 5 6 <para> 7 There currently is support to bundle applications that are packaged as Ruby 8 gems. The utility "bundix" allows you to write a 9 <filename>Gemfile</filename>, let bundler create a 10 <filename>Gemfile.lock</filename>, and then convert this into a nix 11 expression that contains all Gem dependencies automatically. 12 </para> 13 14 <para> 15 For example, to package sensu, we did: 16 </para> 17 18<screen> 19<![CDATA[$ cd pkgs/servers/monitoring 20$ mkdir sensu 21$ cd sensu 22$ cat > Gemfile 23source 'https://rubygems.org' 24gem 'sensu' 25$ $(nix-build '<nixpkgs>' -A bundix --no-out-link)/bin/bundix --magic 26$ cat > default.nix 27{ lib, bundlerEnv, ruby }: 28 29bundlerEnv rec { 30 name = "sensu-${version}"; 31 32 version = (import gemset).sensu.version; 33 inherit ruby; 34 # expects Gemfile, Gemfile.lock and gemset.nix in the same directory 35 gemdir = ./.; 36 37 meta = with lib; { 38 description = "A monitoring framework that aims to be simple, malleable, and scalable"; 39 homepage = http://sensuapp.org/; 40 license = with licenses; mit; 41 maintainers = with maintainers; [ theuni ]; 42 platforms = platforms.unix; 43 }; 44}]]> 45</screen> 46 47 <para> 48 Please check in the <filename>Gemfile</filename>, 49 <filename>Gemfile.lock</filename> and the <filename>gemset.nix</filename> so 50 future updates can be run easily. 51 </para> 52 53 <para> 54 For tools written in Ruby - i.e. where the desire is to install a package and 55 then execute e.g. <command>rake</command> at the command line, there is an 56 alternative builder called <literal>bundlerApp</literal>. Set up the 57 <filename>gemset.nix</filename> the same way, and then, for example: 58 </para> 59 60<screen> 61<![CDATA[{ lib, bundlerApp }: 62 63bundlerApp { 64 pname = "corundum"; 65 gemdir = ./.; 66 exes = [ "corundum-skel" ]; 67 68 meta = with lib; { 69 description = "Tool and libraries for maintaining Ruby gems."; 70 homepage = https://github.com/nyarly/corundum; 71 license = licenses.mit; 72 maintainers = [ maintainers.nyarly ]; 73 platforms = platforms.unix; 74 }; 75}]]> 76</screen> 77 78 <para> 79 The chief advantage of <literal>bundlerApp</literal> over 80 <literal>bundlerEnv</literal> is the executables introduced in the 81 environment are precisely those selected in the <literal>exes</literal> list, 82 as opposed to <literal>bundlerEnv</literal> which adds all the executables 83 made available by gems in the gemset, which can mean e.g. 84 <command>rspec</command> or <command>rake</command> in unpredictable versions 85 available from various packages. 86 </para> 87 88 <para> 89 Resulting derivations for both builders also have two helpful attributes, 90 <literal>env</literal> and <literal>wrappedRuby</literal>. The first one 91 allows one to quickly drop into <command>nix-shell</command> with the 92 specified environment present. E.g. <command>nix-shell -A sensu.env</command> 93 would give you an environment with Ruby preset so it has all the libraries 94 necessary for <literal>sensu</literal> in its paths. The second one can be 95 used to make derivations from custom Ruby scripts which have 96 <filename>Gemfile</filename>s with their dependencies specified. It is a 97 derivation with <command>ruby</command> wrapped so it can find all the needed 98 dependencies. For example, to make a derivation <literal>my-script</literal> 99 for a <filename>my-script.rb</filename> (which should be placed in 100 <filename>bin</filename>) you should run <command>bundix</command> as 101 specified above and then use <literal>bundlerEnv</literal> like this: 102 </para> 103 104<programlisting> 105<![CDATA[let env = bundlerEnv { 106 name = "my-script-env"; 107 108 inherit ruby; 109 gemfile = ./Gemfile; 110 lockfile = ./Gemfile.lock; 111 gemset = ./gemset.nix; 112}; 113 114in stdenv.mkDerivation { 115 name = "my-script"; 116 buildInputs = [ env.wrappedRuby ]; 117 script = ./my-script.rb; 118 buildCommand = '' 119 install -D -m755 $script $out/bin/my-script 120 patchShebangs $out/bin/my-script 121 ''; 122}]]> 123</programlisting> 124</section>