Skip to content

Commit

Permalink
Bundle the kadena-cli package (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
enobayram authored May 14, 2024
1 parent d3dd068 commit e264e3e
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 97 deletions.
19 changes: 18 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
{
description = "A nixpkgs analogue for Kadena's packages";
inputs = {
# We currently need nixpkgs-23-05 for a working version of rollup
# we can avoid this input when this issue is fixed:
# https://github.com/NixOS/nixpkgs/issues/294183
nixpkgs-23-05.url = "github:NixOS/nixpkgs/23.05";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = inputs: inputs.flake-utils.lib.eachDefaultSystem (system:
let inherit (inputs) self;
pkgs = inputs.nixpkgs.legacyPackages.${system};
pkgs-23-05 = inputs.nixpkgs-23-05.legacyPackages.${system};
nodePackages = import "${self}/pkgs/nodePackages" {
inherit pkgs system;
nodejs = pkgs.nodejs;
};
graph = import pkgs/graph {
inherit pkgs system nodePackages;
};
kadena-cli = import pkgs/kadena-cli {
inherit pkgs system nodePackages;
rollup = pkgs-23-05.nodePackages.rollup;
};
in {
packages = {
inherit (graph) kadena-graph kadena-graph-unbundled;
kadena-cli = nodePackages."@kadena/kadena-cli";
clibundle = nodePackages."clibundle-../kadena-cli";
kadena-cli = kadena-cli.kadena-cli-bundled;
};
apps = {
update-node-packages = {
Expand Down
3 changes: 2 additions & 1 deletion pkgs/graph/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ pkgs ? import <nixpkgs> {inherit system;}
, system ? builtins.currentSystem
, nodejs ? pkgs.nodejs
, nodejs-slim ? pkgs.nodejs-slim
, nodePackages ? import ../nodePackages { inherit pkgs system nodejs; }
}:
let
Expand Down Expand Up @@ -48,7 +49,7 @@ let
remove-references-to -t ${kadena-graph-unbundled} $out/bin/kadena-graph
substituteInPlace $out/bin/kadena-graph \
--replace "/usr/bin/env node" ${pkgs.nodejs-slim}/bin/node
--replace "/usr/bin/env node" ${nodejs-slim}/bin/node
wrapProgram $out/bin/kadena-graph \
--set PRISMA_QUERY_ENGINE_LIBRARY "${prisma-slim}/lib/libquery_engine.node" \
Expand Down
49 changes: 49 additions & 0 deletions pkgs/kadena-cli/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ pkgs ? import <nixpkgs> {inherit system;}
, system ? builtins.currentSystem
, nodejs ? pkgs.nodejs
, nodejs-slim ? pkgs.nodejs-slim
, rollup ? pkgs.nodePackages.rollup
, nodePackages ? import ../nodePackages { inherit pkgs system nodejs; }
}:
with pkgs.lib;
let
clibundle = nodePackages."clibundle-../kadena-cli";
kadena-cli-archive = lists.findSingle
(d: d.packageName == "@kadena/kadena-cli")
(builtins.abort "@kadena/kadena-cli not found in clibundle dependencies")
(builtins.abort "Multiple @kadena/kadena-cli found in clibundle dependencies")
clibundle.dependencies;
kadena-cli-bundled = pkgs.stdenv.mkDerivation rec {
inherit (kadena-cli-archive) version packageName;
buildInputs = [rollup pkgs.makeWrapper pkgs.removeReferencesTo];
name = "kadena-${version}";
enableParallelBuilding = true;
passAsFile = [ "buildCommand" ];
buildCommand = ''
mkdir -p $out
cp -r ${clibundle} clibundle
chmod -R +w clibundle
KADENACLI="clibundle/lib/node_modules/clibundle/"
( cd "$KADENACLI" &&
rollup -c rollup.config.mjs
)
mkdir -p $out/lib
cp $KADENACLI/lib/cli.mjs $out/lib/cli.mjs
cp $KADENACLI/package.json $out/package.json
remove-references-to -t ${clibundle} $out/lib/cli.mjs
mkdir -p $out/bin
cat - > $out/bin/kadena <<EOF
#!${pkgs.runtimeShell}
${nodejs-slim}/bin/node $out/lib/cli.mjs $@
EOF
chmod +x $out/bin/kadena
'';
};
in {
inherit nodePackages kadena-cli-bundled;
}
18 changes: 18 additions & 0 deletions pkgs/kadena-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "clibundle",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"bundle": "rollup -c rollup.config.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@kadena/kadena-cli": "^1.0.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3"
}
}
16 changes: 16 additions & 0 deletions pkgs/kadena-cli/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import resolve from "@rollup/plugin-node-resolve";

export default {
input: "node_modules/@kadena/kadena-cli/lib/index.js",
output: {
file: "./lib/cli.mjs",
format: "es",
intro: "const navigator = {};",
entryFileNames: "[name].mjs",
chunkFileNames: "[name].mjs",
inlineDynamicImports: true,
},
plugins: [resolve(), commonjs(), json()],
};
3 changes: 2 additions & 1 deletion pkgs/nodePackages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
}:
let
composition = import ./composition.nix { inherit pkgs system nodejs; };
filterFake = builtins.mapAttrs ( name: drv: drv.override ( old: {
filterFake = builtins.mapAttrs ( name: drv: drv.override ( old: rec {
dependencies = builtins.filter ( d: d.version != "fake-version" ) old.dependencies;
passthru = { inherit dependencies; } // old.passthru or {};
}));
update = pkgs.writeShellScript "update-node-packages" ''
set -e
Expand Down
Loading

0 comments on commit e264e3e

Please sign in to comment.