Build the app as a Nix package, with a NixOS module to run it
All checks were successful
Build and Deploy to Production / test (push) Successful in 11m36s
Build and Deploy to Production / build (push) Successful in 49s
Build and Deploy to Production / deploy (push) Successful in 1m6s

packages.default is the app with its gems (bundlerEnv from Gemfile.lock
and gemset.nix, generated by bundix); bin/datacenterbingo is Puma serving
config.ru. nixosModules.default (services.datacenterbingo) runs it as a
hardened systemd service with a dynamic user, readiness from Puma
(Type=notify), settings in environment and secrets from a file. CI
checks that gemset.nix matches Gemfile.lock (bin/check-gemset) and
builds the package.

The container deploy stays until the NixOS service has taken over.

Assisted-by: Claude:opus-5-5
This commit is contained in:
Sergei Poljanski 2026-11-17 13:38:19 +04:00
commit adc6e824e5
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
7 changed files with 536 additions and 1 deletions

View file

@ -54,6 +54,13 @@ jobs:
TEST_DATABASE_URL: postgres://postgres@postgres:5432/bingo_test
run: nix develop -c bundle exec rake test
# What the servers run: gemset.nix in step with Gemfile.lock, and
# the package builds
- name: Package
run: |
nix develop -c ruby bin/check-gemset
nix build .#default
build:
runs-on: arch-latest
needs: test

View file

@ -61,3 +61,10 @@ jobs:
env:
TEST_DATABASE_URL: postgres://postgres@postgres:5432/bingo_test
run: nix develop -c bundle exec rake test
# What the servers run: gemset.nix in step with Gemfile.lock, and
# the package builds
- name: Package
run: |
nix develop -c ruby bin/check-gemset
nix build .#default

19
bin/check-gemset Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# gemset.nix (what Nix builds) must name the same gem versions as
# Gemfile.lock. After a Gemfile.lock change: nix run nixpkgs#bundix
lock = {}
File.read('Gemfile.lock').scan(/^ ([a-zA-Z0-9_.-]+) \(([^)]+)\)$/) do |name, ver|
lock[name] = ver.sub(/-(x86|aarch64|arm|arm64)[a-z0-9_-]*\z/, '')
end
gemset = {}
File.read('gemset.nix').scan(/^ "?([a-zA-Z0-9_.-]+)"? = \{.*?^ version = "([^"]+)";/m) do |name, ver|
gemset[name] = ver
end
bad = gemset.reject { |n, v| lock[n] == v }.map { |n, v| "#{n}: gemset #{v}, lock #{lock[n] || 'missing'}" }
bad += (lock.keys - gemset.keys).map { |n| "#{n}: in Gemfile.lock, missing from gemset.nix" }
if bad.empty?
puts "gemset.nix matches Gemfile.lock (#{gemset.size} gems)"
else
puts bad, '', 'Run: nix run nixpkgs#bundix'
exit 1
end

View file

