Separate apps and assets

This commit is contained in:
Akshay Mankar 2023-06-03 10:24:05 +02:00
parent 59249c421d
commit 779d1ad5d9
Signed by untrusted user: axeman
GPG key ID: CA08F3AB62369B89
7 changed files with 120 additions and 84 deletions

19
node/erpnext-app.nix Normal file
View file

@ -0,0 +1,19 @@
{ fetchFromGitHub
, fetchYarnDeps
, runCommand
, nodejs
, yarn
, path
, nodePackages
}:
let
src = import ../srcs/erpnext.nix {inherit fetchFromGitHub; };
offlineCache = fetchYarnDeps {
yarnLock = "${src}/yarn.lock";
sha256 = "sha256-Vho4BSbxcsVYExLvUaeoc3xIpbXoCUP/4jw4RwGnWGY=";
};
mkApp = import ./mk-app.nix {
inherit path runCommand nodejs yarn nodePackages;
};
in mkApp "erpnext" src offlineCache

View file

@ -1,72 +0,0 @@
{ fetchFromGitHub
, fetchYarnDeps
, runCommand
, nodejs
, yarn
, path
, nodePackages
}:
let
erpnextSrc = import ../srcs/erpnext.nix {inherit fetchFromGitHub; };
erpnextOfflineCache = fetchYarnDeps {
yarnLock = "${erpnextSrc}/yarn.lock";
sha256 = "sha256-Vho4BSbxcsVYExLvUaeoc3xIpbXoCUP/4jw4RwGnWGY=";
};
frappeSrc = import ../srcs/frappe.nix {inherit fetchFromGitHub; };
frappeOfflineCache = fetchYarnDeps {
yarnLock = "${frappeSrc}/yarn.lock";
sha256 = "sha256-PBdMUz9gJIoQaqQYbdk+xnd8CyZPmdeyz/9WznCb4Ss=";
};
# Copied from nixpkgs:pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix
fixup_yarn_lock = runCommand "fixup_yarn_lock" {buildInputs = [ nodejs ];} ''
mkdir -p $out/lib
mkdir -p $out/bin
cp ${path}/pkgs/development/tools/yarn2nix-moretea/yarn2nix/lib/urlToName.js $out/lib/urlToName.js
cp ${path}/pkgs/development/tools/yarn2nix-moretea/yarn2nix/internal/fixup_yarn_lock.js $out/bin/fixup_yarn_lock
patchShebangs $out
'';
mkApp = name: src: yarnOfflineCache: runCommand "${name}-app" {
buildInputs = [fixup_yarn_lock yarn nodePackages.node-gyp-build];
} ''
mkdir -p $out/share/apps
cp -r ${src} $out/share/apps/${name}
chmod -R +w $out/share/apps/${name}
export HOME=$(mktemp -d)
yarn config --offline set yarn-offline-mirror ${yarnOfflineCache}
cd $out/share/apps/${name}
fixup_yarn_lock yarn.lock
yarn --offline --ignore-scripts install
'';
frappeApp = mkApp "frappe" frappeSrc frappeOfflineCache;
erpnextApp = mkApp "erpnext" erpnextSrc erpnextOfflineCache;
in runCommand "frappe-erpnext-apps-sites" {buildInputs = [yarn]; } ''
mkdir -p $out/share/sites $out/share/apps
# Cannot symlink because the code which traverses path to find sites
# directory gets confused.
cp -r ${frappeApp}/share/apps/frappe $out/share/apps/frappe
cp -r ${erpnextApp}/share/apps/erpnext $out/share/apps/erpnext
cat > $out/share/sites/apps.txt <<EOF
frappe
erpnext
EOF
cd $out/share/apps/frappe
yarn --offline production
symlinkPublic() {
find $out/share/apps/$1/$1/public -type d -mindepth 1 -maxdepth 1 | xargs -I '{}' bash -c "ln -s {} $out/share/sites/assets/$1/"'$(basename {})'
}
symlinkPublic frappe
symlinkPublic erpnext
''

18
node/frappe-app.nix Normal file
View file

@ -0,0 +1,18 @@
{ fetchFromGitHub
, fetchYarnDeps
, runCommand
, nodejs
, yarn
, path
, nodePackages
}:
let
src = import ../srcs/frappe.nix {inherit fetchFromGitHub; };
offlineCache = fetchYarnDeps {
yarnLock = "${src}/yarn.lock";
sha256 = "sha256-PBdMUz9gJIoQaqQYbdk+xnd8CyZPmdeyz/9WznCb4Ss=";
};
mkApp = import ./mk-app.nix {
inherit path runCommand nodejs yarn nodePackages;
};
in mkApp "frappe" src offlineCache

View file

