Nix ruby: Difference between revisions

From wikinotes
 
Line 45: Line 45:
<source lang="ruby">
<source lang="ruby">
source 'https://rubygems.org'
source 'https://rubygems.org'
gem 'pkg-config'
gem 'graphql'
gem 'rails', '~> 6.1'
gem 'pry'
</source>
</source>
}}
}}
Line 52: Line 52:
{{ expand
{{ expand
| <code>${package}/default.nix</code>
| <code>${package}/default.nix</code>
|  
|
<source lang="nix">
 
<syntaxhighlight lang="nix">
# ${package}/default.nix
# ${package}/default.nix
#
#
Line 61: Line 62:
#    nix-shell
#    nix-shell
#      bundle install
#      bundle install
#      bundle exec rails new --skip-git .
#      bundle exec pry
#
# 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
{ pkgs ? import <nixpkgs> {} }:
   package = "nix_rails";
   pkgs.stdenv.mkDerivation {
  version = "0.0.1";
    pname = "ruby-graphql-intro";
  gems = bundlerEnv {
    version = "0.0.1";
     name = "${package}-env";
     env = pkgs.bundlerEnv {
    inherit ruby;
       name = "ruby-graphql-intro";
    gemdir = ./.;
      ruby = pkgs.ruby;
    gemConfig = pkgs.defaultGemConfig // {
       gemdir = ./.;
       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 = [
     buildInputs = [
       pkgs.pkg-config
       pkgs.ruby
      pkgs.zlib
       pkgs.bundler
      pkgs.sqlite
       pkgs.libffi
      pkgs.yarn
      ruby
      gems
     ];
     ];
   }
   }
</source>
</syntaxhighlight>
 
}}
}}


* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/bundler-env/default.nix bundler-env] configures the environment, Gemfile, gemset.nix, etc.
* bundix creates gemset.nix from your Gemfile
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/bundler-env/default.nix bundler-env] configures your environment using the Gemfile, gemset.nix, etc.
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/gem-config/default.nix gem-config] allows you to override mkDerivation for each gem
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/gem-config/default.nix gem-config] allows you to override mkDerivation for each gem



Revision as of 21:24, 6 September 2021

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 'graphql'
gem 'pry'

${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 pry

{ pkgs ? import <nixpkgs> {} }:
  pkgs.stdenv.mkDerivation {
    pname = "ruby-graphql-intro";
    version = "0.0.1";
    env = pkgs.bundlerEnv {
      name = "ruby-graphql-intro";
      ruby = pkgs.ruby;
      gemdir = ./.;
    };
    buildInputs = [
      pkgs.ruby
      pkgs.bundler
    ];
  }


  • bundix creates gemset.nix from your Gemfile
  • bundler-env configures your environment using the 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