diff --git a/.gitea/workflows/nix-deploy.yml b/.gitea/workflows/nix-deploy.yml new file mode 100644 index 0000000..8a43398 --- /dev/null +++ b/.gitea/workflows/nix-deploy.yml @@ -0,0 +1,78 @@ +name: Build & Deploy (Nix) + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build-and-deploy: + strategy: + fail-fast: false + max-parallel: 1 + matrix: + service: [api, ml-service, web] + runs-on: ubuntu-latest + + steps: + - name: Check out repository + run: | + git clone https://git.imrnes.team/MythEclipse/zeavis-edu.git . + git checkout ${{ github.sha }} + + - name: Build & Deploy ${{ matrix.service }} + env: + VPS_HOST: ${{ secrets.VPS_HOST }} + VPS_USER: ${{ secrets.VPS_USER }} + VPS_SSH_KEY: ${{ secrets.VPS_SSH_KEY }} + run: | + set -eu + + # --- Install Nix & Build --- + curl -fsSL https://install.determinate.systems/nix \ + | sh -s -- install linux --no-confirm --init none 2>&1 + + mkdir -p /etc/nix + echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf + + . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh + + echo "=== Building: zeavis-${{ matrix.service }} ===" + STORE_PATH=$(nix build .#${{ matrix.service }} --impure --option sandbox false --no-link --print-out-paths | tail -1) + echo "=== Store path: $STORE_PATH" + + # --- Deploy --- + key_file=$(mktemp /tmp/deploy-key.XXXXXX) + if printf '%s' "$VPS_SSH_KEY" | base64 -d 2>/dev/null | head -c 6 | grep -q "BEGIN"; then + printf '%s' "$VPS_SSH_KEY" | base64 -d > "$key_file" + else + printf '%s\n' "$VPS_SSH_KEY" > "$key_file" + fi + chmod 600 "$key_file" + sed -i 's/\r$//' "$key_file" + ssh-keygen -y -f "$key_file" >/dev/null 2>&1 || { echo "SSH key invalid"; exit 1; } + + export NIX_SSHOPTS="-i $key_file -o StrictHostKeyChecking=no" + nix copy --to "ssh://${VPS_USER}@${VPS_HOST}" "$STORE_PATH" 2>&1 + + ssh -i "$key_file" -o StrictHostKeyChecking=no "${VPS_USER}@${VPS_HOST}" " + set -eu + if [ -d /nix/var/nix/profiles/zeavis-${{ matrix.service }} ] && [ ! -L /nix/var/nix/profiles/zeavis-${{ matrix.service }} ]; then + rm -rf /nix/var/nix/profiles/zeavis-${{ matrix.service }} + fi + nix-env --profile /nix/var/nix/profiles/zeavis-${{ matrix.service }} --set $STORE_PATH + systemctl daemon-reload + systemctl enable zeavis-${{ matrix.service }} 2>/dev/null || true + systemctl restart zeavis-${{ matrix.service }} + for i in \$(seq 1 30); do + systemctl is-active --quiet zeavis-${{ matrix.service }} && break + sleep 1 + done + systemctl is-active zeavis-${{ matrix.service }} || { + echo \"=== SERVICE FAILED — journal ===\" + journalctl -u zeavis-${{ matrix.service }} -n 40 --no-pager + exit 1 + } + systemctl status zeavis-${{ matrix.service }} --no-pager 2>&1 | head -8 + " 2>&1 diff --git a/Machine_Learning/model/model.onnx b/Machine_Learning/model/model.onnx new file mode 100644 index 0000000..8edd4ec Binary files /dev/null and b/Machine_Learning/model/model.onnx differ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..617b2f3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1785301185, + "narHash": "sha256-eoS3KQTO0aPWXZvIaRbRAzSSHW3l5wdMFXtT1ISfoKA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9bc02893134c733dd85de46ee4fb2fac696b5529", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..53b01dc --- /dev/null +++ b/flake.nix @@ -0,0 +1,145 @@ +{ + description = "ZeaVis Edu — Bun API + Rust ML + Vite web (Nix build)"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + packages = { + # ── API: Bun + Elysia + Drizzle (workspace) ──────────────── + api = pkgs.stdenvNoCC.mkDerivation { + pname = "zeavis-api"; + version = "0.1.0"; + src = ./.; + nativeBuildInputs = [ pkgs.bun ]; + + buildPhase = '' + export HOME="$TMPDIR" + bun install --frozen-lockfile + bun run --cwd packages/shared build + ''; + + installPhase = '' + mkdir -p $out/bin $out/lib/zeavis-api + cp -r package.json bun.lock bunfig.toml tsconfig.base.json $out/lib/zeavis-api/ + cp -r node_modules $out/lib/zeavis-api/node_modules + cp -r apps $out/lib/zeavis-api/apps + cp -r packages $out/lib/zeavis-api/packages + cat > $out/bin/zeavis-api << WRAPPER +#!${pkgs.runtimeShell} +cd $out/lib/zeavis-api +exec ${pkgs.bun}/bin/bun apps/api/src/index.ts +WRAPPER + chmod +x $out/bin/zeavis-api + ''; + }; + + # ── ML Service: Rust (axum + ort/onnxruntime) ────────────── + ml-service = pkgs.stdenv.mkDerivation { + pname = "zeavis-ml-service"; + version = "0.1.0"; + src = ./.; + nativeBuildInputs = [ pkgs.rustc pkgs.cargo pkgs.pkg-config pkgs.cacert ]; + buildInputs = [ pkgs.openssl ]; + + buildPhase = '' + export HOME="$TMPDIR" CARGO_HOME="$TMPDIR/.cargo" + export SRC_ROOT="$PWD" + cd apps/ml-service + cargo build --locked --release + ''; + + installPhase = '' + cd "$SRC_ROOT" + mkdir -p $out/bin $out/share/zeavis-ml + cp apps/ml-service/target/release/zeavis-ml-service $out/bin/.zeavis-ml-service + cp Machine_Learning/model/model.onnx $out/share/zeavis-ml/model.onnx + cat > $out/bin/zeavis-ml-service << WRAPPER +#!${pkgs.runtimeShell} +export MODEL_PATH="$out/share/zeavis-ml/model.onnx" +export MODEL_INPUT_SIZE="224" +export ML_SERVICE_HOST="0.0.0.0" +export ML_SERVICE_PORT="8200" +export RUST_LOG="info" +exec $out/bin/.zeavis-ml-service +WRAPPER + chmod +x $out/bin/zeavis-ml-service + ''; + }; + + # ── Web: Vite static + nginx ─────────────────────────────── + web = pkgs.stdenvNoCC.mkDerivation { + pname = "zeavis-web"; + version = "0.1.0"; + src = ./.; + nativeBuildInputs = [ pkgs.bun ]; + + buildPhase = '' + export HOME="$TMPDIR" + bun install --frozen-lockfile + bun run --cwd packages/shared build + bun run --cwd apps/web build + ''; + + installPhase = '' + mkdir -p $out/bin $out/etc $out/share/zeavis-web/html + cp -r apps/web/dist/* $out/share/zeavis-web/html/ + cat > $out/etc/nginx.conf << CONF +error_log /var/lib/zeavis-web/nginx-error.log; +pid /var/lib/zeavis-web/nginx.pid; +events {} +http { + include ${pkgs.nginx}/conf/mime.types; + access_log /var/lib/zeavis-web/nginx-access.log; + server { + listen 8088; + server_name _; + root $out/share/zeavis-web/html; + index index.html; + + location /api/ { + proxy_pass http://127.0.0.1:3200/api/; + proxy_set_header Host \$host; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto \$scheme; + } + + location /metrics { + proxy_pass http://127.0.0.1:3200/metrics; + proxy_set_header Host \$host; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto \$scheme; + } + + location / { + try_files \$uri \$uri/ /index.html; + } + } +} +CONF + cat > $out/bin/zeavis-web << WRAPPER +#!${pkgs.runtimeShell} +mkdir -p /var/lib/zeavis-web +exec ${pkgs.nginx}/bin/nginx -c $out/etc/nginx.conf -p /var/lib/zeavis-web -g "daemon off;" +WRAPPER + chmod +x $out/bin/zeavis-web + ''; + }; + + default = self.packages.${system}.api; + }; + + devShells.default = pkgs.mkShell { + buildInputs = [ pkgs.bun pkgs.nodejs_22 pkgs.rustc pkgs.cargo ]; + }; + }); +}