@ -5,12 +5,22 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
outputs =
{ self, nixpkgs }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages.${system}.default = pkgs.callPackage ./nix/package.nix {
version = self.shortRev or self.dirtyShortRev or "dev";
};
# services.datacenterbingo (nix/module.nix)
nixosModules.default = import ./nix/module.nix self;
checks.${system}.package = self.packages.${system}.default;
devShells.${system}.default = pkgs.mkShell {
buildInputs = [
pkgs.ruby_4_0

357
gemset.nix Normal file
View file

@ -0,0 +1,357 @@
{
aws-eventstream = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "15nf544xhbhxgm4d38479yifqjspa41ysjp58h1v0jyzc6fakdnf";
type = "gem";
};
version = "1.4.1";
};
aws-partitions = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "15pszcgqb56z5gvks3y42rjpazri6a7hn642pa5iy4bxapyhdkyv";
type = "gem";
};
version = "1.1305.0";
};
aws-sdk-core = {
dependencies = ["aws-eventstream" "aws-partitions" "aws-sigv4" "base64" "bigdecimal" "jmespath" "logger" "rexml"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "04j61zz7d0d3ijldsn66mx2cnbgd8mr6818pf4bs1ni48x8506a7";
type = "gem";
};
version = "3.268.0";
};
aws-sdk-kms = {
dependencies = ["aws-sdk-core" "aws-sigv4"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1b0dfk3mrz0wpvjmaavcf01yfz8gb1ccvh7zmhbiifgjmg48c4hg";
type = "gem";
};
version = "1.143.0";
};
aws-sdk-s3 = {
dependencies = ["aws-sdk-core" "aws-sdk-kms" "aws-sigv4"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1fjvbavn4i8vqy6b7x9ncpkclgks1kzij7824r0bh9hyljj5rzgh";
type = "gem";
};
version = "1.242.0";
};
aws-sigv4 = {
dependencies = ["aws-eventstream"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "003ch8qzh3mppsxch83ns0jra8d222ahxs96p9cdrl0grfazywv9";
type = "gem";
};
version = "1.12.1";
};
base64 = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0yx9yn47a8lkfcjmigk79fykxvr80r4m1i35q82sxzynpbm7lcr7";
type = "gem";
};
version = "0.3.0";
};
bigdecimal = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0612spks81fvpv2zrrv3371lbs6mwd7w6g5zafglyk75ici1x87a";
type = "gem";
};
version = "3.3.1";
};
dotenv = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "17b1zr9kih0i3wb7h4yq9i8vi6hjfq07857j437a8z7a44qvhxg3";
type = "gem";
};
version = "3.2.0";
};
drb = {
groups = ["default" "test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0wrkl7yiix268s2md1h6wh91311w95ikd8fy8m5gx589npyxc00b";
type = "gem";
};
version = "2.2.3";
};
erubi = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1naaxsqkv5b3vklab5sbb9sdpszrjzlfsbqpy7ncbnw510xi10m0";
type = "gem";
};
version = "1.13.1";
};
ffi = {
groups = ["default" "development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1kqasqvy8d7r09ri4n6bkdwbk63j7afd9ilsw34nzlgh0qp69ldw";
type = "gem";
};
version = "1.17.4";
};
jmespath = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1cdw9vw2qly7q7r41s7phnac264rbsdqgj4l0h4nqgbjb157g393";
type = "gem";
};
version = "1.6.2";
};
listen = {
dependencies = ["logger" "rb-fsevent" "rb-inotify"];
groups = ["default" "development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1ln9c0vx165hkfbn2817qw4m6i77xcxh6q0r5v6fqfhlcbdq5qf6";
type = "gem";
};
version = "3.10.0";
};
logger = {
groups = ["default" "development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "00q2zznygpbls8asz5knjvvj2brr3ghmqxgr83xnrdj4rk3xwvhr";
type = "gem";
};
version = "1.7.0";
};
minitest = {
dependencies = ["drb" "prism"];
groups = ["test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0vh733a365lw2dpa8zvdmr35715kfrm9hqsli8g67248maakadzz";
type = "gem";
};
version = "6.1.0";
};
mustermann = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "163i29mdcr1h0nximk3d51a1fgp7vz3sfasn8p1rjm2d4g3p0qac";
type = "gem";
};
version = "3.1.1";
};
nio4r = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "18fwy5yqnvgixq3cn0h63lm8jaxsjjxkmj8rhiv8wpzv9271d43c";
type = "gem";
};
version = "2.7.5";
};
pg = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1yval9rdbc0p01dmf8zwm953w1hk0ggdp1pgr7a87s442983ddgp";
type = "gem";
};
version = "1.7.0";
};
prism = {
groups = ["default" "test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1wq1fqa52h90hynqcbwf35iydnv4fl03y4pvj0rii4qfx9npq2hw";
type = "gem";
};
version = "2.1.0";
};
puma = {
dependencies = ["nio4r"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0p2m65inmzqpza2x8ls6l5zdfa7n8rlinc2ns0cwqkh6rxc0g014";
type = "gem";
};
version = "8.1.1";
};
rack = {
groups = ["default" "test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1isfvhl6r55ddxwq8qsmn6zbljfmzjgq5zszjhm7mg1y8rgciv0z";
type = "gem";
};
version = "3.2.8";
};
rack-protection = {
dependencies = ["base64" "logger" "rack"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1b4bamcbpk29i7jvly3i7ayfj69yc1g03gm4s7jgamccvx12hvng";
type = "gem";
};
version = "4.2.1";
};
rack-session = {
dependencies = ["base64" "rack"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1s7zcxlmg88a6dam4aqbgk9xkpy6dkdfqmmcszkkliy3q3w38m2r";
type = "gem";
};
version = "2.1.2";
};
rack-test = {
dependencies = ["rack"];
groups = ["test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1s212a39z3v1ksgnhvwddsiq2n7dr4cqvzdnq4pmfb00rivx265i";
type = "gem";
};
version = "2.3.0";
};
rackup = {
dependencies = ["rack"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0s48d2a0z5f0cg4npvzznf933vipi6j7gmk16yc913kpadkw4ybc";
type = "gem";
};
version = "2.3.1";
};
rake = {
groups = ["test"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "009p524zl0p0kfa65nii8wdmaigkmawv9pbvlcffky7islmmp0nb";
type = "gem";
};
version = "13.4.2";
};
rb-fsevent = {
groups = ["default" "development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1zmf31rnpm8553lqwibvv3kkx0v7majm1f341xbxc0bk5sbhp423";
type = "gem";
};
version = "0.11.2";
};
rb-inotify = {
dependencies = ["ffi"];
groups = ["default" "development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0vmy8xgahixcz6hzwy4zdcyn2y6d6ri8dqv5xccgzc1r292019x0";
type = "gem";
};
version = "0.11.1";
};
rerun = {
dependencies = ["listen"];
groups = ["development"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1zc8xdzz56xa1lvh740igkh174dbvn574v3c2hqcnkdknbr5lslr";
type = "gem";
};
version = "0.14.0";
};
rexml = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "0hninnbvqd2pn40h863lbrn9p11gvdxp928izkag5ysx8b1s5q0r";
type = "gem";
};
version = "3.4.4";
};
sequel = {
dependencies = ["bigdecimal"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1biq47v96n705ia39vrx969x9nx6n4vy5b82pc4l3nra6mdk65n3";
type = "gem";
};
version = "5.110.0";
};
sinatra = {
dependencies = ["logger" "mustermann" "rack" "rack-protection" "rack-session" "tilt"];
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "103h6wjpcqp3i034hi44za2v365yz7qk9s5df8lmasq43nqvkbmp";
type = "gem";
};
version = "4.2.1";
};
tilt = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1cvaikq1dcbfl008i16c1pi1gmdax7vfkvmhch64jdkakyk9nnqd";
type = "gem";
};
version = "2.7.0";
};
}

