Nix ruby: Difference between revisions
From wikinotes
(2 intermediate revisions by the same user not shown) | |||
Line 45: | Line 45: | ||
<source lang="ruby"> | <source lang="ruby"> | ||
source 'https://rubygems.org' | source 'https://rubygems.org' | ||
gem ' | gem 'graphql' | ||
gem ' | gem 'pry' | ||
</source> | </source> | ||
}} | }} | ||
Line 52: | Line 52: | ||
{{ expand | {{ expand | ||
| <code>${package}/default.nix</code> | | <code>${package}/default.nix</code> | ||
| | | | ||
< | |||
<syntaxhighlight lang="nix"> | |||
# ${package}/default.nix | # ${package}/default.nix | ||
# | # | ||
Line 61: | Line 62: | ||
# nix-shell | # nix-shell | ||
# bundle install | # 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 = [ | buildInputs = [ | ||
pkgs. | pkgs.ruby | ||
pkgs.bundler | |||
pkgs. | |||
]; | ]; | ||
} | } | ||
</ | </syntaxhighlight> | ||
}} | }} | ||
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/bundler-env/default.nix bundler-env] configures the | * 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 | ||
Line 130: | Line 93: | ||
require 'solargraph' | require 'solargraph' | ||
</source> | </source> | ||
So far, I've been managing the initial setup with a [[make|Makefile]].<br> | |||
See [[ruby rails: project]] section on nix for a more complex build involving build overrides. | |||
</blockquote><!-- bundix (builtin) --> | </blockquote><!-- bundix (builtin) --> | ||
Latest revision as of 21:32, 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'So far, I've been managing the initial setup with a Makefile.
See ruby rails: project section on nix for a more complex build involving build overrides.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