nix-flakes/hosts/server/hikari/hako/services/api.nix

87 lines
2.1 KiB
Nix

{ inputs, pkgs, ... }:
let
apiDependencies = pkgs.stdenv.mkDerivation {
pname = "imnyang-api-dependencies";
version = "unstable";
src = inputs.api;
nativeBuildInputs = [ pkgs.bun ];
dontConfigure = true;
buildPhase = ''
export HOME="$TMPDIR/home"
mkdir -p "$HOME"
bun install --frozen-lockfile --no-progress
'';
installPhase = ''
mkdir -p "$out"
cp -a node_modules "$out/node_modules"
'';
outputHashMode = "recursive";
outputHash = "sha256-ke6cG/cBON6d6giFo1RAQ4gMfIbmagJfCI1FfZl1w/8=";
};
apiPackage = pkgs.stdenv.mkDerivation {
pname = "imnyang-api";
version = "unstable";
src = inputs.api;
nativeBuildInputs = [ pkgs.bun ];
# Bun standalone executables contain the bundled application after the
# ELF sections; stripping them leaves only the Bun runtime.
dontStrip = true;
dontConfigure = true;
buildPhase = ''
cp -a ${apiDependencies}/node_modules ./node_modules
bun run build
'';
installPhase = ''
install -Dm755 server "$out/bin/server"
mkdir -p "$out/share/imnyang-api"
cp -r src/mitda "$out/share/imnyang-api/mitda"
'';
};
in
{
systemd.services.api = {
description = "Elysia";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
path = [ pkgs.dnsutils ];
preStart = ''
if ! test -e /var/lib/imnyang-api/nadae.hrts; then
printf '0\n' > /var/lib/imnyang-api/nadae.hrts
fi
'';
serviceConfig = {
Type = "simple";
User = "imnyang";
WorkingDirectory = "/var/lib/imnyang-api";
StateDirectory = "imnyang-api";
StateDirectoryMode = "0750";
ExecStart = "${apiPackage}/bin/server";
Restart = "always";
RestartSec = 5;
Environment = [
"NODE_ENV=production"
"MITDA_ASSETS_DIR=${apiPackage}/share/imnyang-api/mitda"
];
NoNewPrivileges = true;
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
};
};
}