-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathflake.nix
225 lines (195 loc) · 6.78 KB
/
flake.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{
description = "Code coverage tool for Bash";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
devshell.url = "github:numtide/devshell";
devshell.inputs.nixpkgs.follows = "nixpkgs";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs @ {
self,
devshell,
flake-parts,
treefmt-nix,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} ({lib, ...}: {
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
imports = [
devshell.flakeModule
flake-parts.flakeModules.easyOverlay
treefmt-nix.flakeModule
];
perSystem = {
config,
pkgs,
self',
system,
...
}: let
newestRuby = let
rubies = lib.filterAttrs (name: value: let
hasRubyEngine = builtins.tryEval (value ? rubyEngine);
in
lib.hasPrefix "ruby" name && hasRubyEngine.success && hasRubyEngine.value)
pkgs;
recentRubies =
lib.foldlAttrs (
acc: name: value: let
v = toString value.version;
in
acc ++ (lib.optional (lib.versionAtLeast v "3" && lib.versionOlder v "4") {inherit name value;})
) []
rubies;
sortedRubies = lib.sort (x: y: lib.versionAtLeast (toString x.value.version) (toString y.value.version)) recentRubies;
in
if (lib.length sortedRubies) > 0
then lib.head sortedRubies
else {
name = "ruby";
value = pkgs.ruby;
};
ruby = newestRuby.value;
rubyName = newestRuby.name;
in {
# `nix run '.#devshell' -- update-deps`, etc.
apps.devshell = self'.devShells.default.flakeApp;
checks = {
inherit (config.packages) bashcov;
};
devshells.default = {
imports = [
# Ruby support, including prerequisites for compiling native
# extensions.
"${inputs.devshell}/extra/language/ruby.nix"
];
commands = [
{
name = "fmt";
category = "linting";
help = "Format the Nix code in this project";
command = ''
exec ${config.treefmt.build.wrapper}/bin/treefmt "$@"
'';
}
{
package = ruby;
category = "development";
}
{
package = pkgs.bundix;
category = "maintenance";
}
{
name = "update-deps";
category = "maintenance";
help = "Update dependencies with Bundler and Bundix";
command = ''
export NIX_PATH="nixpkgs=${toString pkgs.path}''${NIX_PATH:+:''${NIX_PATH}}"
lockfile="''${PRJ_ROOT:-.}/Gemfile.nix.lock"
bundle lock --lockfile "$lockfile"
exec bundix --ruby ${lib.escapeShellArg rubyName} --lockfile="$lockfile" "$@"
'';
}
{
name = "update-deps-conservative";
category = "maintenance";
help = "Update dependencies with Bundler and Bundix (if necessary)";
# If `nix build` succeeds, then presume that the lockfiles do not
# need to be updated. If `nix build` fails after updating the
# lockfile, out-of-date dependencies weren't to blame for the
# build failure.
command = ''
export NIX_PATH="nixpkgs=${toString pkgs.path}''${NIX_PATH:+:''${NIX_PATH}}"
nix build && exit
if update-deps; then
nix build || {
rc="$?"
git restore "''${PRJ_ROOT}/Gemfile.nix.lock" "''${PRJ_ROOT}/gemset.nix" || rc="$?"
exit "$rc"
}
fi
'';
}
];
};
overlayAttrs = {
inherit (config.packages) bashcov;
};
packages = {
bashcov = pkgs.callPackage ({
lib,
bash,
buildRubyGem,
makeWrapper,
ruby,
gems,
doCheck ? true, # Whether to run RSpec and Cucumber Rake tasks.
}: let
inherit (gems'.gems.bashcov) version;
gems' = gems.override {inherit ruby;};
gemName = gems'.gems.bashcov.name;
in
assert lib.assertMsg (gems'.gems ? bashcov) "bashcov: gem set must contain `bashcov`";
buildRubyGem {
inherit doCheck gemName version ruby;
name = "${gemName}-${version}";
src = self;
nativeBuildInputs = [bash makeWrapper];
propagatedBuildInputs = [gems'];
checkPhase = ''
${config.packages.gems.wrappedRuby}/bin/rake cucumber spec
'';
# Replace shebangs like "#!/usr/bin/env bash" with Nix store
# paths. Note that we need to do this for `./bin/bashcov`
# itself, as otherwise running `bashcov` from Cucumber features
# during the check phase will fail because `/usr/bin/env` does
# not exist in the build sandbox.
doPatch = true;
postPatch = ''
patchShebangs ./bin ./features ./spec
'';
# Ensure that Bashcov can find Bash, but make this Bash
# low-precedence by placing its /bin/ at the end of PATH.
postInstall = ''
wrapProgram $out/bin/bashcov --suffix PATH : ${bash}/bin
'';
}) {
inherit ruby;
inherit (config.packages) gems;
};
default = config.packages.bashcov;
gems = pkgs.callPackage ({
bundlerEnv,
ruby,
}:
bundlerEnv {
inherit ruby;
pname = "bashcov";
gemdir = self;
lockfile = ./Gemfile.nix.lock;
version = "3.0.2";
postBuild = ''
for gem in $out/${ruby.gemPath}/bundler/gems/*; do
ln -sfrT "$gem" $out/${ruby.gemPath}/gems/"''${gem##*/}"
done
'';
}) {inherit ruby;};
};
treefmt = {
programs.alejandra.enable = true;
flakeFormatter = true;
projectRootFile = "flake.nix";
};
};
});
}