84
nix/module.nix Normal file
View file

@ -0,0 +1,84 @@
# services.datacenterbingo: Puma on a port, for a proxy (Traefik on
# asxpio-ops) to route the public names to. Read-only: the bingo role and
# an S3 key that can only read the photo bucket.
self:
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.datacenterbingo;
inherit (lib) mkOption types;
in
{
options.services.datacenterbingo = {
enable = lib.mkEnableOption "datacenterbingo.xyz";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
defaultText = lib.literalExpression "the datacenterbingo flake's package";
};
port = mkOption {
type = types.port;
default = 3002;
};
threads = mkOption {
type = types.str;
default = "4:16";
description = "Puma's -t min:max.";
};
environment = mkOption {
type = types.attrsOf types.str;
default = { };
example = {
BINGO_HOST = "datacenterbingo.xyz";
S3_ENDPOINT = "http://garage:3900";
S3_REGION = "us-east-1";
S3_BUCKET = "datacenterbingo-photos";
};
description = "Settings without secrets.";
};
environmentFile = mkOption {
type = types.str;
description = "DATABASE_URL, S3_ACCESS_KEY, S3_SECRET_KEY, INDEXNOW_KEY.";
};
};
config = lib.mkIf cfg.enable {
systemd.services.datacenterbingo = {
description = "datacenterbingo.xyz";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
environment = cfg.environment;
# Puma tells systemd when it listens
serviceConfig = {
Type = "notify";
ExecStart = "${lib.getExe cfg.package} -b tcp://0.0.0.0:${toString cfg.port} -t ${cfg.threads}";
EnvironmentFile = cfg.environmentFile;
DynamicUser = true;
Restart = "always";
RestartSec = 5;
TimeoutStartSec = 60;
# The database may be down for a while; keep trying
StartLimitIntervalSec = 0;
MemoryMax = "384M";
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
NoNewPrivileges = true;
CapabilityBoundingSet = "";
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
SystemCallFilter = [ "@system-service" ];
LockPersonality = true;
};
};
};
}

51
nix/package.nix Normal file
View file

@ -0,0 +1,51 @@
# The app with its gems (bundlerEnv, from Gemfile.lock and gemset.nix;
# after changing Gemfile.lock run `nix run nixpkgs#bundix` to refresh
# gemset.nix). bin/datacenterbingo is Puma serving config.ru; pass -b and
# -t like to puma.
{
lib,
stdenvNoCC,
bundlerEnv,
makeWrapper,
ruby_4_0,
version,
}:
let
root = ../.;
gems = bundlerEnv {
name = "datacenterbingo-gems";
ruby = ruby_4_0;
gemfile = root + "/Gemfile";
lockfile = root + "/Gemfile.lock";
gemset = root + "/gemset.nix";
groups = [ "default" ];
};
in
stdenvNoCC.mkDerivation {
pname = "datacenterbingo";
inherit version;
src = lib.fileset.toSource {
inherit root;
fileset = lib.fileset.unions [
../app.rb
../config.ru
../lib
../views
../assets
];
};
nativeBuildInputs = [ makeWrapper ];
installPhase = ''
mkdir -p $out/share $out/bin
cp -r . $out/share/datacenterbingo
# The lock lists precompiled pg/ffi/prism for many platforms; Nix
# builds the source gems, so Bundler must not look for the others
makeWrapper ${gems}/bin/puma $out/bin/datacenterbingo \
--chdir $out/share/datacenterbingo \
--set-default RACK_ENV production \
--set BUNDLE_FORCE_RUBY_PLATFORM true \
--add-flags "--preload $out/share/datacenterbingo/config.ru"
'';
passthru = { inherit gems; };
meta.mainProgram = "datacenterbingo";
}