Skip to content

Conversation

tegefaulkes
Copy link
Contributor

@tegefaulkes tegefaulkes commented Jul 21, 2021

Description

This PR is to test using vercel/pkg with native modules.
It imports the following node modules:

  • level
  • utp-native
  • threads.js

It should be able to build via pkg and nix-build method.

Issues Fixed

Tasks

  • import, build and test level
  • import, build and test utp-native
  • import, build and test threads

Final checklist

  • import and create hello world example for:
    • level
    • utp-native
    • threads
  • build and test via pkg
    • level
    • utp-native
    • threads
  • build and test via nix-build
    • level
    • utp-native
    • threads
  • modify release.nix to include the required prebuilds in the result.
  • final tests of the nix builds. see if they include prebuilds properly.
  • test if GH auto release still works.

Related issues and PRs:

@tegefaulkes
Copy link
Contributor Author

Completed most of the checklist, The last thing to do is check of the GH releases works properly with the changes.
Depending on the output we may have to zip the builds now since they are directories instead of just the executable.

release.nix Outdated
@@ -59,13 +65,16 @@ let
PKG_IGNORE_TAG = 1;
buildPhase = ''
cp ${./package.json} package.json
mkdir -p output/prebuilds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need output/prebuilds? Why not just ./prebuilds? The ./ is the working directory of the build. As long as this directory doesn't conflict with anything in the ./ which I believe is the produced source files from the upstream derivations.

Remember this is an overridden derivation, the $src has already unpacked into the current directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use ./ instead of a temp directory, the result included all of the src files.
I also feel that creating and using a temp directory makes the process more explicit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that because of:

--public-packages "*" \

Is the * picking up everything?

Copy link
Member

@CMCDragonkai CMCDragonkai Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you must use a a special directory for this. I'd say use pkg as the special directory for vercel/pkg related building. Not output which is confusing with $out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can change it to pkg.
as for --public-packages "*" \ is just flagging everything is a public package since non public packages are included as bytecode by default.. This breaks with the --no-bytecode flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using pkg 5.3.1 doesn't fix the issue for leveldown prebuilds, neither does adding them as assets.
this seems to be an issue with pkg itself, I've posted an issue at vercel/pkg#1264.

