Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
718 lines (637 loc) · 28.8 KB
/
Copy pathflake.nix
File metadata and controls
718 lines (637 loc) · 28.8 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# In rails-builder flake.nix
{
description = "Generic Rails builder flake";
inputs = {
# Non-`github:` URL forms, deliberately — see the rationale comment in
# templates/universal/flake.nix. These inputs are re-resolved fresh by
# every CI build (the generated app flake consumes this repo as an
# unlocked input), and the `github:` scheme goes through api.github.com,
# which is anonymous-rate-limited to death on CodeBuild's shared egress
# IPs. Archive tarballs (codeload) avoid the REST API entirely.
# nixpkgs: rev-pinned archive tarball (immutable URL, served by
# codeload, no REST API). A branch tarball would lock as
# mutable-URL+narHash and hard-fail with a NAR mismatch in every
# locked consumer as soon as the branch advances; a git+https nixpkgs
# would mean multi-GB clones in CI. Bumping nixpkgs = edit this rev.
nixpkgs.url = "https://github.com/NixOS/nixpkgs/archive/d233902339c02a9c334e7e593de68855ad26c4cb.tar.gz";
# Small repos: git+https (git smart-HTTP protocol — no REST API).
# These lock by rev exactly like `github:` did, and stay updatable
# via `nix flake update`.
nixpkgs-ruby.url = "git+https://github.com/bobvanderlinden/nixpkgs-ruby?ref=master";
nixpkgs-ruby.inputs.nixpkgs.follows = "nixpkgs";
# Custom bundix fork with fixes
bundix-src.url = "git+https://github.com/glenndavy/bundix?ref=master";
bundix-src.flake = false;
# nixpkgs-ruby's transitive inputs, redirected onto non-API pins we
# control. Without these follows, CI's lock-less resolution fetches
# them via their upstream `github:` refs — through the REST API.
flake-compat.url = "git+https://github.com/edolstra/flake-compat?ref=master";
flake-compat.flake = false;
flake-utils.url = "git+https://github.com/numtide/flake-utils?ref=main";
flake-utils.inputs.systems.follows = "systems";
systems.url = "git+https://github.com/nix-systems/default?ref=main";
nixpkgs-ruby.inputs.flake-compat.follows = "flake-compat";
nixpkgs-ruby.inputs.flake-utils.follows = "flake-utils";
};
outputs = {
self,
nixpkgs,
nixpkgs-ruby,
bundix-src,
...
}: let
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
# Simple version for compatibility - can be overridden with --impure for git info
version = "3.17.115";
forAllSystems = nixpkgs.lib.genAttrs systems;
overlays = [nixpkgs-ruby.overlays.default];
mkPkgsForSystem = system: import nixpkgs {inherit system overlays;};
# Build custom bundix from glenndavy/bundix.
#
# Bundix's bin/bundix has `#!/usr/bin/env ruby` and its makeWrapper-built
# wrapper sets GEM_PATH to a pinned bundler 2.7+ — but neither pins the
# Ruby. Without one in PATH, the shebang finds whatever Ruby the caller
# has first (in generate-dependencies, that's the project's Ruby, which
# for older Rails apps can be 2.5 / 2.6). Bundler 2.7+ uses Ruby 3.0+
# APIs (Module#method_defined? with two args, etc.) and crashes at
# require-time on the older interpreter.
#
# Pin bundix to a modern Ruby + matching bundler so it runs reliably
# regardless of the project's Ruby version. See issue #1.
mkBundixForSystem = system: let
pkgs = mkPkgsForSystem system;
bundixRuby = pkgs.ruby_3_3;
bundixBundler = pkgs.bundler.override { ruby = bundixRuby; };
base = pkgs.callPackage bundix-src {
ruby = bundixRuby;
bundler = bundixBundler;
};
in
pkgs.symlinkJoin {
name = "bundix-pinned-ruby";
paths = [ base ];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/bundix \
--prefix PATH : "${bundixRuby}/bin"
'';
};
mkLibForSystem = system: let
pkgs = mkPkgsForSystem system;
mkRailsBuild = import ./imports/make-rails-build.nix {inherit pkgs;};
mkRailsNixBuild = import ./imports/make-rails-nix-build.nix {inherit pkgs;};
versionDetection = import ./imports/detect-versions.nix;
# Custom bundlerEnv that supports vendor/cache gems.
#
# The bundler fallback (used when gemset.nix has no `bundler` entry)
# comes from callPackage's `bundler` arg. Without an override that
# defaults to `pkgs.bundler` (currently 2.7+), which uses Ruby 3.0+
# APIs and crashes when loaded under Ruby <3.0 — fatal for older
# Rails apps. Two flavours exposed:
#
# customBundlerEnv — back-compat, no bundler override (current
# nixpkgs default; fine for Ruby 3+ apps)
# mkCustomBundlerEnv — accepts a bundler override; callers should
# prefer this and pass their version-matched
# bundlerPackage so it works across the
# Rails 5-8 / Ruby 2.5-3.x range.
customBundlerEnv = pkgs.callPackage ./imports/bundler-env {};
mkCustomBundlerEnv = { bundler ? null }:
pkgs.callPackage ./imports/bundler-env
(if bundler != null then { inherit bundler; } else {});
# High-level helper to build Rails packages with all correct configuration
# Usage: rails-builder.lib.${system}.mkRailsPackage { inherit pkgs; src = ./.; }
# Note: Automatically applies nixpkgs-ruby overlay to pkgs for ruby version packages
mkRailsPackage = args:
import ./imports/mk-rails-package.nix (args
// {
nixpkgsRubyOverlay = nixpkgs-ruby.overlays.default;
railsBuilderVersion = version;
});
# Minimal break-glass devShell — see imports/make-safe-shell.nix.
# Use when your normal shells (with-bundix / with-bundler / default)
# won't enter because a SHA, gemset.nix, or tailwindcss hash is broken.
mkSafeShell = args:
import ./imports/make-safe-shell.nix args;
in {
inherit mkRailsBuild mkRailsNixBuild customBundlerEnv mkCustomBundlerEnv mkRailsPackage mkSafeShell;
inherit (versionDetection) detectRubyVersion detectBundlerVersion detectNodeVersion detectTailwindVersion;
version = version;
};
# Create tests for each system
mkTestsForSystem = system: let
pkgs = mkPkgsForSystem system;
lib = mkLibForSystem system;
versionDetection = import ./imports/detect-versions.nix;
detectFramework = import ./imports/detect-framework.nix;
# Fixtures as plain paths (required for builtins.readFile/pathExists)
railsFixture = ./tests/fixtures/rails-app;
sinatraFixture = ./tests/fixtures/sinatra-app;
rackFixture = ./tests/fixtures/rack-app;
plainRubyFixture = ./tests/fixtures/plain-ruby;
hanami2Fixture = ./tests/fixtures/hanami-2-app;
hanami1Fixture = ./tests/fixtures/hanami-1-app;
# Test version detection against fixtures
testVersionDetection = let
railsRuby = versionDetection.detectRubyVersion {src = railsFixture;};
railsBundler = versionDetection.detectBundlerVersion {src = railsFixture;};
railsTailwind = versionDetection.detectTailwindVersion {src = railsFixture;};
plainRuby = versionDetection.detectRubyVersion {src = plainRubyFixture;};
plainBundler = versionDetection.detectBundlerVersion {src = plainRubyFixture;};
plainTailwind = versionDetection.detectTailwindVersion {src = plainRubyFixture;};
in
pkgs.stdenv.mkDerivation {
name = "test-version-detection";
dontUnpack = true;
buildPhase = ''
echo "Testing version detection..."
test "${railsRuby}" = "3.3.0" || { echo "FAIL: rails ruby expected 3.3.0, got ${railsRuby}"; exit 1; }
echo "ok - Rails ruby version: ${railsRuby}"
test "${railsBundler}" = "2.5.22" || { echo "FAIL: rails bundler expected 2.5.22, got ${railsBundler}"; exit 1; }
echo "ok - Rails bundler version: ${railsBundler}"
test "${railsTailwind}" = "4.1.18" || { echo "FAIL: rails tailwind expected 4.1.18, got ${railsTailwind}"; exit 1; }
echo "ok - Rails tailwind version: ${railsTailwind}"
test "${plainRuby}" = "3.1.0" || { echo "FAIL: plain ruby expected 3.1.0, got ${plainRuby}"; exit 1; }
echo "ok - Plain ruby version: ${plainRuby}"
test "${plainBundler}" = "2.4.0" || { echo "FAIL: plain bundler expected 2.4.0, got ${plainBundler}"; exit 1; }
echo "ok - Plain bundler version: ${plainBundler}"
test "${builtins.toJSON plainTailwind}" = "null" || { echo "FAIL: plain tailwind expected null"; exit 1; }
echo "ok - Plain tailwind version: null"
'';
installPhase = ''
mkdir -p $out
echo "Version detection tests passed" > $out/result
'';
};
# Test framework and dependency detection against fixtures
testFrameworkDetection = let
railsInfo = detectFramework {src = railsFixture;};
sinatraInfo = detectFramework {src = sinatraFixture;};
rackInfo = detectFramework {src = rackFixture;};
plainInfo = detectFramework {src = plainRubyFixture;};
hanami2Info = detectFramework {src = hanami2Fixture;};
hanami1Info = detectFramework {src = hanami1Fixture;};
detectAppName = (import ./imports/detect-app-name.nix).detectAppName;
hanami2AppName = detectAppName { src = hanami2Fixture; framework = "hanami"; };
in
pkgs.stdenv.mkDerivation {
name = "test-framework-detection";
dontUnpack = true;
buildPhase = ''
echo "Testing framework detection..."
test "${railsInfo.framework}" = "rails" || { echo "FAIL: expected rails, got ${railsInfo.framework}"; exit 1; }
echo "ok - Rails framework detected"
test "${builtins.toJSON railsInfo.needsPostgresql}" = "true" || { echo "FAIL: rails should need postgresql"; exit 1; }
echo "ok - Rails needs PostgreSQL"
test "${builtins.toJSON railsInfo.needsRedis}" = "true" || { echo "FAIL: rails should need redis"; exit 1; }
echo "ok - Rails needs Redis"
test "${builtins.toJSON railsInfo.needsTailwindcss}" = "true" || { echo "FAIL: rails should need tailwindcss"; exit 1; }
echo "ok - Rails needs Tailwind CSS"
test "${sinatraInfo.framework}" = "sinatra" || { echo "FAIL: expected sinatra, got ${sinatraInfo.framework}"; exit 1; }
echo "ok - Sinatra framework detected"
test "${builtins.toJSON sinatraInfo.needsSqlite}" = "true" || { echo "FAIL: sinatra should need sqlite"; exit 1; }
echo "ok - Sinatra needs SQLite"
test "${rackInfo.framework}" = "rack" || { echo "FAIL: expected rack, got ${rackInfo.framework}"; exit 1; }
echo "ok - Rack framework detected"
test "${plainInfo.framework}" = "ruby" || { echo "FAIL: expected ruby, got ${plainInfo.framework}"; exit 1; }
echo "ok - Plain Ruby detected"
test "${hanami2Info.framework}" = "hanami" || { echo "FAIL: expected hanami, got ${hanami2Info.framework}"; exit 1; }
echo "ok - Hanami 2.x framework detected"
test "${builtins.toJSON hanami2Info.hanamiVersion}" = "2" || { echo "FAIL: hanami 2 fixture should yield hanamiVersion=2, got ${builtins.toJSON hanami2Info.hanamiVersion}"; exit 1; }
echo "ok - Hanami 2.x hanamiVersion=2"
test "${builtins.toJSON hanami2Info.needsPostgresql}" = "true" || { echo "FAIL: hanami 2 fixture should need postgresql"; exit 1; }
echo "ok - Hanami 2.x needs PostgreSQL"
test "${hanami2AppName}" = "my-hanami-app" || { echo "FAIL: hanami 2 app name expected 'my-hanami-app', got '${hanami2AppName}'"; exit 1; }
echo "ok - Hanami 2.x app name parsed from config/app.rb"
test "${hanami1Info.framework}" = "hanami" || { echo "FAIL: expected hanami, got ${hanami1Info.framework}"; exit 1; }
echo "ok - Hanami 1.x framework detected"
test "${builtins.toJSON hanami1Info.hanamiVersion}" = "1" || { echo "FAIL: hanami 1 fixture should yield hanamiVersion=1, got ${builtins.toJSON hanami1Info.hanamiVersion}"; exit 1; }
echo "ok - Hanami 1.x hanamiVersion=1"
test "${builtins.toJSON hanami1Info.needsSqlite}" = "true" || { echo "FAIL: hanami 1 fixture should need sqlite"; exit 1; }
echo "ok - Hanami 1.x needs SQLite"
'';
installPhase = ''
mkdir -p $out
echo "Framework detection tests passed" > $out/result
'';
};
# Test template is valid and has required structure
testTemplateEval = pkgs.stdenv.mkDerivation {
name = "test-template-eval";
src = ./.;
buildPhase = ''
echo "Testing template validity..."
if [ ! -f templates/universal/flake.nix ]; then
echo "FAIL: universal template missing"
exit 1
fi
echo "ok - Universal template exists"
if grep -q 'description' templates/universal/flake.nix && \
grep -q 'inputs' templates/universal/flake.nix && \
grep -q 'outputs' templates/universal/flake.nix; then
echo "ok - Template has required structure (description, inputs, outputs)"
else
echo "FAIL: template missing required structure"
exit 1
fi
'';
installPhase = ''
mkdir -p $out
echo "Template evaluation tests passed" > $out/result
'';
};
# Test mkRailsPackage evaluation (evaluation-only, no build)
testMkRailsPackage = let
railsBuild = lib.mkRailsPackage {
inherit pkgs;
src = railsFixture;
};
in
pkgs.stdenv.mkDerivation {
name = "test-mk-rails-package";
dontUnpack = true;
buildPhase = ''
echo "Testing mkRailsPackage evaluation..."
test "${railsBuild.detected.rubyVersion}" = "3.3.0" || { echo "FAIL: rubyVersion expected 3.3.0"; exit 1; }
echo "ok - detected.rubyVersion = 3.3.0"
test "${railsBuild.detected.bundlerVersion}" = "2.5.22" || { echo "FAIL: bundlerVersion expected 2.5.22"; exit 1; }
echo "ok - detected.bundlerVersion = 2.5.22"
test "${railsBuild.detected.tailwindVersion}" = "4.1.18" || { echo "FAIL: tailwindVersion expected 4.1.18"; exit 1; }
echo "ok - detected.tailwindVersion = 4.1.18"
test "${railsBuild.detected.framework}" = "rails" || { echo "FAIL: framework expected rails"; exit 1; }
echo "ok - detected.framework = rails"
test "${builtins.toJSON railsBuild.detected.needsPostgresql}" = "true" || { echo "FAIL: needsPostgresql expected true"; exit 1; }
echo "ok - detected.needsPostgresql = true"
test "${builtins.toJSON railsBuild.detected.needsRedis}" = "true" || { echo "FAIL: needsRedis expected true"; exit 1; }
echo "ok - detected.needsRedis = true"
test "${builtins.toJSON (builtins.hasAttr "app" railsBuild)}" = "true" || { echo "FAIL: missing app attr"; exit 1; }
echo "ok - railsBuild has 'app' attribute"
test "${builtins.toJSON (builtins.hasAttr "detected" railsBuild)}" = "true" || { echo "FAIL: missing detected attr"; exit 1; }
echo "ok - railsBuild has 'detected' attribute"
test "${builtins.toJSON (builtins.hasAttr "gems" railsBuild)}" = "true" || { echo "FAIL: missing gems attr"; exit 1; }
echo "ok - railsBuild has 'gems' attribute"
test "${builtins.toJSON (builtins.hasAttr "rubyPackage" railsBuild)}" = "true" || { echo "FAIL: missing rubyPackage attr"; exit 1; }
echo "ok - railsBuild has 'rubyPackage' attribute"
test "${builtins.toJSON (builtins.hasAttr "bundlerPackage" railsBuild)}" = "true" || { echo "FAIL: missing bundlerPackage attr"; exit 1; }
echo "ok - railsBuild has 'bundlerPackage' attribute"
'';
installPhase = ''
mkdir -p $out
echo "mkRailsPackage evaluation tests passed" > $out/result
'';
};
# Combined test runner
runAllTests = pkgs.stdenv.mkDerivation {
name = "run-all-tests";
dontUnpack = true;
buildInputs = [
testVersionDetection
testFrameworkDetection
testTemplateEval
testMkRailsPackage
];
buildPhase = ''
echo "Running all tests for ${system}..."
echo "1. Version detection tests..."
cat ${testVersionDetection}/result
echo "2. Framework detection tests..."
cat ${testFrameworkDetection}/result
echo "3. Template evaluation tests..."
cat ${testTemplateEval}/result
echo "4. mkRailsPackage evaluation tests..."
cat ${testMkRailsPackage}/result
echo "All tests completed successfully!"
'';
installPhase = ''
mkdir -p $out
echo "All tests passed for ${system}" > $out/result
'';
};
in {
inherit testVersionDetection testFrameworkDetection testTemplateEval testMkRailsPackage runAllTests;
};
in {
# Export version at top level for easy access from templates
inherit version;
lib = forAllSystems mkLibForSystem;
# Export custom bundix package for use by template
packages = forAllSystems (system: {
bundix = mkBundixForSystem system;
});
# Add test outputs
checks = forAllSystems mkTestsForSystem;
# Apps
apps = forAllSystems (system: let
pkgs = mkPkgsForSystem system;
customBundix = mkBundixForSystem system;
fix-gemset-sha-script = pkgs.writeShellScriptBin "fix-gemset-sha" (import ./imports/fix-gemset-sha.nix {inherit pkgs;});
generate-gemset-for-script = pkgs.writeShellScriptBin "generate-gemset-for" (import ./imports/generate-gemset-for.nix {
inherit pkgs;
bundixPackage = customBundix;
defaultRubyPackage = pkgs.ruby;
});
in {
flakeVersion = {
type = "app";
program = "${pkgs.writeShellScript "show-version" ''
echo 'Flake Version: ${version}'
''}";
};
# Fix SHA mismatches in gemset.nix
# Usage: nix run github:glenndavy/rails-builder#fix-gemset-sha
fix-gemset-sha = {
type = "app";
program = "${fix-gemset-sha-script}/bin/fix-gemset-sha";
};
# Generate gemset.nix for an external app source (orchestrator pattern)
# Usage: nix run github:glenndavy/rails-builder#generate-gemset-for -- /path/to/app
# Usage: nix run github:glenndavy/rails-builder#generate-gemset-for -- /path/to/app -o ./apps/my-app/gemset.nix
generate-gemset-for = {
type = "app";
program = "${generate-gemset-for-script}/bin/generate-gemset-for";
};
});
templates = {
# 🚀 Universal Template (single source of truth)
universal = {
path = ./templates/universal;
description = "Universal Ruby application template with smart dependency detection (Rails, Hanami, Sinatra, Rack, Ruby)";
};
# 🎯 Framework-specific aliases (all point to universal template)
rails = {
path = ./templates/universal;
description = "Rails application template with smart dependency detection";
};
hanami = {
path = ./templates/universal;
description = "Hanami application template with smart dependency detection";
};
sinatra = {
path = ./templates/universal;
description = "Sinatra application template with smart dependency detection";
};
rack = {
path = ./templates/universal;
description = "Rack application template with smart dependency detection";
};
ruby = {
path = ./templates/universal;
description = "Generic Ruby application template with framework auto-detection";
};
# 📦 Versioned templates for cache-busting
universal-v3-0-0 = {
path = ./templates/universal;
description = "Universal template v3.0.0 with enhanced detection - versioned for cache-busting";
};
rails-v3-0-0 = {
path = ./templates/universal;
description = "Rails template v3.0.0 with enhanced detection - versioned for cache-busting";
};
# 🔄 Legacy compatibility (deprecated but functional)
ruby-fixed = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal' or 'rails' instead - same functionality";
};
ruby-v2-2-3 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
ruby-v2-2-9 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
ruby-v2-2-8 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
ruby-v2-2-7 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
ruby-v2-2-5 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
ruby-v2-2-6 = {
path = ./templates/universal;
description = "[DEPRECATED] Use 'universal-v3-0-0' instead - same functionality with improvements";
};
};
# DevShells for direct access (useful for CI/CD and quick bootstrapping)
# Usage: nix develop --impure github:glenndavy/rails-builder (auto-detects from CWD)
# Usage: nix develop github:glenndavy/rails-builder (uses defaults)
# Usage: nix develop github:glenndavy/rails-builder#with-bundix-bootstrap
devShells = forAllSystems (system: let
pkgs = mkPkgsForSystem system;
versionDetection = import ./imports/detect-versions.nix;
detectFramework = import ./imports/detect-framework.nix;
customBundix = mkBundixForSystem system;
# Default Ruby for bootstrap shell (no project detection needed)
defaultRubyVersion = "3.3.0";
defaultRubyPackage = pkgs."ruby-${defaultRubyVersion}";
# Impure CWD detection for the default devShell
# builtins.getEnv returns "" in pure mode, so this is safe
pwd = builtins.getEnv "PWD";
isImpure = pwd != "";
cwdPath =
if isImpure
then /. + pwd
else null;
# Detect Ruby version from CWD (impure) or fall back to default
detectedRubyVersion = let
result =
if isImpure
then builtins.tryEval (versionDetection.detectRubyVersion {src = cwdPath;})
else {
success = false;
value = null;
};
in
if result.success
then result.value
else null;
rubyVersion =
if detectedRubyVersion != null
then detectedRubyVersion
else defaultRubyVersion;
rubyPackage = pkgs."ruby-${rubyVersion}";
# Detect bundler version from CWD
detectedBundlerVersion = let
result =
if isImpure
then builtins.tryEval (versionDetection.detectBundlerVersion {src = cwdPath;})
else {
success = false;
value = null;
};
in
if result.success
then result.value
else null;
# Detect framework and dependencies from CWD
detectedFramework = let
result =
if isImpure && cwdPath != null && builtins.pathExists (cwdPath + "/Gemfile.lock")
then builtins.tryEval (detectFramework {src = cwdPath;})
else {
success = false;
value = null;
};
in
if result.success
then result.value
else null;
hasFramework = detectedFramework != null;
# Build inputs based on detected dependencies
baseInputs = [
rubyPackage
pkgs.bundler
pkgs.git
pkgs.gnumake
pkgs.gcc
pkgs.pkg-config
pkgs.openssl
pkgs.libyaml
pkgs.zlib
pkgs.libffi
pkgs.readline
pkgs.ncurses
];
detectedInputs =
pkgs.lib.optionals hasFramework (
pkgs.lib.optional detectedFramework.needsPostgresql pkgs.postgresql
++ pkgs.lib.optional detectedFramework.needsSqlite pkgs.sqlite
++ pkgs.lib.optional detectedFramework.needsMysql pkgs.libmysqlclient
++ pkgs.lib.optional detectedFramework.needsLibVips pkgs.vips
++ pkgs.lib.optional detectedFramework.needsImageMagick pkgs.imagemagick
);
linuxInputs = pkgs.lib.optionals pkgs.stdenv.isLinux [
pkgs.libxml2
pkgs.libxslt
];
# Shell hook messages
rubySource =
if detectedRubyVersion != null
then "from .ruby-version"
else "default";
bundlerMsg =
if detectedBundlerVersion != null
then ''echo " Bundler: ${detectedBundlerVersion} (from Gemfile.lock)"''
else "";
frameworkMsg =
if hasFramework
then ''echo " Framework: ${detectedFramework.framework}"''
else "";
databaseMsgs =
(
if hasFramework && detectedFramework.needsPostgresql
then ''echo " Database: postgresql"''
else ""
)
+ (
if hasFramework && detectedFramework.needsMysql
then ''echo " Database: mysql"''
else ""
)
+ (
if hasFramework && detectedFramework.needsSqlite
then ''echo " Database: sqlite"''
else ""
);
cacheMsgs =
(
if hasFramework && detectedFramework.needsRedis
then ''echo " Cache: redis"''
else ""
)
+ (
if hasFramework && detectedFramework.needsMemcached
then ''echo " Cache: memcached"''
else ""
);
impureTip =
if !isImpure
then ''
echo ""
echo "Tip: Run with --impure to auto-detect from your project:"
echo " nix develop --impure github:glenndavy/rails-builder"
''
else "";
in {
# Bootstrap shell with bundix for generating gemset.nix
with-bundix-bootstrap = pkgs.mkShell {
name = "rails-builder-bootstrap";
buildInputs =
[
defaultRubyPackage
customBundix
pkgs.bundler
pkgs.git
pkgs.gnumake
pkgs.gcc
pkgs.pkg-config
pkgs.openssl
pkgs.libyaml
pkgs.zlib
pkgs.libffi
pkgs.readline
pkgs.ncurses
]
++ pkgs.lib.optionals pkgs.stdenv.isLinux [
pkgs.libxml2
pkgs.libxslt
pkgs.postgresql
];
shellHook = ''
echo "Rails Builder Bootstrap Shell (${system})"
echo "Ruby: $(ruby --version)"
echo "Bundix: $(bundix --version 2>/dev/null || echo 'available')"
echo ""
echo "This shell is for bootstrapping new projects."
echo "For full development, initialize a project with:"
echo " nix flake init -t github:glenndavy/rails-builder#universal"
echo " nix develop .#with-bundler"
'';
};
# Auto-detecting dev shell
# With --impure: detects Ruby version, framework, and dependencies from CWD
# Without --impure: provides a generic Ruby dev shell with sensible defaults
default = pkgs.mkShell {
name = "rails-builder-dev";
buildInputs = baseInputs ++ detectedInputs ++ linuxInputs;
shellHook = ''
echo "Rails Builder Dev Shell (${system})"
echo " Ruby: ${rubyVersion} (${rubySource})"
${bundlerMsg}
${frameworkMsg}
${databaseMsgs}
${cacheMsgs}
${impureTip}
echo ""
'';
};
# Break-glass shell — minimal ruby + bundler, no bundlerEnv / gemset.nix
# / tailwindcss. Always entry-able, even when the project's normal shells
# won't eval. Use --impure to pick up the project's ruby version.
safe = import ./imports/make-safe-shell.nix {
inherit pkgs;
src =
if isImpure
then cwdPath
else null;
};
});
# NixOS modules for systemd service deployment
nixosModules = {
# Core module (framework agnostic)
ruby-app = import ./nixos-modules/rails-app.nix;
# Framework-specific aliases for discoverability
rails-app = self.nixosModules.ruby-app;
hanami-app = self.nixosModules.ruby-app;
sinatra-app = self.nixosModules.ruby-app;
rack-app = self.nixosModules.ruby-app;
# Default points to generic name
default = self.nixosModules.ruby-app;
};
};
}