-
Notifications
You must be signed in to change notification settings - Fork 306
/
ruby.nix
112 lines (97 loc) · 3.5 KB
/
ruby.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{ pkgs, config, lib, ... }:
let
cfg = config.languages.ruby;
nixpkgs-ruby = config.lib.getInput {
name = "nixpkgs-ruby";
url = "github:bobvanderlinden/nixpkgs-ruby";
attribute = "languages.ruby.version or languages.ruby.versionFile";
follows = [ "nixpkgs" ];
};
in
{
options.languages.ruby = {
enable = lib.mkEnableOption "tools for Ruby development";
package = lib.mkOption {
type = lib.types.package;
default = pkgs.ruby_3_1;
defaultText = lib.literalExpression "pkgs.ruby_3_1";
description = "The Ruby package to use.";
};
version = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
The Ruby version to use.
This automatically sets the `languages.ruby.package` using [nixpkgs-ruby](https://github.com/bobvanderlinden/nixpkgs-ruby).
'';
example = "3.2.1";
};
versionFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
The .ruby-version file path to extract the Ruby version from.
This automatically sets the `languages.ruby.package` using [nixpkgs-ruby](https://github.com/bobvanderlinden/nixpkgs-ruby).
When the `.ruby-version` file exists in the same directory as the devenv configuration, you can use:
```nix
languages.ruby.versionFile = ./.ruby-version;
```
'';
example = lib.literalExpression ''
./ruby-version
'';
};
bundler = {
enable = lib.mkEnableOption "bundler";
package = lib.mkOption {
type = lib.types.package;
default = pkgs.bundler.override { ruby = cfg.package; };
defaultText = lib.literalExpression "pkgs.bundler.override { ruby = cfg.package; }";
description = "The bundler package to use.";
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = cfg.version == null || cfg.versionFile == null;
message = ''
`languages.ruby.version` and `languages.ruby.versionFile` are both set.
Only one of the two may be set. Remove one of the two options.
'';
}
];
# enable C tooling by default so native extensions can be built
languages.c.enable = lib.mkDefault true;
languages.ruby.bundler.enable = lib.mkDefault true;
languages.ruby.package =
let
packageFromVersion = lib.mkIf (cfg.version != null) (
nixpkgs-ruby.packages.${pkgs.stdenv.system}."ruby-${cfg.version}"
);
packageFromVersionFile = lib.mkIf (cfg.versionFile != null) (
nixpkgs-ruby.lib.packageFromRubyVersionFile {
file = cfg.versionFile;
system = pkgs.stdenv.system;
}
);
in
lib.mkMerge [
packageFromVersion
packageFromVersionFile
];
packages = lib.optional cfg.bundler.enable cfg.bundler.package ++ [
cfg.package
];
env.BUNDLE_PATH = config.env.DEVENV_STATE + "/.bundle";
env.GEM_HOME = "${config.env.BUNDLE_PATH}/${cfg.package.rubyEngine}/${cfg.package.version.libDir}";
enterShell =
let libdir = cfg.package.version.libDir;
in
''
export RUBYLIB="$DEVENV_PROFILE/${libdir}:$DEVENV_PROFILE/lib/ruby/site_ruby:$DEVENV_PROFILE/lib/ruby/site_ruby/${libdir}:$DEVENV_PROFILE/lib/ruby/site_ruby/${libdir}/${pkgs.stdenv.system}:''${RUBYLIB:-}"
export GEM_PATH="$GEM_HOME/gems:''${GEM_PATH:-}"
export PATH="$GEM_HOME/bin:$PATH"
'';
};
}