release.nix Outdated
'';
dontFixup = true;
};
buildExe = arch:
stdenv.mkDerivation rec {
name = "${utils.basename}-${version}-win32-${arch}.exe";
name = "${utils.basename}-${version}-win32-${arch}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this? The name of the windows executable has an extension of .exe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output is a directory now that includes the .exe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should leave it as the actual executable. Is there other files necessary for the actual $out?

This is not a nix package, so the executable as $out is totally fine.

Plus this actually is integrated into the Gitlab CI/CD which expects a single file as $out. Changing it to a directory adds extra work to do there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have the output as a directory since we need to include the prebuilds for leveldown.
we can have it as a zip file if needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be resolved now back to the way it was?

@CMCDragonkai
Copy link
Member

Is there a way for you test the CI/CD execution of your changes.

You just need to go into the gitlab yaml file and enable this for your branch as well.

Then go to https://gitlab.com/MatrixAI/open-source/typescript-demo-lib/-/pipelines to manually trigger it. Or https://gitlab.com/MatrixAI/open-source/typescript-demo-lib/-/jobs.

@CMCDragonkai
Copy link
Member

Is there a way for you test the CI/CD execution of your changes.

You just need to go into the gitlab yaml file and enable this for your branch as well.

Then go to https://gitlab.com/MatrixAI/open-source/typescript-demo-lib/-/pipelines to manually trigger it. Or https://gitlab.com/MatrixAI/open-source/typescript-demo-lib/-/jobs.

Oh if you do this, make sure to switch it back later, we only want builds to occur on master, not on every branch.

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Jul 22, 2021 via email

@tegefaulkes tegefaulkes self-assigned this Jul 22, 2021
@CMCDragonkai
Copy link
Member

CMCDragonkai commented Jul 22, 2021 via email

@tegefaulkes
Copy link
Contributor Author

I have created an upstram PR addressing the issue with the leveldown warnings.
vercel/pkg#1273

@tegefaulkes
Copy link
Contributor Author

When this is done, the changes for js-polykey are not far behind. the only complication would be the jose/jwt throwing warnings. But I've added them as an asset and it seems to be working with manual testing e.g. starting agent + starting a session.

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Aug 3, 2021

I'm checking out your change to vercel/pkg now.

Firstly:

git fetch origin pull/1273/head:pr-1273
git checkout pr-1273
# setup nix-shell here
npm install
npm run build
# the build outputs to ./lib-es5, however the executable requires the node interpreter
node ./lib-es5/bin.js  --help

The output:


  pkg [options] <input>

  Options:

    -h, --help           output usage information
    -v, --version        output pkg version
    -t, --targets        comma-separated list of targets (see examples)
    -c, --config         package.json or any json file with top-level config
    --options            bake v8 options into executable to run with them on
    -o, --output         output file name or template for several files
    --out-path           path to save output one or more executables
    -d, --debug          show more information during packaging process [off]
    -b, --build          don't download prebuilt base binaries, build them
    --public             speed up and disclose the sources of top-level project
    --public-packages    force specified packages to be considered public
    --no-bytecode        skip bytecode generation and include source files as plain js
    -C, --compress       [default=None] compression algorithm = Brotli or GZip

  Examples:

  – Makes executables for Linux, macOS and Windows
    $ pkg index.js
  – Takes package.json from cwd and follows 'bin' entry
    $ pkg .
  – Makes executable for particular target machine
    $ pkg -t node14-win-arm64 index.js
  – Makes executables for target machines of your choice
    $ pkg -t node12-linux,node14-linux,node14-win index.js
  – Bakes '--expose-gc' and '--max-heap-size=34' into executable
    $ pkg --options "expose-gc,max-heap-size=34" index.js
  – Consider packageA and packageB to be public
    $ pkg --public-packages "packageA,packageB" index.js
  – Consider all packages to be public
    $ pkg --public-packages "*" index.js
  – Bakes '--expose-gc' into executable
    $ pkg --options expose-gc index.js
  – reduce size of the data packed inside the executable with GZip
    $ pkg --compress GZip index.js

I'm going to assume you manage to make it work.. so he main thing is that we need to get node2nix to work against your fork's branch.

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Aug 3, 2021

@tegefaulkes In the future for this sort of work you would want to create the forks under MatrixAI group in github rather than your personal account: https://github.com/tegefaulkes/pkg

It makes it easier for us to maintain forks and maintain overrides in our nixpkgs overlay.


So to start I'm going to fork from vercel/pkg and pull your branch in there https://github.com/MatrixAI/pkg.

@CMCDragonkai
Copy link
Member

The other really important thing is to always branch off from main or master when creating a PR. This way you can have a main that syncs up with upstream, while creating a feature branch to be PRed into upstream main.

So for now I'll just create a feature branch off main and do the same as what you did.

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Aug 3, 2021

This is now done here: https://github.com/MatrixAI/pkg/tree/leveldown

The steps were:

git remote add matrixai git@github.com:MatrixAI/pkg.git
git checkout main
git checkout -b leveldown
# make the change
git add -A
git commit -m 'Removes dictionary override for bundling leveldown - Fixes vercel#1264'
git push matrixai

So now:

»» ~/Projects/pkg
 ♜ git branch -vv                                                                                                           (leveldown) pts/3 22:21:32
  * leveldown 63367b0 Removes dictionary override for bundling leveldown - Fixes vercel#1264
    main      09c3f21 [origin/main] 5.3.1
    pr-1273   6ad2e53 Fixes #1264. Removes dictionary override for bundling leveldown. changes from PR #837 makes this unnecessary now.

This repository and branch will now be the target of our Nix override.

@CMCDragonkai
Copy link
Member

The nix override work in our overlay is located here: https://gitlab.com/MatrixAI/Engineering/nixpkgs-overlay/-/merge_requests/15

@tegefaulkes
Copy link
Contributor Author

Ok, should be working now. Last thing is to check if the github CI still works.

@CMCDragonkai
Copy link
Member

I don't think anything needs to change in our .gitlab-ci.yml file. It should work fine now.

So please fix up my comments, ensure any linting is done, and also clean up the README and format it and squash all the commits.

@tegefaulkes
Copy link
Contributor Author

Misclicked closed. My bad.

@tegefaulkes
Copy link
Contributor Author

tegefaulkes commented Aug 4, 2021

Failing on tests. I'll look it over and fix it up tomorrow.
after that it should be ready to merge.

@tegefaulkes
Copy link
Contributor Author

Checks are passing, looks like it's working. this should be good for a merge after a review. @CMCDragonkai

@tegefaulkes
Copy link
Contributor Author

Squashed.

@tegefaulkes tegefaulkes marked this pull request as ready for review August 5, 2021 04:24
@tegefaulkes
Copy link
Contributor Author

tegefaulkes commented Aug 5, 2021

That should be the last of it.

  • rebase
  • small fixes
  • squash
  • review
  • check pipeline succeeds
  • merge

@CMCDragonkai
Copy link
Member

CMCDragonkai commented Aug 5, 2021

Just fixed up a few things:

  • Removed useless comments from pkgs.nix
  • Formatted README.md with spacing between the code blocks and text
  • Restoring the mocks should happen at the end of each test
  • Indentation issues in release.nix
  • Cleaned up commit message

@CMCDragonkai
Copy link
Member

I noticed you also enabled source map for this repo. This is a good idea for a library. For a end-user application, not sure if it is relevant.

@tegefaulkes
Copy link
Contributor Author

I noticed you also enabled source map for this repo. This is a good idea for a library. For a end-user application, not sure if it is relevant.

Ah, jest was throwing some minor errors without it. It's not needed I think.

@CMCDragonkai
Copy link
Member

You should also always test: nix-build ./release.nix --attr application.

Also we now have a custom override for our pkg here, this would have to done for all the repos using pkg.

Until upstream merges. Our overlay can also have this change applied too in the mean time.

@CMCDragonkai
Copy link
Member

The application build is currently failing due to the missing node-gyp-build command. It needs to be available to the derivation. We've dealt with this before.

@CMCDragonkai
Copy link
Member

So it turns out that what's required is that:

     node2nixProd = (
  -    (import (node2nixDrv false) { inherit pkgs nodejs; }).shell.override {
  +    (import (node2nixDrv false) { inherit pkgs nodejs; }).shell.override (attrs: {
  +      buildInputs = attrs.buildInputs ++ [ nodePackages.node-gyp-build ];
         dontNpmInstall = true;
  -    }
  +    })

The override can also take a function which takes the attrs, originally this was not the case. And there was a difference between override and overrideAttrs.

Interestingly you cannot make this work if you use overrideAttrs.

@CMCDragonkai
Copy link
Member

Now I find that you need node-gyp-build for both node2nixProd and node2nixDev.

This makes sense as the node2nixProd will only install production dependencies, whereas node2nixDev will install both production and development dependencies. So if there's an override, both require the node-gyp-build to be available.

Now that override and overrideAttrs both take a function with attrs, I wonder if this is because override delegates to overrideAttrs if it's an attribute function. Because nixpkgs upstream says that override is meant to override the function parameters. But this doesn't make any sense if there is access to existing attributes.

@CMCDragonkai
Copy link
Member

Yea I checked override being able to take attrs and applying something like buildInputs is specific to node2nix.

@CMCDragonkai
Copy link
Member

Regarding override issues: svanderburg/node2nix#178

I've changed to using override instead of overrideAttrs for both node2nixDev and node2nixProd.

Regarding the build warnings of creating a homeless shelter that's cause pkg is attempting to download prebuilt-binaries but instead building from source should be possible.

This is solved with this environment variable in the release builds:

      # ensure that native modules are built from source
      npm_config_build_from_source = "true";

Regarding the application build, both dev and prod require the node-gyp-build due to leveldown. So this will be important for any future native modules we are adding in, the build system will become ever more complex. So it's ideal we document what each thing is for.


@tegefaulkes still has to fix up the executable itself, the arguments to ./result/bin/typescript-demo-lib should be all optional. I think some arguments were introduced by @emmacasolin before where there was no defaulting value for them. Right now not passing it the right set of args results in weird errors.

This is also important for the docker output which doesn't pass any parameters to the command. It will look like something is broken without this being fixed.

@CMCDragonkai CMCDragonkai added this to the Release Polykey CLI 1.0.0 milestone Aug 5, 2021
@CMCDragonkai CMCDragonkai added the development Standard development label Aug 5, 2021
@CMCDragonkai
Copy link
Member

My last commit ensures parity between nix-shell and nix-build. This should ensure that pkg being used inside nix-shell is the same as that which is used for the release. These changes will need to be applied to js-polykey too.

At the final point make sure to test all builds, application, docker, and the executables. And then merging time!

@CMCDragonkai CMCDragonkai merged commit 61885dc into master Aug 5, 2021
@CMCDragonkai CMCDragonkai deleted the pkg-integration branch August 5, 2021 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Standard development
Development

Successfully merging this pull request may close these issues.

Integrate a Node native module like utp-native or leveljs in order to demonstrate vercel/pkg
2 participants