Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update custom Nix dev deps to work on also linux #1862

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,10 @@
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs;
let

renamed_grpc_tools = stdenv.mkDerivation {
name = "renamed-grpc-tools";
version = "0.1";
phases = [ "installPhase" ];
installPhase = ''
mkdir -p $out/bin
cp -r ${protobuf}/include/ $out/bin/
cp "${grpc-tools}/bin/protoc" $out/bin/grpc_tools_node_protoc
cp "${grpc-tools}/bin/grpc_node_plugin" $out/bin/grpc_tools_node_protoc_plugin
'';
};

ts_protoc = stdenv.mkDerivation rec {
name = "protoc-gen-ts-wksp";
src = fetchFromGitHub {
owner = "thesayyn";
repo = "protoc-gen-ts";
rev = "0.8.7";
hash = "sha256-PGprtSPMRTodt/SD6gpEr/n22jiNqB1/C6HJGlDndLg=";
};
buildInputs = [ git cacert nodejs bazel ];
buildPhase = ''
export HOME=$(pwd)
mkdir -p $out/bin
npm ci
bazel build package
'';
installPhase = ''
cp bazel-bin/package/package/protoc-gen-ts.js $out/bin/protoc-gen-ts
'';
};

grpc-tools-node =
import ./nix-pkgs/grpc-tools-node.nix { inherit pkgs; };
protoc-gen-ts =
import ./nix-pkgs/protoc-gen-ts.nix { inherit pkgs; };
in [
goreleaser
go_1_19
Expand All @@ -75,8 +46,8 @@
libiconv
bash-completion
# local definition (see above)
renamed_grpc_tools
ts_protoc
grpc-tools-node
protoc-gen-ts
];

shellHook = ''
Expand Down
14 changes: 14 additions & 0 deletions nix-pkgs/grpc-tools-node.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
stdenv.mkDerivation {
name = "grpc-tools-node";
version = "0.1";
phases = [ "installPhase" ];
# For some reason, Node gRPC has its own 'protoc' binary
installPhase = ''
mkdir -p $out/bin
cp -r ${protobuf}/include/ $out/bin/
cp "${grpc-tools}/bin/protoc" $out/bin/grpc_tools_node_protoc
cp "${grpc-tools}/bin/grpc_node_plugin" $out/bin/grpc_tools_node_protoc_plugin
'';
}
148 changes: 148 additions & 0 deletions nix-pkgs/protoc-gen-ts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
pkg_name = "protoc-gen-ts";
pkg_version = "0.8.7";

# Using fixed output derivation so we can fetch things from outside
# and to avoid messing with Bazel's hermetic deps (mostly nodejs)
# we'll build it direclty using using Nix's nodejs. Very very hack solution
# after countless hours fighting Bazel deps vs nix sandbox. Maybe there a way easer
# way to tell bazel to use external nodejs but I couldn't figure it out.
# It seems the upstream repo is moving away from Bazel into Cargo.
deps = stdenv.mkDerivation rec {
name = "${pkg_name}-build";

src = fetchFromGitHub {
owner = "thesayyn";
repo = pkg_name;
rev = pkg_version;
hash = "sha256-PGprtSPMRTodt/SD6gpEr/n22jiNqB1/C6HJGlDndLg=";
};

buildInputs = [ git cacert nodejs ];

# Setup TS/Node config files directly into the src repo
tsconfig_json = pkgs.writeText "tsconfig_json" ''
{
"compilerOptions": {
"target": "ES2020",
"module": "EsNext",
"moduleResolution": "node",
"outDir": "./.build",
"noImplicitAny": true
}
}
'';

package_json = pkgs.writeText "package_json" ''
{
"name": "example-node-nix",
"version": "1.0.0",
"main": "dist/index.js",
"bin": {
"example-node-nix": "dist/index.js"
},
"scripts": {
"build": "tsc -p tsconfig.json && rollup -c rollup.config.js",
"start": "node dist/index.js"
},
"dependencies": {
"@grpc/grpc-js": "^1.7.3",
"@types/google-protobuf": "^3.15.5",
"google-protobuf": "^3.19.1"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.1.5",
"@tsconfig/node16": "^1.0.1",
"@tsconfig/node16-strictest": "^1.0.0",
"@types/node": "^18.7.14",
"rollup": "<3.0.0",
"typescript": "^4.8.2",
"tslib": "^2.6.2"
}
}
'';

rollup_config_js = pkgs.writeText "rollup_config_js" ''
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import fs from "node:fs";

const executable = () => {
return {
name: 'executable',
writeBundle: (options) => {
fs.chmodSync(options.file, '755');
},
};
};

export default {
input: "./index.ts",
output: {
file: '.bin/protoc-gen-ts.js',
format: 'cjs'
},
plugins: [
typescript(),
nodeResolve(),
commonjs(),
executable()
],
onwarn(message, warn) {
if (message.code === "EVAL" || message.code == "THIS_IS_UNDEFINED") {
return;
}
warn(message);
}
}
'';

buildPhase = ''
cp ${package_json} src/package.json
cp ${rollup_config_js} src/rollup.config.js
cp ${tsconfig_json} src/tsconfig.json
cd src

echo ">> Create .tmp folder as Nix sandbox are homeless and nodejs needs one"
mkdir -p .tmp
export HOME=$(pwd)/.tmp

echo ">> Intsalling TS/JS/nodejs deps"
npm install

echo ">> Patching npm's TS and RollUp scripts to work on Nix"
patchShebangs --build -- node_modules/typescript/bin/tsc
patchShebangs --build -- node_modules/rollup/dist/bin/rollup

echo ">> Building"
npm run build
'';

installPhase = ''
mkdir -p $out/bin
cp .bin/protoc-gen-ts.js $out/bin/protoc-gen-ts
'';

outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = "sha256-cIIFpEXlg/UogK4QskIeWFDuXReFDBKokddTB6juTZo=";
};

in stdenv.mkDerivation rec {
name = pkg_name;
version = pkg_version;
phases = [ "installPhase" ];

# Fixed Output derivation can't have Nix references (paths /nix/...) on it
# and we need to add it on this second (and normal) derivation. We're adding
# a shebang to call nodejs in the compiled script.
installPhase = ''
mkdir -p $out/bin
echo "$(echo '#!${nodejs}/bin/node' && cat '${deps}/bin/protoc-gen-ts')" > $out/bin/protoc-gen-ts
'';
}