Home Explore Blog Models CI



nixpkgs

4th chunk of `doc/languages-frameworks/ruby.section.md`
c8b6ece8b787f2de98d70c1f41b89731729731dd13c2f86300000001000009b2
    gemdir = ./.;
    gemConfig = pkgs.defaultGemConfig // {
      pg = attrs: {
        buildFlags = [
          "--with-pg-config=${pkgs."postgresql_${pg_version}".pg_config}/bin/pg_config"
        ];
      };
    };
  };
in
mkShell {
  buildInputs = [
    gems
    gems.wrappedRuby
  ];
}
```

And finally via overlays:

```nix
{
  pg_version ? "10",
}:
let
  pkgs = import <nixpkgs> {
    overlays = [
      (self: super: {
        defaultGemConfig = super.defaultGemConfig // {
          pg = attrs: {
            buildFlags = [
              "--with-pg-config=${pkgs."postgresql_${pg_version}".pg_config}/bin/pg_config"
            ];
          };
        };
      })
    ];
  };
in
pkgs.ruby.withPackages (ps: with ps; [ pg ])
```

Then we can get whichever postgresql version we desire and the `pg` gem will always reference it correctly:

```ShellSession
$ nix-shell --argstr pg_version 9_4 --run 'ruby -rpg -e "puts PG.library_version"'
90421

$ nix-shell --run 'ruby -rpg -e "puts PG.library_version"'
100007
```

Of course for this use-case one could also use overlays since the configuration for `pg` depends on the `postgresql` alias, but for demonstration purposes this has to suffice.

### Platform-specific gems {#ruby-platform-specif-gems}

Right now, bundix has some issues with pre-built, platform-specific gems: [bundix PR #68](https://github.com/nix-community/bundix/pull/68).
Until this is solved, you can tell bundler to not use platform-specific gems and instead build them from source each time:
- globally (will be set in `~/.config/.bundle/config`):
```shell
$ bundle config set force_ruby_platform true
```
- locally (will be set in `<project-root>/.bundle/config`):
```shell
$ bundle config set --local force_ruby_platform true
```

### Adding a gem to the default gemset {#adding-a-gem-to-the-default-gemset}

Now that you know how to get a working Ruby environment with Nix, it's time to go forward and start actually developing with Ruby. We will first have a look at how Ruby gems are packaged on Nix. Then, we will look at how you can use development mode with your code.

All gems in the standard set are automatically generated from a single `Gemfile`. The dependency resolution is done with `bundler` and makes it more likely that all gems are compatible with each other.

In order to add a new gem to nixpkgs, you can put it into the `/pkgs/development/ruby-modules/with-packages/Gemfile` and run `./maintainers/scripts/update-ruby-packages`.

Title: Nix Ruby: Custom Gem Builds, Platform Fixes, and Contributing Gems to Nixpkgs
Summary
This chunk concludes the demonstration of configuring Ruby gems in Nix, specifically showing how to customize the `pg` gem's build flags to link with different PostgreSQL versions using `bundlerEnv`'s `gemConfig` or Nix overlays. It then addresses a known issue with `bundix` and pre-built, platform-specific gems, providing a temporary solution by forcing `bundle` to build gems from source using `bundle config set force_ruby_platform true`. Finally, it introduces the process of adding new Ruby gems to the default `nixpkgs` gemset, explaining that gems are generated from a central `Gemfile` and can be added by modifying `/pkgs/development/ruby-modules/with-packages/Gemfile` and running the provided update script.