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/asxpio is Puma serving config.ru, bin/asxpio-monitor the reachability worker. Photos are resized with ImageMagick 7's magick. nixosModules.default (services.asxpio) runs both as hardened systemd services with a dynamic user, the web one with readiness from Puma (Type=notify); settings and secrets come from one environment file. CI checks that gemset.nix matches Gemfile.lock (bin/check-gemset) and builds the package. Checked locally: migrations on an empty database, /healthz and /, an invoice PDF with the Noto fonts, a photo resized to WebP and cropped. The container deploy stays until the NixOS service has taken over. Assisted-by: Claude:opus-5-5
This commit is contained in:
parent
c8c81ea584
commit
4c0b6c3eef
2 changed files with 752 additions and 1 deletions
94
nix/module.nix
Normal file
94
nix/module.nix
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
# services.asxpio: the site, invoices and admin (Puma on a port, for a
|
||||
# proxy, Traefik on asxpio-ops, to route asxp.io and admin.asxp.io to), and
|
||||
# the reachability worker for datacenterbingo's status page.
|
||||
self:
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.asxpio;
|
||||
inherit (lib) mkOption types;
|
||||
hardening = {
|
||||
DynamicUser = true;
|
||||
Restart = "always";
|
||||
RestartSec = 5;
|
||||
# The database may be down for a while; keep trying
|
||||
ProtectSystem = "strict";
|
||||
ProtectHome = true;
|
||||
PrivateTmp = true;
|
||||
PrivateDevices = true;
|
||||
NoNewPrivileges = true;
|
||||
CapabilityBoundingSet = "";
|
||||
RestrictAddressFamilies = [
|
||||
"AF_INET"
|
||||
"AF_INET6"
|
||||
"AF_UNIX"
|
||||
];
|
||||
SystemCallFilter = [ "@system-service" ];
|
||||
LockPersonality = true;
|
||||
EnvironmentFile = cfg.environmentFile;
|
||||
};
|
||||
in
|
||||
{
|
||||
options.services.asxpio = {
|
||||
enable = lib.mkEnableOption "asxp.io";
|
||||
package = mkOption {
|
||||
type = types.package;
|
||||
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
|
||||
defaultText = lib.literalExpression "the asxpio flake's package";
|
||||
};
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 3001;
|
||||
};
|
||||
threads = mkOption {
|
||||
type = types.str;
|
||||
default = "4:16";
|
||||
description = "Puma's -t min:max.";
|
||||
};
|
||||
environmentFile = mkOption {
|
||||
type = types.str;
|
||||
description = "The app's settings and secrets (see .env.example).";
|
||||
};
|
||||
monitor.enable = mkOption {
|
||||
type = types.bool;
|
||||
default = true;
|
||||
description = "The reachability worker (bin/monitor). Run one only: two record every sample twice.";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
systemd.services.asxpio = {
|
||||
description = "asxp.io";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
startLimitIntervalSec = 0;
|
||||
# Puma tells systemd when it listens; migrations run before that
|
||||
serviceConfig = hardening // {
|
||||
Type = "notify";
|
||||
ExecStart = "${lib.getExe cfg.package} -b tcp://0.0.0.0:${toString cfg.port} -t ${cfg.threads}";
|
||||
TimeoutStartSec = 120;
|
||||
MemoryMax = "512M";
|
||||
};
|
||||
};
|
||||
systemd.services.asxpio-monitor = lib.mkIf cfg.monitor.enable {
|
||||
description = "asxpio reachability worker";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [
|
||||
"network-online.target"
|
||||
"asxpio.service"
|
||||
];
|
||||
wants = [ "network-online.target" ];
|
||||
startLimitIntervalSec = 0;
|
||||
environment.MONITOR_HEARTBEAT = "/tmp/monitor.heartbeat";
|
||||
serviceConfig = hardening // {
|
||||
ExecStart = "${cfg.package}/bin/asxpio-monitor";
|
||||
MemoryMax = "256M";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
57
nix/package.nix
Normal file
57
nix/package.nix
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
# 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/asxpio is Puma serving config.ru (pass -b and -t like
|
||||
# to puma); bin/asxpio-monitor is the reachability worker (bin/monitor).
|
||||
{
|
||||
lib,
|
||||
stdenvNoCC,
|
||||
bundlerEnv,
|
||||
makeWrapper,
|
||||
ruby_4_0,
|
||||
imagemagick,
|
||||
version,
|
||||
}:
|
||||
let
|
||||
root = ../.;
|
||||
gems = bundlerEnv {
|
||||
name = "asxpio-gems";
|
||||
ruby = ruby_4_0;
|
||||
gemfile = root + "/Gemfile";
|
||||
lockfile = root + "/Gemfile.lock";
|
||||
gemset = root + "/gemset.nix";
|
||||
groups = [ "default" ];
|
||||
};
|
||||
share = "$out/share/asxpio";
|
||||
in
|
||||
stdenvNoCC.mkDerivation {
|
||||
pname = "asxpio";
|
||||
inherit version;
|
||||
src = lib.fileset.toSource {
|
||||
inherit root;
|
||||
fileset = lib.fileset.unions [
|
||||
../asxpio.rb
|
||||
../config.ru
|
||||
../lib
|
||||
../views
|
||||
../public
|
||||
../db
|
||||
../bin/monitor
|
||||
];
|
||||
};
|
||||
nativeBuildInputs = [ makeWrapper ];
|
||||
# The lock lists precompiled pg/ffi/prism for many platforms; Nix builds
|
||||
# the source gems, so Bundler must not look for the others. Photos are
|
||||
# resized with ImageMagick 7's magick (the image had 6's convert).
|
||||
installPhase = ''
|
||||
mkdir -p $out/share $out/bin
|
||||
cp -r . ${share}
|
||||
common=(--chdir ${share} --set-default RACK_ENV production --set BUNDLE_FORCE_RUBY_PLATFORM true
|
||||
--set-default BINGO_CONVERT ${imagemagick}/bin/magick)
|
||||
makeWrapper ${gems}/bin/puma $out/bin/asxpio "''${common[@]}" \
|
||||
--add-flags "--preload ${share}/config.ru"
|
||||
makeWrapper ${gems.wrappedRuby}/bin/ruby $out/bin/asxpio-monitor "''${common[@]}" \
|
||||
--add-flags ${share}/bin/monitor
|
||||
'';
|
||||
passthru = { inherit gems; };
|
||||
meta.mainProgram = "asxpio";
|
||||
}
|
||||
Loading…
Reference in a new issue