Ruby rails: project

From wikinotes
Revision as of 21:04, 19 September 2021 by Will (talk | contribs) (→‎Nix Environment)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

New Projects

Normal Project

# new project
rails new myproject
rails server
qutebrowser http://127.0.0.1:3000

# create view
rails generate controller Welcome index
qutebrowser http://127.0.0.1:3000/welcome#index

# create db
rails db:create

Isolated Ruby Environment

  • install ruby, chruby (and ensure ruby is visible to chruby)
  • install rails into that ruby version
# use 'gem' from desired ruby version
/opt/rubies/2.6.5/bin/gem install yarn
/opt/rubies/2.6.5/bin/gem install rails

# create project for version
rails new myproject \
  -r /opt/rubies/2.6.5/bin/ruby

Nix Environment

You may also be interested in nix ruby.

.envrc

use_nix

Gemfile

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

default.nix


When prompted, replace your original Gemfile.
It may be tempting to use a subdir, but I've had library issues when using the sub-bundle directory.

I highly recommend using nix-shell manually instead of direnv while test-building your environment.

# ${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
#    bundle exec rails s
#
# 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 
    ];
    #GEM_HOME="~/.gem";
  }


NOTE:

GemConfig lets you override build arguments, envvars, requirements etc (same params as stdenv.mkDerivation).

Anatomy

project/
    app/           # models, views, controllers, helpers, mailers, channels, jobs, assets
    bin/           # executables
    config/        # rails configuration
    db/            # database schema, migration files
    lib/           # utility modules (shared by, or useful to code in napp/)
    log/           # logfiles
    public/        # static-html, compiled assets
    storage/       # disk-service active-storage files.
    test/          # unittests, fixtures, etc
    tmp/           # cache, pidfiles etc
    vendor/        # vendored third party code

    config.ru      # `rack` configuration (related to http requests)
    Gemfile        # `bundler` gem requirements
    Gemfile.lock   # `bundler` installed gem versions
    package.json   # `npm` requirements
    Rakefile       # configures where cli-tasks are loaded from