From afc1db95e068d7c088eb27b437cf94b8f20b846c Mon Sep 17 00:00:00 2001 From: nelind Date: Wed, 28 Jan 2026 17:18:38 +0100 Subject: [PATCH] feat(nix): derivation for frontend --- flake.lock | 19 ++++++++++++++- flake.nix | 21 +++++++++++++---- frontend.nix | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 frontend.nix diff --git a/flake.lock b/flake.lock index 1c499aa..8345151 100644 --- a/flake.lock +++ b/flake.lock @@ -16,9 +16,26 @@ "type": "github" } }, + "nixpkgs-fetch-deno": { + "locked": { + "lastModified": 1766410835, + "narHash": "sha256-dRhVt0aFDyTqppyzRLxiO1JZEAoIA2fUnaeyJTe+UwU=", + "owner": "aMOPel", + "repo": "nixpkgs", + "rev": "c9801acc8c4fac6377d076bc1c102b15bd9cfa6f", + "type": "github" + }, + "original": { + "owner": "aMOPel", + "ref": "feat/fetchDenoDeps", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "nixpkgs-fetch-deno": "nixpkgs-fetch-deno" } } }, diff --git a/flake.nix b/flake.nix index 5dd1449..99202f9 100644 --- a/flake.nix +++ b/flake.nix @@ -1,18 +1,29 @@ { - inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; - outputs = { self, nixpkgs, ... }: let + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + + # tranquil frontend uses deno as its package manager and build time runtime. + # nixpkgs does not have deno support yet but its being worked on in https://github.com/NixOS/nixpkgs/pull/419255 + # for now we important that PR as well purely for its fetchDenoDeps + nixpkgs-fetch-deno.url = "github:aMOPel/nixpkgs/feat/fetchDenoDeps"; + }; + + outputs = { self, nixpkgs, ... } @ inputs : let forAllSystems = function: nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed ( - system: function nixpkgs.legacyPackages.${system} + system: (function system nixpkgs.legacyPackages.${system}) ); in { - packages = forAllSystems (pkgs: { + packages = forAllSystems (system: pkgs: { tranquil-pds = pkgs.callPackage ./default.nix { }; + tranquil-frontend = pkgs.callPackage ./frontend.nix { + inherit (inputs.nixpkgs-fetch-deno.legacyPackages.${system}) fetchDenoDeps; + }; default = self.packages.${pkgs.stdenv.hostPlatform.system}.tranquil-pds; }); - devShells = forAllSystems (pkgs: { + devShells = forAllSystems (system: pkgs: { default = pkgs.callPackage ./shell.nix { }; }); }; diff --git a/frontend.nix b/frontend.nix new file mode 100644 index 0000000..97c2493 --- /dev/null +++ b/frontend.nix @@ -0,0 +1,66 @@ +{ + lib, + stdenvNoCC, + + fetchDenoDeps, + fetchFromGitHub, + + buildGoModule, + + deno, + esbuild, +}: let + toml = (lib.importTOML ./Cargo.toml).workspace.package; + deno-deps = fetchDenoDeps { + pname = "tranquil-frontend-deno-deps"; + denoLock = ./frontend/deno.lock; + hash = "sha256-UB+E00TjWX0fTUZ7XwcwRJ/OUOSSJpz6Ss04U5i8dGI="; + }; + # the esbuild in upstream nixpkgs is too old. + esbuild' = esbuild.override { + buildGoModule = args: buildGoModule ( + args // ( + let + version = "0.27.2"; + in { + inherit version; + src = fetchFromGitHub { + owner = "evanw"; + repo = "esbuild"; + tag = "v${version}"; + hash = "sha256-JbJB3F1NQlmA5d0rdsLm4RVD24OPdV4QXpxW8VWbESA"; + }; + vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ"; + } + ) + ); + }; +in stdenvNoCC.mkDerivation { + pname = "tranquil-frontend"; + inherit (toml) version; + + src = ./frontend; + + nativeBuildInputs = [ + deno + ]; + # tell vite (through the esbuild api) where the nix provided esbuild binary is + env.ESBUILD_BINARY_PATH = lib.getExe esbuild'; + + buildPhase = '' + # copy the deps to the required location + cp -r --no-preserve=mode ${deno-deps.denoDeps}/.deno ./ + cp -r --no-preserve=mode ${deno-deps.denoDeps}/vendor ./ + + pwd + ls /build/frontend/vendor + + # Now you can run the project using deps + # you need to activate [deno's vendor feature](https://docs.deno.com/runtime/fundamentals/modules/#vendoring-remote-modules) + # you need to use the `$DENO_DIR` env var, to point deno to the correct local cache + DENO_DIR=./.deno deno run --frozen --cached-only build + ''; + installPhase = '' + cp -r ./dist $out + ''; +}