From 4c0b6c3eef88123d4357e03eb1115cabcd6ff790 Mon Sep 17 00:00:00 2001 From: Sergei Poljanski Date: Tue, 17 Nov 2026 14:01:16 +0400 Subject: [PATCH] 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 --- .forgejo/workflows/deploy.yaml | 7 + .forgejo/workflows/test.yaml | 7 + bin/check-gemset | 19 ++ flake.nix | 12 +- gemset.nix | 557 +++++++++++++++++++++++++++++++++ nix/module.nix | 94 ++++++ nix/package.nix | 57 ++++ 7 files changed, 752 insertions(+), 1 deletion(-) create mode 100755 bin/check-gemset create mode 100644 gemset.nix create mode 100644 nix/module.nix create mode 100644 nix/package.nix diff --git a/.forgejo/workflows/deploy.yaml b/.forgejo/workflows/deploy.yaml index e4551b9..a593623 100644 --- a/.forgejo/workflows/deploy.yaml +++ b/.forgejo/workflows/deploy.yaml @@ -54,6 +54,13 @@ jobs: TEST_DATABASE_URL: postgres://postgres@postgres:5432/asxpio_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 diff --git a/.forgejo/workflows/test.yaml b/.forgejo/workflows/test.yaml index ed926d2..730496b 100644 --- a/.forgejo/workflows/test.yaml +++ b/.forgejo/workflows/test.yaml @@ -61,3 +61,10 @@ jobs: env: TEST_DATABASE_URL: postgres://postgres@postgres:5432/asxpio_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 diff --git a/bin/check-gemset b/bin/check-gemset new file mode 100755 index 0000000..b82876c --- /dev/null +++ b/bin/check-gemset @@ -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 diff --git a/flake.nix b/flake.nix index 63ff4d5..9279bbc 100644 --- a/flake.nix +++ b/flake.nix @@ -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.asxpio (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 diff --git a/gemset.nix b/gemset.nix new file mode 100644 index 0000000..2ffb931 --- /dev/null +++ b/gemset.nix @@ -0,0 +1,557 @@ +{ + 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"; + }; + chunky_png = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1znw5x86hmm9vfhidwdsijz8m38pqgmv98l9ryilvky0aldv7mc9"; + type = "gem"; + }; + version = "1.4.0"; + }; + date = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1h0db8r2v5llxdbzkzyllkfniqw9gm092qn7cbaib73v9lw0c3bm"; + type = "gem"; + }; + version = "3.5.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"; + }; + mail = { + dependencies = ["logger" "mini_mime" "net-imap" "net-pop" "net-smtp"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1s30l5z7jkazgi4m6l6mh80rgsyhh2pp1pa5h70xclsj8z54wmq6"; + type = "gem"; + }; + version = "2.9.1"; + }; + matrix = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0nscas3a4mmrp1rc07cdjlbbpb2rydkindmbj3v3z5y1viyspmd0"; + type = "gem"; + }; + version = "0.4.3"; + }; + mini_mime = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1vycif7pjzkr29mfk4dlqv3disc5dn0va04lkwajlpr1wkibg0c6"; + type = "gem"; + }; + version = "1.1.5"; + }; + minitest = { + dependencies = ["drb" "prism"]; + groups = ["test"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0vh733a365lw2dpa8zvdmr35715kfrm9hqsli8g67248maakadzz"; + type = "gem"; + }; + version = "6.1.0"; + }; + minitest-mock = { + groups = ["test"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1c2m99b9hgvhyhzv0nxjqlgymr0vmxpalzlq41lrcyj1hmqysh3h"; + type = "gem"; + }; + version = "5.27.0"; + }; + multi_json = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1040lr5y2phn7avdyam6zw6ikprlmk77biw3yhclsfwfh0qnl4p6"; + type = "gem"; + }; + version = "1.21.1"; + }; + mustermann = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "163i29mdcr1h0nximk3d51a1fgp7vz3sfasn8p1rjm2d4g3p0qac"; + type = "gem"; + }; + version = "3.1.1"; + }; + net-imap = { + dependencies = ["date" "net-protocol"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1ngnmjc7vc4k06dyapgid2gxc4si6yr092sidfps6r3v49ibnc7b"; + type = "gem"; + }; + version = "0.6.8"; + }; + net-pop = { + dependencies = ["net-protocol"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1wyz41jd4zpjn0v1xsf9j778qx1vfrl24yc20cpmph8k42c4x2w4"; + type = "gem"; + }; + version = "0.1.2"; + }; + net-protocol = { + dependencies = ["timeout"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1a32l4x73hz200cm587bc29q8q9az278syw3x6fkc9d1lv5y0wxa"; + type = "gem"; + }; + version = "0.2.2"; + }; + net-smtp = { + dependencies = ["net-protocol"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0g4gcydr0hza3wiizny0p5q4phl3fkhfsa4r5xir8bbb9p03q41m"; + type = "gem"; + }; + version = "0.5.2"; + }; + nio4r = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "18fwy5yqnvgixq3cn0h63lm8jaxsjjxkmj8rhiv8wpzv9271d43c"; + type = "gem"; + }; + version = "2.7.5"; + }; + pdf-core = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "14ffyc016pznyir87dcm2gyavwcppdyf87hyjhzixh3340g10p8a"; + type = "gem"; + }; + version = "0.10.0"; + }; + pg = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1yval9rdbc0p01dmf8zwm953w1hk0ggdp1pgr7a87s442983ddgp"; + type = "gem"; + }; + version = "1.7.0"; + }; + prawn = { + dependencies = ["matrix" "pdf-core" "ttfunk"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1diwqjchmymhj4ihp4xaad2mjc8q8bmibbbxwfd5pgrh9wxhxqpl"; + type = "gem"; + }; + version = "2.5.0"; + }; + prawn-table = { + dependencies = ["prawn"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1nxd6qmxqwl850icp18wjh5k0s3amxcajdrkjyzpfgq0kvilcv9k"; + type = "gem"; + }; + version = "0.2.2"; + }; + 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"; + }; + rqrcode = { + dependencies = ["chunky_png" "rqrcode_core"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0hlm1cfqs891irh4pl6wynsfm7nh7w7baf0g6cqxfrxvlr64khb4"; + type = "gem"; + }; + version = "3.2.0"; + }; + rqrcode_core = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0l9hl5nb7jx8sjchsrlv6bk30hywr449ihcdxv2qy6wwz1fvh0zk"; + type = "gem"; + }; + version = "2.1.0"; + }; + 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"; + }; + sinatra-contrib = { + dependencies = ["multi_json" "mustermann" "rack-protection" "sinatra" "tilt"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "0jkbsaika8pr9bc90ag3wqrhbgiy7h5a93k11j8sls6j8k4r3l0h"; + type = "gem"; + }; + version = "4.2.1"; + }; + tilt = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1cvaikq1dcbfl008i16c1pi1gmdax7vfkvmhch64jdkakyk9nnqd"; + type = "gem"; + }; + version = "2.7.0"; + }; + timeout = { + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1jxcji88mh6xsqz0mfzwnxczpg7cyniph7wpavnavfz7lxl77xbq"; + type = "gem"; + }; + version = "0.6.1"; + }; + ttfunk = { + dependencies = ["bigdecimal"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1ji0kn8jkf1rpskv3ijzxvqwixg4p6sk8kg0vmwyjinci7jcgjx7"; + type = "gem"; + }; + version = "1.8.0"; + }; +} diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..1a70eac --- /dev/null +++ b/nix/module.nix @@ -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"; + }; + }; + }; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..d8ce4b9 --- /dev/null +++ b/nix/package.nix @@ -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"; +}