From 9010e2da9860b8b555c725ed2d110b3518cec5e4 Mon Sep 17 00:00:00 2001 From: dawn <90008@gaze.systems> Date: Wed, 20 May 2026 22:04:01 +0300 Subject: [PATCH] add nix package --- .gitignore | 1 + flake.lock | 61 ++++++++++++++++++++++++++++++++++ flake.nix | 31 ++++++++++++++++++ nix/default.nix | 79 +++++++++++++++++++++++++++++++++++++++++++++ nix/modules.nix | 34 +++++++++++++++++++ nix/web-modules.nix | 34 +++++++++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nix/default.nix create mode 100644 nix/modules.nix create mode 100644 nix/web-modules.nix diff --git a/.gitignore b/.gitignore index 2be306e..09362ca 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ data .DS_Store web/dist web/node_modules +result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..811ca69 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1778869304, + "narHash": "sha256-30sZNZoA1cqF5JNO9fVX+wgiQYjB7HJqqJ4ztCDeBZE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d233902339c02a9c334e7e593de68855ad26c4cb", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1777168982, + "narHash": "sha256-GOkGPcboWE9BmGCRMLX3worL4EMnsnG8MyKmXNeYuhQ=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "f5901329dade4a6ea039af1433fb087bd9c1fe14", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1778716662, + "narHash": "sha256-m1Yf0wZ8j1OHjTc2UwHwyQRSnNeSgLJOd7q5Y45hzi4=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f7c1a2d347e4c52d5fb8d10cb4d94b5884e546fb", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "parts": "parts" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2890ed0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,31 @@ +{ + inputs.parts.url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + + outputs = inp: + inp.parts.lib.mkFlake {inputs = inp;} { + systems = ["x86_64-linux"]; + perSystem = { + lib, + config, + pkgs, + ... + }: { + devShells.default = pkgs.mkShell { + name = "drop-devshell"; + packages = with pkgs; [ + bun + typescript-language-server + ]; + shellHook = '' + export PATH="$PATH:$PWD/node_modules/.bin" + ''; + }; + packages.drop = pkgs.callPackage ./nix { + modules = pkgs.callPackage ./nix/modules.nix {}; + webModules = pkgs.callPackage ./nix/web-modules.nix {}; + }; + packages.default = config.packages.drop; + }; + }; +} diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 0000000..72e560b --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,79 @@ +{ + lib, + stdenv, + bun, + makeBinaryWrapper, + modules, # server node_modules + webModules, # web/node_modules (separate fetch) +}: + +stdenv.mkDerivation { + pname = "drop"; + version = "main"; + + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../package.json + ../bun.lock + ../src + ../web/package.json + ../web/bun.lock + ../web/index.html + ../web/src + ../web/public + ../web/vite.config.ts + ../web/tsconfig.json + ]; + }; + + nativeBuildInputs = [ bun makeBinaryWrapper ]; + buildInputs = [ stdenv.cc.cc.lib ]; + dontCheck = true; + + configurePhase = '' + runHook preConfigure + + cp -R --no-preserve=ownership,mode ${modules} node_modules + find node_modules -type d -exec chmod 755 {} \; + find node_modules/.bin -exec chmod 755 {} \; + + cp -R --no-preserve=ownership,mode ${webModules} web/node_modules + find web/node_modules -type d -exec chmod 755 {} \; + find web/node_modules/.bin -exec chmod 755 {} \; + substituteInPlace web/node_modules/.bin/vite \ + --replace-fail "/usr/bin/env node" "${bun}/bin/bun --bun" + + runHook postConfigure + ''; + + buildPhase = '' + runHook preBuild + + HOME=$TMPDIR ${bun}/bin/bun run --cwd web build + + HOME=$TMPDIR ${bun}/bin/bun build \ + --target bun \ + --outfile server.js \ + src/index.ts + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out/bin $out/web + cp server.js $out/server.js + cp -r web/dist $out/web/dist + + substituteInPlace $out/server.js \ + --replace-fail '"./web/dist"' '"'$out'/web/dist"' \ + --replace-fail '"web/dist/index.html"' '"'$out'/web/dist/index.html"' + + makeBinaryWrapper ${bun}/bin/bun $out/bin/drop \ + --add-flags "run --no-install $out/server.js" + + runHook postInstall + ''; +} diff --git a/nix/modules.nix b/nix/modules.nix new file mode 100644 index 0000000..b2a7bf6 --- /dev/null +++ b/nix/modules.nix @@ -0,0 +1,34 @@ +{ + lib, + stdenv, + bun, +}: +stdenv.mkDerivation { + name = "modules"; + + src = lib.fileset.toSource { + root = ../.; + fileset = lib.fileset.unions [ + ../package.json + ../bun.lock + ]; + }; + + nativeBuildInputs = [bun]; + + outputHash = "sha256-fsXrtXX9bp2NJHHSAjZ3ZCA2OogCWpgNkovhid9cBnI="; + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + + dontConfigure = true; + dontCheck = true; + dontFixup = true; + + buildPhase = '' + HOME=$TMPDIR bun install --no-cache --no-progress --frozen-lockfile + ''; + installPhase = '' + cp -R node_modules $out + ls -la $out + ''; +} diff --git a/nix/web-modules.nix b/nix/web-modules.nix new file mode 100644 index 0000000..844f06e --- /dev/null +++ b/nix/web-modules.nix @@ -0,0 +1,34 @@ +{ + lib, + stdenv, + bun, +}: +stdenv.mkDerivation { + name = "modules"; + + src = lib.fileset.toSource { + root = ../web; + fileset = lib.fileset.unions [ + ../web/package.json + ../web/bun.lock + ]; + }; + + nativeBuildInputs = [bun]; + + outputHash = "sha256-cRu2pxzfq3PIppiKMJ7RxU3ydKSrILmHGKcyrr9yFSo="; + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + + dontConfigure = true; + dontCheck = true; + dontFixup = true; + + buildPhase = '' + HOME=$TMPDIR bun install --no-cache --no-progress --frozen-lockfile + ''; + installPhase = '' + cp -R node_modules $out + ls -la $out + ''; +} -- 2.51.2