@ -0,0 +1,32 @@
{ runCommand
, yarn
, frappe-app
, erpnext-app
}:
runCommand "frappe-erpnext-apps-sites" {buildInputs = [yarn]; } ''
mkdir -p sites apps
# Cannot symlink because the code which traverses path to find sites
# directory gets confused.
cp -r ${frappe-app}/share/apps/frappe apps/frappe
cp -r ${erpnext-app}/share/apps/erpnext apps/erpnext
cat > sites/apps.txt <<EOF
frappe
erpnext
EOF
pushd apps/frappe > /dev/null
yarn --offline production
popd > /dev/null
symlinkPublic() {
find $1/share/apps/$2/$2/public -type d -mindepth 1 -maxdepth 1 | xargs -I '{}' bash -c "ln -s {} sites/assets/$2/"'$(basename {})'
}
symlinkPublic ${frappe-app} frappe
symlinkPublic ${erpnext-app} erpnext
mkdir -p $out/share/sites
cp -r sites/assets $out/share/sites/assets
''

32
node/mk-app.nix Normal file
View file

@ -0,0 +1,32 @@
{ runCommand
, path
, nodejs
, yarn
, nodePackages
}:
let
# Copied from nixpkgs:pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix
fixup_yarn_lock = runCommand "fixup_yarn_lock" {buildInputs = [ nodejs ];} ''
mkdir -p $out/lib
mkdir -p $out/bin
cp ${path}/pkgs/development/tools/yarn2nix-moretea/yarn2nix/lib/urlToName.js $out/lib/urlToName.js
cp ${path}/pkgs/development/tools/yarn2nix-moretea/yarn2nix/internal/fixup_yarn_lock.js $out/bin/fixup_yarn_lock
patchShebangs $out
'';
in name: src: yarnOfflineCache: runCommand "${name}-app" {
buildInputs = [fixup_yarn_lock yarn nodePackages.node-gyp-build];
} ''
mkdir -p $out/share/apps
cp -r ${src} $out/share/apps/${name}
chmod -R +w $out/share/apps/${name}
export HOME=$(mktemp -d)
yarn config --offline set yarn-offline-mirror ${yarnOfflineCache}
cd $out/share/apps/${name}
fixup_yarn_lock yarn.lock
yarn --offline --ignore-scripts install
''

View file

@ -1,5 +1,7 @@
self: super: {
run-erpnext = self.callPackage ./scripts/run-erpnext.nix {};
erpnext-apps-sites = self.callPackage ./node/erpnext-apps-sites.nix {};
frappe-erpnext-assets = self.callPackage ./node/frappe-erpnext-assets.nix {};
erpnext-app = self.callPackage ./node/erpnext-app.nix {};
frappe-app = self.callPackage ./node/frappe-app.nix {};
erpnext-nginx-conf = self.callPackage ./nginx-erpnext-conf.nix {};
}

View file

@ -2,7 +2,9 @@
, python3
, nodejs
, writeShellApplication
, erpnext-apps-sites
, frappe-app
, erpnext-app
, frappe-erpnext-assets
, erpnext-nginx-conf
}:
let
@ -21,13 +23,18 @@ in writeShellApplication {
tmp=/tmp/erpnext
mkdir -p $tmp/sites $tmp/config/pids $tmp/logs/nginx $tmp/env/bin
mkdir -p $tmp/apps $tmp/sites $tmp/config/pids $tmp/logs/nginx $tmp/env/bin
ln -s ${erpnext-apps-sites}/share/apps $tmp/apps
ln -s ${frappe-app}/share/apps/frappe $tmp/apps/frappe
ln -s ${erpnext-app}/share/apps/erpnext $tmp/apps/erpnext
ln -s ${frappe-erpnext-assets}/share/sites/assets $tmp/sites/assets
ln -s ${penv} $tmp/env
ln -s ${erpnext-nginx-conf} $tmp/nginx-erpnext.conf
for f in ${erpnext-apps-sites}/share/sites/*; do
ln -s "$f" "$tmp/sites/$(basename "$f")"
done
cat > $tmp/sites/apps.txt <<EOF
frappe
erpnext
EOF
cat >$tmp/sites/common_site_config.json <<EOF
{
@ -42,16 +49,14 @@ in writeShellApplication {
}
EOF
ln -s ${penv} $tmp/env
ln -sf ${erpnext-nginx-conf} $tmp/nginx-erpnext.conf
cd $tmp
# Upstream initializes the DB with this command
# TODO: Make this idempotent
cd $tmp
bench new-site localhost --mariadb-root-password password --admin-password admin
bench --site localhost install-app erpnext
echo "Workdir: $tmp"
# TODO: Run these as systemd units
node $tmp/apps/frappe/socketio.js &
gunicorn --chdir="$tmp/sites" --bind=0.0.0.0:9090 --threads=4 --workers=2 --worker-class=gthread --worker-tmp-dir=/dev/shm --timeout=120 --preload frappe.app:application
'';