From 923499ad5c8e02bbe9e4e24884c67b3d44a8b775 Mon Sep 17 00:00:00 2001 From: Tobias Frischmann Date: Mon, 31 Aug 2026 13:21:43 +0200 Subject: [PATCH] init flake --- .gitignore | 3 ++ README.md | 29 +++++++++++++++++- flake.lock | 27 +++++++++++++++++ flake.nix | 50 ++++++++++++++++++++++++++++++++ nuxt.config.ts | 16 +++++++++- server/plugins/init.ts | 19 ++++++++++++ server/plugins/startUpAnalyze.ts | 3 -- server/tasks/analyze.ts | 6 ++-- 8 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 server/plugins/init.ts delete mode 100644 server/plugins/startUpAnalyze.ts diff --git a/.gitignore b/.gitignore index 68f19bd..d9d48ca 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ logs .env .env.* !.env.example + +# Nix +result diff --git a/README.md b/README.md index 7491249..440a95d 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,33 @@ a Page to view some global statistics about [Modrinth](https://modrinth.com/) +## Deployment via Nix + +### flake.nix + +```nix +inputs = { + ... + modrinth-statistics.url = + "git+https://tangled.org/tobinio.dev/modrinth-statistics?ref=refs/tags/"; + ... +}; +``` + +### configuration.nix + +```nix +systemd.services.modrinth-statistics = { + description = "Modrinth Statistics"; + after = [ "network.target", "postgresql.service" ]; + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + ExecStart = "${pkgs.nodejs_24}/bin/node ${inputs.modrinth-statistics.packages.${pkgs.system}.default}/server/index.mjs"; + }; +}; +``` + ### .env ```dotenv @@ -9,5 +36,5 @@ a Page to view some global statistics about [Modrinth](https://modrinth.com/) NUXT_DB_URL="postgresql://app:password@localhost:5432/app" NUXT_PUBLIC_UMAMI_ID="" -NUXT_SKIP_ANALYZE=false +NUXT_NO_DB_WRITE=false ``` diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..dac7a98 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1788039129, + "narHash": "sha256-pa4Q0qErvCvzCaaUph7Sm37RhR4xvPrYI8Lgz6k85+A=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d2f67949798825fe853f7c5d0492b8bf016d3f88", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..a3d12fe --- /dev/null +++ b/flake.nix @@ -0,0 +1,50 @@ +{ + description = "Modrinth Statistics Fullstack (Backend + Frontend)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs }: + let + system = "x86_64-linux"; + version = "4.0.0"; + pnpm = pkgs.pnpm_11; + pkgs = import nixpkgs { inherit system; }; + in + { + packages.${system}.default = pkgs.stdenv.mkDerivation (finalAttrs: { + pname = "modrinth-statistics"; + inherit version; + + src = ./.; + + nativeBuildInputs = [ + pkgs.nodejs + pkgs.pnpmConfigHook + pnpm + ]; + + pnpmDeps = pkgs.fetchPnpmDeps { + inherit (finalAttrs) pname version src; + inherit pnpm; + fetcherVersion = 4; + hash = "sha256-m7DknUzp6vRaAVKm5ZXLtof3CpUTLOsEO9NihPEM4p0="; + }; + + buildPhase = '' + runHook preBuild + export NUXT_TELEMETRY_DISABLED=1 + pnpm build + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + mkdir -p $out + cp -r .output/* $out/ + runHook postInstall + ''; + }); + }; +} diff --git a/nuxt.config.ts b/nuxt.config.ts index 6881d44..7c67a19 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -1,3 +1,5 @@ +import { cpSync } from "node:fs"; + const simple_types = ["projects", "versions", "authors", "files", "revenue"]; const project_types = [ "mod", @@ -57,11 +59,23 @@ export default defineNuxtConfig({ sitemap: { urls: urls, }, + scripts: { + assets: { + fallbackOnSrcOnBundleFail: true, + }, + }, + hooks: { + "nitro:build:public-assets"() { + cpSync("server/database/migrations", ".output/server/migrations", { + recursive: true, + }); + }, + }, runtimeConfig: { db: { url: undefined, }, - skipAnalyze: undefined, + noDbWrite: undefined, public: { umamiID: undefined, }, diff --git a/server/plugins/init.ts b/server/plugins/init.ts new file mode 100644 index 0000000..407ea7d --- /dev/null +++ b/server/plugins/init.ts @@ -0,0 +1,19 @@ +import { join } from "node:path"; +import { migrate } from "drizzle-orm/node-postgres/migrator"; + +export default defineNitroPlugin(async () => { + const noDbWrite = useRuntimeConfig().noDbWrite; + if (noDbWrite) { + LOGGER.warn("noDbWrite is enabled - not running database migration"); + return; + } + + LOGGER.info("Running database migration"); + const db = useDrizzle(); + await migrate(db, { + migrationsFolder: join(process.cwd(), "./server/migrations"), + }); + LOGGER.info("Database migration completed"); + + await runTask("analyze"); +}); diff --git a/server/plugins/startUpAnalyze.ts b/server/plugins/startUpAnalyze.ts deleted file mode 100644 index 7793613..0000000 --- a/server/plugins/startUpAnalyze.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default defineNitroPlugin(async () => { - await runTask("analyze"); -}); diff --git a/server/tasks/analyze.ts b/server/tasks/analyze.ts index b829f4c..69f7504 100644 --- a/server/tasks/analyze.ts +++ b/server/tasks/analyze.ts @@ -12,9 +12,9 @@ export default defineTask({ description: "Update all the statistics", }, async run() { - const skipAnalyze = useRuntimeConfig().skipAnalyze; - if (skipAnalyze) { - LOGGER.warn("skipAnalyze is enabled"); + const noDbWrite = useRuntimeConfig().noDbWrite; + if (noDbWrite) { + LOGGER.warn("noDbWrite is enabled - skipping analyze"); return { result: "Skipped" }; } -- knotmirror 0.0.1