NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
16.51k stars 12.99k forks source link

Ruby on Rails on NixOS #45980

Open coretemp opened 5 years ago

coretemp commented 5 years ago

Issue description

There are a lot of outdated GitHub projects on getting rails (in particular the executable named rails) to work on NixOS.

For instance https://github.com/ebzzry/rails-nix.

However, all of these methods seem to have bitrotted. What is a way to get a simple "hello world" Ruby on Rails running on NixOS?

To make this more actionable, the manual should document how to do so.

Steps to reproduce

Google for "nixos ruby on rails", observe results, test results and see they all fail.

Technical details

18.03 or newer

siers commented 5 years ago

Did you trying using bundix?

alyssais commented 5 years ago

Working on an existing Rails app is quite straightforward (run bundix, and then make a trivial shell.nix that uses bundlerEnv as documented in the manual). However, generating a new one presents a bit of a bootstrapping problem, because how do you get the rails executable in the first place. The trick is to set up a nix-shell environment for a Gemfile containing just Rails, and then use that to generate the application:

$ mkdir rails_app
$ cd rails_app
$ cat > Gemfile
source "https://rubygems.org"
gem "rails"
$ nix-shell -p bundix --run 'bundix -l'
$ cat > shell.nix
with import <nixpkgs> {};

(bundlerEnv {
  name = "rails_app";
  gemdir = ./.;
}).env
$ nix-shell --run 'bundle exec rails new . --force --skip-bundle'
$ nix-shell -p bundix --run 'bundix -l'

At this point, running nix-shell should give you a Nix shell with all the dependencies of your Rails application available, and eg bundle exec rails server should work.

coretemp commented 5 years ago

Great, your instructions work.

It could be automated a bit further and then added to the manual as far as I am concerned.

zetavg commented 5 years ago

I'd just created a "hello world" Rails app, which uses the buildRailsApp defined here.

That buildRailsApp isn't complete and may lack lots of stuff such as Active Record support, but I'm interested in fulfilling those features if someone needs it, or on the next time I've started working on a Rails project.

waiting-for-dev commented 4 years ago

Just be careful with given rails command:

bundle exec rails new . --force --skip-bundle

It will fail while attempting rails webpacker:install automatically.

A better workflow is executing:

bundle exec rails new . --force --skip-bundle --skip-webpack-install

Then, after last bundix -l, run rails webpacker:install yourself.

Another problem is if you don't have yarn installed or you execute with nix-shell --pure. It is safer and more self-contained if you add yarn to your nix.shell:

  with import <nixpkgs> {};                                                                                                      

  let                                                                                                                            
    env = bundlerEnv {                                                                                                           
      name = "rails_app";                                                                                                        
      gemdir = ./.;                                                                                                              
    };                                                                                                                           
  in                                                                                                                             
    stdenv.mkDerivation {                                                                                                        
      name = "rails_app";                                                                                                        
      buildInputs = [env yarn];                                                                                                  
    }
stale[bot] commented 4 years ago

Thank you for your contributions. This has been automatically marked as stale because it has had no activity for 180 days. If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity. Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on irc.freenode.net.
waiting-for-dev commented 4 years ago

Still important to me

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info