nixpacks icon indicating copy to clipboard operation
nixpacks copied to clipboard

First-class support for nrwl/nx and applications within such a monorepo

Open ChristianIvicevic opened this issue 2 years ago • 16 comments

Feature request

I am hosting a bigger application that lives within a monorepo managed by nx. Currently I want to selectively deploy services living in the apps folder. As a workaround I have configured the variables NIXPACKS_BUILD_CMD and NIXPACKS_START_CMD for each of my services to point it to the correct commands and start commands to run from within the monorepo. Those commands are npx nx build <app-name> --prod and node dist/apps/<app-name>/main.js respectively.

Some form of configuration to specifically build and run applications by name without the need to duplicate those custom commands would be convenient.

Motivation

No response

Contribution

No response

ChristianIvicevic avatar Jul 29 '22 17:07 ChristianIvicevic

What type of config would be ideal for you? Maybe something like NIXPACKS_NX_APP? Where setting that variable would correctly configure the build and start commands.

coffee-cup avatar Jul 29 '22 17:07 coffee-cup

I think NX has a default app field, so we could build that by default if we detect NX.

Milo123459 avatar Jul 29 '22 17:07 Milo123459

I am not sure how relevant the UI of Railway itself is, but entering the information there would be most convenient.

Disregarding the UI, I have to confess that defining a NIXPACKS_NX_APP or NIXPACKS_BUILD_CMD is barely any difference. Furthermore it's important to keep in mind that the build commands should be applicable to all kinds of applications as nx build just delegates that and start commands can vary significantly, depending on the app itself. I haven't seen a specific nx command to run the production build from the dist folder yet.

ChristianIvicevic avatar Jul 29 '22 18:07 ChristianIvicevic

I am not sure how relevant the UI of Railway itself is, but entering the information there would be most convenient.

Yes this is a very good point. While this Nixpacks repo is just related to the nixpacks build tool itself, we are planning on moving a lot of these settings into the Railway UI at one point.

coffee-cup avatar Jul 29 '22 18:07 coffee-cup

Is there something unique about nrwl/nx that can help us identify automatically?

AhmedMozaly avatar Jul 29 '22 19:07 AhmedMozaly

I religiously use NX and was planning to make a proof of concept integration with nixpacks, but it looks like I was too slow :)!

Couple things of note that came to my head, if the app name is known the builder could parse the nx.json or relevant project.json for that app and extract the dist path.

Second thing, I imagine nextjs apps are a common use case when working with NX. When building a nextjs app with nx on vercel the dist dir has to manually be set to dist/apps/<app-name>/.next so something like this might be needed.

Thirdly, the output or dist dir can be set for (it looks like) all the common builders, but I don't know how much value that would provide instead of just parsing the project config and running with the path in that.

Lastly, it might be quite common that an NX repo won't contain a default app providing it's mono repo with lots of apps. Regarding the integration with railway could it take the service name and use that as the app name?

Re Ahmed: iirc running nx build will default to the default app and error if it can't work out which app is the default app.

(This comment may be scuffed, it's like 12 and I'm on my phone 🙈)

edit: Got nixpacks working with nx here but I think I want to play around with it more, regarding different app types and just different project configurations as there's a wide variety of valid project configs.

FinnDore avatar Jul 29 '22 22:07 FinnDore

@FinnDore After troubleshooting some issues with deploying an nx monorepo to Railway I noticed that the Nx Daemon is active and having issues with the cache it creates causing every other build to fail during the build phase with a SIGTERM. Out of desperation I set NX_DAEMON=false and all of a sudden all builds are passing without issues now.

I'll be reporting that as I don't think the daemon should be enabled in CI at all to see what's going on here.

ChristianIvicevic avatar Aug 01 '22 13:08 ChristianIvicevic

Oh interesting, I've not seen that issue personally. But thanks for the heads up. I'll adds that to my list of things to look into

FinnDore avatar Aug 01 '22 13:08 FinnDore

@ChristianIvicevic Once the app is built what do you think is the best way to generate the command to run the app? For a simple express app it's just this: image

For an angular spa, you would have to host the build output on something like an nginx server. and for a nextjs app you would take the .next dir and do something else with that.

Should it default to node <ouputPath>/main.js? but also be changeable via env var?

FinnDore avatar Aug 01 '22 17:08 FinnDore

The default that you propose catches all common Node-based applications, including express, Nest.js etc. Anything beyond that is most likely one of the common SPAs as you said.

I am personally not hosting those on Railway and using Vercel instead since I am using Next.js anyways. Not sure about other users of the platform. To be fair I was made aware of Railway via a message "if lambdas on Vercel running as a backend for your application aren't enough go use Railway" implying to use it for microservices etc. thus I'd be inclined to have the default be that which you propose as it fits my personal use-case.

Monorepos are an exception anyways since the design of nixpacks is to be used configuration-free as much as possible which just doesn't work in monorepos obviously...

ChristianIvicevic avatar Aug 01 '22 17:08 ChristianIvicevic

@Milo123459 @coffee-cup I am going slightly offtopic here, but I have reported the issue with the cache and daemon in the official repository with a repro for you folks so you can debug on Railway itself which I cannot: https://github.com/nrwl/nx/issues/11395

My gut-feeling, without knowing ANY details whatsoever, is that certain cache folders produced by nx within node_modules/.cache should not be cached. Especially with Nx Cloud those would get repopulated correctly, I'd assume.

ChristianIvicevic avatar Aug 01 '22 19:08 ChristianIvicevic

I've implemented most of the above on this branch. It works for nextjs apps, express apps and apps that build to an index.js. I just need to work out the unit tests for next apps. Once that's done I might ask @ChristianIvicevic to have a little play with the changes to see if they work well with a variety of nx repos if that's ok? Then I can submit a pr if its a feature nixpacks would want.

FinnDore avatar Aug 07 '22 11:08 FinnDore

Hey! That's fantastic news!

Would you mind creating a draft PR with your changes as of now? Just want to conduct some official reviews before you push forward.

Milo123459 avatar Aug 07 '22 11:08 Milo123459

Oh yea no worries. 1 sec

edit: https://github.com/railwayapp/nixpacks/pull/387

FinnDore avatar Aug 07 '22 11:08 FinnDore

Thanks for the work on this @FinnDore !

Appologizes for the late response on this. I'll leave any comments/suggestions I have in the PR

coffee-cup avatar Aug 08 '22 18:08 coffee-cup

Thanks 😀. You're good no worries!

FinnDore avatar Aug 08 '22 21:08 FinnDore

Completed in https://github.com/railwayapp/nixpacks/pull/387

coffee-cup avatar Aug 17 '22 19:08 coffee-cup