Search code examples
rubynix

How to set up Ruby on OS X with Nix instead of Homebrew?


I switched recently to the Nix package manager instead of Homebrew for OS X.

Usually, I install it with rbenv like:

brew install rbenv
rbenv install 2.2.0

I am asking myself now how does one do it with Nix? I've heard Nix itself is a good enough package manager, so maybe I can get rid of rbenv for handling my ruby version?

When I install ruby via Nix

nix-env -i ruby

Even when OS X recognises it:

$ which ruby
/Users/USERNAME/.nix-profile/bin/ruby

I am still getting a Permission denied error when trying to:

gem install sass

Would you still recommend rbenv for handling ruby installations? I know I could just chown the Nix folder, but I should own it anyway.


Solution

  • I've had similar experience using python with nix. Generally the approach when using nix is NOT to:

    • Install a language specific package manager for some version of a language (gem, pip, ...)
    • Use that language specific package manager to install libraries

    Instead the nix way (from what I've learned) to do things is:

    • Install all already packaged applications with nix
    • If you are developing an application, instead of installing the libraries your application depends on via nix (or gem), you define a nix expression that describes your applications dependencies and use it as a development environment. (I think the documentation should be improved in this matter.)
    • Generally don't install libraries on their own and manually refer to those in some third-party build process. (This works in some scenarios, as in compiled C/C++ libraries, where dependencies of the libraries are built into them via RPATHs. But this does not work well with e.g. python packages.) In the end what you really want is the application, not the library!

    You can find some documentation on how to set up such environments in the official nixpkgs documentation, mostly covering Haskell at the moment, but Ruby should be similar.