# Ruby

There currently is support to bundle applications that are packaged as Ruby gems. The utility "bundix" allows you to write a `Gemfile`, let bundler create a `Gemfile.lock`, and then convert this into a nix expression that contains all Gem dependencies automatically.

For example, to package sensu, we did:

    $ cd pkgs/servers/monitoring
    $ mkdir sensu
    $ cd sensu
    $ cat > Gemfile
    source 'https://rubygems.org'
    gem 'sensu'
    $ nix-shell -p bundler --command "bundler package --path /tmp/vendor/bundle"
    $ $(nix-build '<nixpkgs>' -A bundix)/bin/bundix
    $ cat > default.nix
    { lib, bundlerEnv, ruby }:

    bundlerEnv rec {
      name = "sensu-${version}";

      version = (import gemset).sensu.version;
      inherit ruby;
      gemfile = ./Gemfile;
      lockfile = ./Gemfile.lock;
      gemset = ./gemset.nix;

      meta = with lib; {
        description = "A monitoring framework that aims to be simple, malleable, and scalable";
        homepage    = http://sensuapp.org/;
        license     = with licenses; mit;
        maintainers = with maintainers; [ theuni ];
        platforms   = platforms.unix;
      };
    }

Please check in the `Gemfile`, `Gemfile.lock` and the `gemset.nix` so future updates can be run easily.

Resulting derivations also have two helpful items, `env` and `wrapper`. The first one allows one to quickly drop into `nix-shell` with the specified environment present. E.g. `nix-shell -A sensu.env` would give you an environment with Ruby preset so it has all the libraries necessary for `sensu` in its paths. The second one can be used to make derivations from custom Ruby scripts which have `Gemfile`s with their dependencies specified. It is a derivation with `ruby` wrapped so it can find all the needed dependencies. For example, to make a derivation `my-script` for a `my-script.rb` (which should be placed in `bin`) you should run `bundix` as specified above and then use `bundlerEnv` lile this:

    let env = bundlerEnv {
      name = "my-script-env";

      inherit ruby;
      gemfile = ./Gemfile;
      lockfile = ./Gemfile.lock;
      gemset = ./gemset.nix;
    };

    in stdenv.mkDerivation {
      name = "my-script";

      buildInputs = [ env.wrapper ];

      script = ./my-script.rb;

      buildCommand = ''
        mkdir -p $out/bin
        install -D -m755 $script $out/bin/my-script
        patchShebangs $out/bin/my-script
      '';
    }