Build the app as a Nix package, with a NixOS module to run it
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:
parent
8de75012ad
commit
adc6e824e5
2 changed files with 536 additions and 1 deletions
84
nix/module.nix
Normal file
84
nix/module.nix
Normal 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
51
nix/package.nix
Normal 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";
|
||||
}
|
||||
Loading…
Reference in a new issue