Nix ruby

From wikinotes
Revision as of 05:06, 27 March 2021 by Will (talk | contribs) (→‎bundix (builtin))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation

official nixpkgs ruby docs https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/ruby.section.md
official ruby docs https://nixos.org/manual/nixpkgs/stable/#sec-language-ruby
wiki docs https://nixos.wiki/wiki/Packaging/Ruby
bundix github https://github.com/nix-community/bundix
bundlerEnv src https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/bundler-env/default.nix

Tutorials

compiling native extensions in bundix https://stackoverflow.com/questions/37933375/how-to-build-a-ruby-gem-using-nix-that-has-native-extensions
nix rails performance https://www.tweag.io/blog/2020-06-25-eval-cache/

Package Providers

ad-hoc

# install ruby version with target gems
nix-shell -p "ruby_2_7.withPackages (ps: with ps; [ nokogiri pry ])"

bundix (builtin)

${package}/Gemfile

Just a regular Gemfile

source 'https://rubygems.org'
gem 'pkg-config'
gem 'rails', '~> 6.1'

${package}/default.nix

# ${package}/default.nix
#
# first run:
#    nix-shell -p bundler --run 'bundle lock'
#    nix-shell -p bundix --run 'bundix -m'
#    nix-shell
#      bundle install
#      bundle exec rails new --skip-git .
#
# gem updates:
#    nix-shell -p bundler --run 'bundle update'
#    
# use:
#    nix-shell
#    bundle exec pry
#    bundle exec rails c
#
# borked?
#    nix-collect-garbage
#
# don't do this:
#    nix-shell -p bundler --run 'bundle config set path ~/.gem'
{ 
  pkgs ? import <nixpkgs> {},
  lib ? pkgs.lib, 
  bundlerEnv ? pkgs.bundlerEnv,
  ruby ? pkgs.ruby
}:

let
  package = "nix_rails";
  version = "0.0.1";
  gems = bundlerEnv {
    name = "${package}-env";
    inherit ruby;
    gemdir = ./.;
    gemConfig = pkgs.defaultGemConfig // {
      nokogiri = attrs: {
        buildFlags = [ 
          "--with-zlib-lib=${pkgs.lib.makeLibraryPath [ pkgs.zlib ]}" 
          "--with-zlib-include=${pkgs.zlib.dev}/include" 
        ];
      };
      mimemagic = attrs: {
        FREEDESKTOP_MIME_TYPES_PATH="${pkgs.shared-mime-info}/share/mime/packages/freedesktop.org.xml";
        buildInputs = [ pkgs.shared-mime-info ];
      };
    };
  };

in 
  pkgs.stdenv.mkDerivation {
    name = "${package}-${version}";
    buildInputs = [
      pkgs.pkg-config
      pkgs.zlib
      pkgs.sqlite
      pkgs.libffi
      pkgs.yarn
      ruby
      gems 
    ];
  }
  • bundler-env configures the environment, Gemfile, gemset.nix, etc.
  • gem-config allows you to override mkDerivation for each gem

To interactively add additional dev packages, you can simply gem install them.

nix-shell
gem install solargraph
irb
require 'solargraph'

overridable bundix (builtin)

Keep the same default.nix,
we'll just inject some gems and packages specific to your dev environment.

${package}/shell.nix

# ${package}/shell.nix

{ pkgs ? import <nixpkgs> {} }:

let
  pkg = import ./default.nix {};

in
  pkg.overrideAttrs(
    old: {
      shellHook = 
        (if builtins.hasAttr "shellHook" pkg
          then builtins.getAttr "shellHook" pkg
          else "")
          + "\n" 
          + ''
          gem install pry
        '';
      buildInputs =
        (if builtins.hasAttr "buildInputs" pkg
          then builtins.getAttr "buildInputs" pkg
          else [])
          ++ [
            pkgs.ripgrep
            pkgs.vim
          ];
    }
  )

rails and other setups

See ruby rails: project