Packaging/Ruby

From NixOS Wiki
Revision as of 16:35, 19 March 2018 by imported>Makefu (Makefu moved page Packaging Ruby to Packaging/Ruby)

Ruby projects are generally packaged with bundix. This guide will show how to package a ruby project

Building BeEF

$ git clone https://github.com/beefproject/beef/

Create shell.nix with all dependencies

we create a shell.nix in the project directory:

with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "env";
  buildInputs = [
    ruby.devEnv
    git
    sqlite
    libpcap
    postgresql
    libxml2
    libxslt
    pkgconfig
    bundix
  ];
}

Build Gemfile.lock and gemset.nix

$ nix-shell
# generate Gemfile.lock
$ bundle install
# generate gemset.nix
$ bundix

Build default.nix

This will be the package configuration:

{ stdenv, bundlerEnv, ruby }:
let
  # the magic which will include gemset.nix
  gems = bundlerEnv {
    name = "beef-env";
    inherit ruby;
    gemdir  = ./.;
  };
in stdenv.mkDerivation {
  name = "beef";
  src = ./.;
  buildInputs = [gems ruby];
  installPhase = ''
    mkdir -p $out/{bin,share/beef}
    cp -r * $out/share/beef
    bin=$out/bin/beef
# we are using bundle exec to start in the bundled environment
    cat > $bin <<EOF
#!/bin/sh -e
exec ${gems}/bin/bundle exec ${ruby}/bin/ruby $i "\$@"
EOF
    chmod +x $bin
  '';
}

Build the package

$ nix-build -E '((import <nixpkgs> {}).callPackage (import ./default.nix) { })' --keep-failed
....
do_sqlite3 build FAILED!

Our Problem is that do_sqlite3 wants to build native extensions and requires sqlite as dependency. There are two ways to solve this issue:

  1. add a global override to your nixpkgs
  2. set the gemConfig for the bundleEnv manually

Adding a global override for a gem

We edit: <nixpkgs/pkgs/development/ruby-modules/gem-config/default.nix:

{
   ...
   do_sqlite3 = attrs: {
     buildInputs = [ sqlite ];
   };
   ...
}


After this change we build the package again.

set the local gemConfig

TODO: also merge with the defaultGemConfig somehow