Linked to incompatible libruby when trying to run ruby-lsp

I’m using devenv.sh to develop a Ruby app. I’m having trouble getting ruby-lsp to work though and get the following error when trying to run it. It seems like it’s picking up the wrong version.

❯ ruby-lsp
<internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require': linked to incompatible /nix/store/ccvbrdslj740g71a0dkqjc77akxxdyzp-ruby-3.3.5/lib/libruby.so.3.3 - /data/code/fabrik/.devenv/state/.bundle/ruby/3.3.0/gems/date-3.4.0/lib/date_core.so (LoadError)
	from <internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
	from /data/code/fabrik/.devenv/state/.bundle/ruby/3.3.0/gems/date-3.4.0/lib/date.rb:4:in `<top (required)>'
	from <internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
	from <internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
	from /nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/time.rb:4:in `<top (required)>'
	from <internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
	from <internal:/nix/store/mq2n86q6l60yx86xxs3vd7af6s09nf5n-ruby-3.3.0/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in `require'
	from /nix/store/a0azmrgjxkf70pxdnm3732mfj3rypxn0-ruby3.3-ruby-lsp-0.15.0/lib/ruby/gems/3.3.0/gems/ruby-lsp-0.15.0/lib/ruby_lsp/setup_bundler.rb:9:in `<top (required)>'
	from /nix/store/a0azmrgjxkf70pxdnm3732mfj3rypxn0-ruby3.3-ruby-lsp-0.15.0/lib/ruby/gems/3.3.0/gems/ruby-lsp-0.15.0/exe/ruby-lsp:56:in `require_relative'
	from /nix/store/a0azmrgjxkf70pxdnm3732mfj3rypxn0-ruby3.3-ruby-lsp-0.15.0/lib/ruby/gems/3.3.0/gems/ruby-lsp-0.15.0/exe/ruby-lsp:56:in `<top (required)>'
	from /nix/store/a0azmrgjxkf70pxdnm3732mfj3rypxn0-ruby3.3-ruby-lsp-0.15.0/bin/ruby-lsp:18:in `load'
	from /nix/store/a0azmrgjxkf70pxdnm3732mfj3rypxn0-ruby3.3-ruby-lsp-0.15.0/bin/ruby-lsp:18:in `<main>'

My devenv.nix:

{ pkgs, lib, config, inputs, ... }: {
  packages = with pkgs; [
    git
    pkg-config
    libyaml.dev
    openssl.dev
    sqlite
    rubyPackages_3_3.ruby-lsp
  ];

  enterShell = ''
    bundle check || bundle
  '';

  enterTest = ''
    rake
  '';

  languages.ruby = {
    enable = true;
    versionFile = ./.ruby-version;
  };

  processes.rails.exec = "bin/rails server";
}

.ruby-version is 3.3.5.

I’m running on NixOS unstable, my laptop is a bit out of date but the desktop is current and I get the same issue.

I also tried to override the ruby-lsp version (with .override { version = "0.22.0"; })) but it still seemed to pick up version 0.15.0 so I think that’s wrong as well but possibly unrelated.

Probably a shot in the dark, as I’m also new to devenv.sh but it looks like you’re running ruby-3.3.0 and linking to library for ruby-3.3.5.

I wanted to run latest ruby, and found the ruby package in nixos to be not quite up to date so I started using nixpkgs-ruby instead.

Here’s how I set it up for my devenvs

devenv.yaml

inputs:
  nixpkgs:
    url: github:nixos/nixpkgs/nixpkgs-unstable
  nixpkgs-ruby:
    url: github:bobvanderlinden/nixpkgs-ruby
    inputs:
      nixpkgs:
        follows: nixpkgs

devenv.nix

{ pkgs, nixpkgs-ruby, ... }:
{
  env.RUBY_YJIT_ENABLE = 1;

  languages.ruby.enable = true;
  languages.ruby.package = nixpkgs-ruby.packages.${pkgs.system}."ruby-3.3";
}

Which should pick up your .ruby-version and use a more up to date version of ruby.

Thanks! Funnily enough I already had nixpkgs-ruby setup in devenv.yaml but must have forgotten about it. This will just pick up the latest version of Ruby 3.3 which in my case was 3.3.6 but I made a tweak to the config to pick up the version specified in .ruby-version as follows:

    package = nixpkgs-ruby.packages.${pkgs.system}."ruby-${lib.removeSuffix "\n" (builtins.readFile ./.ruby-version)}";

Also, I don’t think the YJIT flag is needed as it’s the default in Ruby 3.3 (Ruby on Rails — YJIT enabled by default, Active Model improvements and much more).

Unfortunately this doesn’t actually solve my problem. ruby-lsp still tries to use Ruby 3.1.4 (at least on the laptop I’m using at the moment). I need to somehow get ruby-lsp to use the current version of Ruby…

Is there something like rubyPackages_3_3 but for nixpkgs-ruby?