1
0
mirror of synced 2024-12-04 19:17:58 +01:00
Bayshore/flake.nix

161 lines
6.8 KiB
Nix
Raw Normal View History

2023-06-21 17:49:44 +02:00
{
description = "Wangan Midnight Maximum Tune 6 private server";
inputs = {
2023-06-21 21:11:40 +02:00
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
2023-06-21 17:49:44 +02:00
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
let
packages = flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages."${system}";
2023-06-21 18:20:49 +02:00
inputs = with pkgs; [ pkg-config python3 prisma-engines ];
2023-06-21 17:49:44 +02:00
# This likely sucks ass
# https://github.com/prisma/prisma/issues/3026
prismaHook = with pkgs; ''
export PRISMA_MIGRATION_ENGINE_BINARY="${prisma-engines}/bin/migration-engine"
export PRISMA_QUERY_ENGINE_BINARY="${prisma-engines}/bin/query-engine"
export PRISMA_QUERY_ENGINE_LIBRARY="${prisma-engines}/lib/libquery_engine.node"
2023-06-21 21:10:15 +02:00
export PRISMA_INTROSPECTION_ENGINE_BINARY="${prisma-engines}/bin/migration-engine"
2023-06-21 17:49:44 +02:00
export PRISMA_FMT_BINARY="${prisma-engines}/bin/prisma-fmt"
'';
in rec {
packages.bayshore = pkgs.buildNpmPackage {
pname = "bayshore";
2023-07-27 04:58:57 +02:00
version = "1.1.2";
2023-06-21 17:49:44 +02:00
src = ./.;
2023-06-21 22:47:40 +02:00
npmDepsHash = "sha256-VbFdHmPF9we1MS8OibpJY51WKFUK3Iq9xNyb8GiBgL0=";
2023-06-21 17:49:44 +02:00
nativeBuildInputs = inputs;
buildInputs = inputs;
preBuild = ''
${prismaHook}
# Generate the Prisma client - without this the build'll fail
npm run prisma-generate
'';
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r dist $out/dist
2023-06-21 17:49:44 +02:00
cp -r prisma $out/prisma
2023-06-21 22:03:32 +02:00
# SLOW AND TAKES UP TOO MUCH DISK!
# THIS IS A MASSIVE BODGE!
cp -r node_modules $out/node_modules
2023-06-21 17:49:44 +02:00
2023-06-21 22:18:00 +02:00
mkdir -p $out/data
cp server_wangan.* $out/data
2023-06-21 17:49:44 +02:00
mkdir -p $out/bin
cat > $out/bin/bayshore <<EOF
#!${pkgs.stdenv.shell}
${prismaHook}
${pkgs.nodejs}/bin/node $out/dist/index.js \$@
EOF
chmod +x $out/bin/bayshore
cat > $out/bin/prisma <<EOF
#!${pkgs.stdenv.shell}
${prismaHook}
${pkgs.nodePackages.prisma}/bin/prisma \$@
EOF
chmod +x $out/bin/prisma
runHook postInstall
'';
meta = with pkgs.lib; {
description = "Wangan Midnight Maximum Tune 6 private server";
homepage = "https://github.com/ProjectAsakura/Bayshore";
license = licenses.agpl3Only;
};
2023-06-21 17:55:49 +02:00
# We don't need a devshell lol
};
2023-06-21 17:49:44 +02:00
2023-06-21 17:55:49 +02:00
apps.bayshore = flake-utils.lib.mkApp {
name = "bayshore";
drv = packages.bayshore;
2023-06-21 17:49:44 +02:00
};
}
);
in packages // {
nixosModule = { config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.bayshore;
pkg = self.packages.${pkgs.system}.bayshore;
in {
options.services.bayshore = {
enable = mkEnableOption "bayshore";
postgresUrl = mkOption {
type = types.str;
default = "postgres://bayshore:password@localhost:5432/bayshore";
};
# Yes, not exposing the config as options is ~cringe~. However, I'm cringe but I'm free.
configFile = mkOption {
type = types.str;
default = "/var/lib/bayshore/config.json";
};
};
config = mkIf cfg.enable {
systemd.services.bayshore = {
description = "Wangan Midnight Maximum Tune 6 private server";
wantedBy = ["multi-user.target"];
# We need to wait for PSQL to be up before starting!
2023-06-21 21:31:05 +02:00
after = ["network.target" "postgresql.target"];
2023-06-21 17:49:44 +02:00
preStart = ''
export DATABASE_URL="${cfg.postgresUrl}"
2023-06-21 21:45:16 +02:00
export POSTGRES_URL="${cfg.postgresUrl}"
export BAYSHORE_NIX="true"
2023-06-21 17:49:44 +02:00
${pkg}/bin/prisma migrate deploy --schema=${pkg}/prisma/schema.prisma
'';
script = ''
# I don't think Bayshore actually uses this var but
# it's good practice to set it anyway so I'll do it
export NODE_ENV=production
export DATABASE_URL="${cfg.postgresUrl}"
export POSTGRES_URL="${cfg.postgresUrl}"
export BAYSHORE_CONFIG_PATH="${cfg.configFile}"
2023-06-21 22:18:00 +02:00
export BAYSHORE_DATA_PATH="${pkg}/data"
2023-06-21 21:45:16 +02:00
export BAYSHORE_NIX="true"
2023-06-21 17:49:44 +02:00
# The ports will always be on default
# because they're set through env-vars here.
#
# However, I do not give a shit.
${pkg}/bin/bayshore
'';
serviceConfig.Restart = "always";
serviceConfig.RestartSec = "5s";
unitConfig = {
StartLimitIntervalSec = "500";
StartLimitBurst = "5";
};
2023-06-21 17:49:44 +02:00
};
# Don't create a user - we *want* to run as root
# else we won't be able to bind to 80. Could downgrade perms later,
# but honestly who cares? I sure don't, at this point. I just wanna get it up.
};
};
};
}