Legacy ruby version

I have a legacy project with a ruby version that has long reached end of life.
To test locally I’d like to install ruby 2.2.4 and bundler 1.17.3.

I’ve read using-nix-to-manage-multiple-ruby-versions and setting-up-a-nix-environment-for-rails. From the first post I used the nix expression as a basis, here with my adjustments for the ruby version:

let  
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;

  ruby = pkgs.ruby_2_2_4;
  bundler = pkgs.bundler_1_17_3;
  rubygems = (pkgs.rubygems.override { ruby = ruby; });

in stdenv.mkDerivation rec {  
  name = "mortgages";
  buildInputs = [
    ruby
    bundler
    pkgs.libxml2
    pkgs.libxslt
    pkgs.zlib
    pkgs.bzip2
    pkgs.openssl
    pkgs.mysql
    pkgs.libmysql
    pkgs.imagemagickBig
    pkgs.pkgconfig
  ];

  shellHook = ''
    export PKG_CONFIG_PATH=${pkgs.libxml2}/lib/pkgconfig:${pkgs.libxslt}/lib/pkgconfig:${pkgs.zlib}/lib/pkgconfig:${pkgs.mysql}/lib/pkgconfig:${pkgs.imagemagickBig}/lib/pkgconfig
    export C_INCLUDE_PATH=${pkgs.libmysql}/include/mysql

    mkdir -p .nix-gems
    export GEM_HOME=$PWD/.nix-gems
    export GEM_PATH=$GEM_HOME
    export PATH=$GEM_HOME/bin:$PATH  '';

}

But when I run nix-shell default.nix --show-trace I get the output:

error: while evaluating the attribute 'buildInputs' of the derivation 'mortgages' at /path/to/project/default.nix:9:3:
while evaluating 'getOutput' at /nix/store/vifxvc1qnb4g672zp3nf9bm6jr2p631x-nixpkgs-19.09pre187222.002b853782e/nixpkgs/lib/attrsets.nix:464:23, called from undefined position:
while evaluating anonymous function at /nix/store/vifxvc1qnb4g672zp3nf9bm6jr2p631x-nixpkgs-19.09pre187222.002b853782e/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:142:17, called from undefined position:
attribute 'ruby_2_2_4' missing, at/path/to/project/default.nix:5:10

So my question is: what am I doing wrong? Do I have to target an older nixpkgs version in order to get my desired results?

That’s correct, you’re using the default “nixpkgs” channel on your system by using the <nixpkgs> import. You can get the older ruby by using something like this:

import (builtins.fetchTarball {
  name = "nixos-unstable-2018-09-12";
  url = https://github.com/nixos/nixpkgs/archive/ca2ba44cab47767c8127d1c8633e2b581644eb8f.tar.gz;
  # Hash obtained using `nix-prefetch-url --unpack <url>`
  sha256 = "1jg7g6cfpw8qvma0y19kwyp549k1qyf11a5sg6hvn6awvmkny47v";
}) {}

Looking at https://hydra.nixos.org/search?query=ruby_2_2 we never had a 2.2.4 version in nixpkgs, so you’ll have to use either 2.2.3 or 2.2.5.

There are other ways, like building it yourself by modifying the version/hash in nixpkgs, but I think 2.2.5 should be fully compatible with 2.2.4 so I’d instead recommend that first.

Getting the nixpkgs revision for the version you want is also easy by looking at Hydra (not sure which OS you’re using so I’ll just assume a x86_64-linux:
https://hydra.nixos.org/job/nixos/release-17.03/nixpkgs.ruby_2_2_5.x86_64-linux#tabs-status
Click on the last successful build and from there go to the “Inputs” tab, which shows you which version of nixpkgs it was built with.

It is also easy to mix two versions of nixpkgs for different packages, so you only have to take Ruby from this ancient checkout and the rest of the packages can be more recent by simply assigning it to a different variable than pkgs.

Hope this helps.

2 Likes

I’m sorry I haven’t answered earlier.

This looks good, even though I fear I’ll have to go through the manual process, because I really don’t want to change anything in this project. This has to be the reference implementation hence I’d rather replicate the dependencies as close as possible.

Thanks for the input!

I once also had this problem of needing various versions of Ruby. I made GitHub - bobvanderlinden/nixpkgs-ruby: A Nix repository with all Ruby versions being kept up-to-date automatically

I don’t use it anymore, but I can imagine that it still works if that version of Ruby is able to compile against your version of nixpkgs. The list of ruby versions is at least still being kept up to date by running GitHub - bobvanderlinden/nixpkgs-ruby-updater daily.

2 Likes