Use Nix with Node@v20 & Yarn@4.x in nixpacks.toml and railway.toml

xiroiTRIAL

6 months ago

Hi! 👋 I'm trying to configure a Node.js project with Nix on Railway. I have both railway.toml and nixpacks.toml files set up, but I'm not sure if I'm correctly defining the Nix config path. Here's my current setup:

railway.toml:

[build]
builder = "nixpacks"
nixpacksConfigPath = "nixpacks.toml"
buildCommand = "yarn workspace @my-app build"
watchPatterns = [
    "apps/web/src/**",
    "apps/web/vite.config.mts",
    "packages/**"
]

[deploy]
startCommand = "yarn workspace @my-app preview"
healthcheckPath = "/"
healthcheckTimeout = 180
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

[environments.staging.services.environment]
NODE_ENV = "production"
VITE_DATABASE_NAME = "database_staging"
VITE_DEBUG_ENABLED = "false"
VITE_HOST_NAME = "https://example.com"
VITE_SYNC_URL = "https://sync.example.com"
VITE_SERVER_HOST_NAME = "https://api.example.com"

[environments.staging.variables]
NODE_VERSION = "20"
YARN_VERSION = "4.3.1"

nixpacks.toml:

[phases.setup]
nixPkgs = ['nodejs@20', 'yarn']

[variables]
NODE_VERSION = "20"
YARN_VERSION = "4.3.1"

[phases.install]
cmds = [
    "corepack enable",
    "corepack prepare yarn@4.3.1 --activate",
    "yarn install"
]

[phases.build]
cmds = ["yarn workspace @my-app build"]

Is this the correct way to specify the Nix configuration path in railway.toml? And is my nixpacks.toml set up correctly for a Node.js/Yarn project? Any guidance would be appreciated! 🙏

P.S. I already read https://docs.railway.app/guides/config-as-code

0 Replies

xiroiTRIAL

6 months ago

cba0b597-8dda-49ec-abd1-d50886e024be


xiroiTRIAL

6 months ago

Use Nix with Node@v20 & Yarn@4.x in nixpacks.toml and railway.toml


xiroiTRIAL

6 months ago

Railway logs:

#7 [ 3/11] COPY .nixpacks/nixpkgs-*.nix .nixpacks/nixpkgs-*.nix
#7 DONE 0.2s

#8 [ 4/11] RUN nix-env -if .nixpacks/nixpkgs-*.nix && nix-collect-garbage -d
#8 0.122 error: syntax error, unexpected '@'
#8 0.122        at /app/.nixpacks/nixpkgs-*.nix:19:15:
#8 0.122            18|         '')
#8 0.122            19|         nodejs@20 yarn
#8 0.122              |               ^
#8 0.122            20|       ];

#8 ERROR: process "/bin/bash -ol pipefail -c nix-env -if .nixpacks/nixpkgs-*.nix && nix-collect-garbage -d" did not complete successfully: exit code: 1
-----
> [ 4/11] RUN nix-env -if .nixpacks/nixpkgs-*.nix && nix-collect-garbage -d:
0.122 error: syntax error, unexpected '@'
0.122        at /app/.nixpacks/nixpkgs-*.nix:19:15:
0.122            18|         '')
0.122            19|         nodejs@20 yarn
0.122              |               ^
0.122            20|       ];
-----

Dockerfile:8
-------------------
6 |
7 |     COPY .nixpacks/nixpkgs-*.nix .nixpacks/nixpkgs-*.nix
8 | >>> RUN nix-env -if .nixpacks/nixpkgs-*.nix && nix-collect-garbage -d
9 |
10 |
-------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c nix-env -if .nixpacks/nixpkgs-*.nix && nix-collect-garbage -d" did not complete successfully: exit code: 1

Error: Docker build failed

6 months ago

Hello,

yes nixpacksConfigPath is correct, but its also redundant since that is the default value.

You also cant set environment variables in a railway.toml file, you would want to set them on your service in the UI.

nodejs@20 is not a valid package.

nixPkgs = ['nodejs@20', 'yarn'] is not needed, node 20 will be used automatically if your engines.node field in the package.json specifies it, and yarn will be used if you have a yarn lock file.

[phases.install]
cmds = [
    "corepack enable",
    "corepack prepare yarn@4.3.1 --activate",
    "yarn install"
]

Corepack will install the set version of yarn for you if you have it set in the packageManager field in the package.json

[variables]
NODE_VERSION = "20"
YARN_VERSION = "4.3.1"

Doesn't do anything

[phases.build]
cmds = ["yarn workspace @my-app build"]

You already set this in your railway.toml


6 months ago

with all that said -

  • set the engines.node field in your package.json to 20

  • set the packageManager field in your package.json to yarn@4.3.1

  • remove the nixpacks.toml file, it is redundant in this context.

  • set the needed variables in the service variables tab.

  • use this railway.toml file -

[build]
builder = "nixpacks"
buildCommand = "yarn workspace @my-app build"
watchPatterns = [
    "apps/web/src/**",
    "apps/web/vite.config.mts",
    "packages/**"
]

[deploy]
startCommand = "yarn workspace @my-app preview"
healthcheckPath = "/"
healthcheckTimeout = 180
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

xiroiTRIAL

6 months ago

How can I split railway.toml for different apps of my monorepo (like web and server)?

The docs recommend to use railway.toml in root directory, I was thinking to put in apps/web/ and apps/server


xiroiTRIAL

6 months ago

Thanks Bordy for the detailed explanation!!


xiroiTRIAL

6 months ago

With your suggestion, I got this:

#9 [5/7] COPY . /app/.

#9 DONE 1.6s


#10 [6/7] RUN  yarn workspace @xiroi/apps-web build
#10 0.129 /bin/bash: line 1: yarn: command not found

#10 ERROR: process "/bin/bash -ol pipefail -c yarn workspace @xiroi/apps-web build" did not complete successfully: exit code: 127
-----
> [6/7] RUN  yarn workspace @xiroi/apps-web build:
0.129 /bin/bash: line 1: yarn: command not found
-----

Dockerfile:16
-------------------
14 |     # build phase
15 |     COPY . /app/.
16 | >>> RUN  yarn workspace @xiroi/apps-web build
17 |
18 |     # setup phase
-------------------

ERROR: failed to solve: process "/bin/bash -ol pipefail -c yarn workspace @xiroi/apps-web build" did not complete successfully: exit code: 127

Error: Docker build failed

xiroiTRIAL

6 months ago

My settings:

[build]
builder = "nixpacks"
buildCommand = "yarn workspace @xiroi/apps-web build"
# watchPatterns = [
#    "xiroi-apps/web/**",
#    "xiroi-packages/**"
# ]

[deploy]
startCommand = "yarn workspace @xiroi/apps-web preview"
healthcheckPath = "/"
healthcheckTimeout = 180
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

6 months ago

isolated or shared monorepo?


6 months ago

do you not have a yarn lock file?


xiroiTRIAL

6 months ago

I have it. It's already in git


xiroiTRIAL

6 months ago

Shared. My root package.json looks like:

"workspaces": [
    "xiroi-apps/*",
    "xiroi-packages/bc/commerce/*",
    "xiroi-packages/bc/communication/*",
    "xiroi-packages/bc/communication/infras,
...
    "xiroi-packages/shared/*",
    "xiroi-packages/shared/infrastructure/*",
    "xiroi-packages/shared/ui/*",
    "xiroi-packages/shared/usecases/*",
    "xiroi-packages/shared/utils/*",
    "xiroi-packages/ui-pages/*"
]

6 months ago

for shared -- you wouldn't want to be setting a root directly in the service settings


6 months ago

put each railway.toml in the subdirectory though, and then set the location with the Railway Config File setting in the service settings


xiroiTRIAL

6 months ago

To my xiroi-apps/web/package.json, I added:

    "engines": {
        "node": ">=20",
        "npm": "please-use-yarn",
        "yarn": ">=4.1.0"
    },

Then xiroi-apps/web/railway.toml is:

[build]
builder = "nixpacks"
buildCommand = "yarn build"

[deploy]
startCommand = "yarn preview"
healthcheckPath = "/"
healthcheckTimeout = 180
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

Logs error:

6 DONE 1.6s

#7 [3/5] COPY . /app/.

#7 DONE 1.2s


#8 [4/5] RUN  yarn build

#8 0.209 /bin/bash: line 1: yarn: command not found

#8 ERROR: process "/bin/bash -ol pipefail -c yarn build" did not complete successfully: exit code: 127
-----
> [4/5] RUN  yarn build:
0.209 /bin/bash: line 1: yarn: command not found
-----

Dockerfile:15
-------------------
13 |     # build phase
14 |     COPY . /app/.
15 | >>> RUN  yarn build
16 |
17 |
-------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c yarn build" did not complete successfully: exit code: 127

Error: Docker build failed

xiroiTRIAL

6 months ago

P.S. My intention is to use a paid plan of Railway when I launch my web & app.


6 months ago

thats great, i hope we can get you up and running, but for that i would greatly appreciate if you made sure to read the information im giving you 🙂


6 months ago

^


xiroiTRIAL

6 months ago

1299104686833668000


6 months ago

1299104762482004000


xiroiTRIAL

6 months ago

Ohhh, it's below Source Repo. I missed the underlined text 😁

1299105242138546200


xiroiTRIAL

6 months ago

No success. This is my "web" service settings:

  • Root directory: /xiroi-apps/web

  • Railway config file: xiroi-apps/web/railway.toml

Logs:

#8 [4/5] RUN  yarn build
#8 0.081 /bin/bash: line 1: yarn: command not found

#8 ERROR: process "/bin/bash -ol pipefail -c yarn build" did not complete successfully: exit code: 127
-----
> [4/5] RUN  yarn build:

0.081 /bin/bash: line 1: yarn: command not found
-----

Dockerfile:15
-------------------
13 |     # build phase
14 |     COPY . /app/.
15 | >>> RUN  yarn build
16 |
17 |
-------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c yarn build" did not complete successfully: exit code: 127

Error: Docker build failed

6 months ago

you wouldn't want to be setting a root directly in the service settings


xiroiTRIAL

6 months ago

hahah I'm little confused. My initial config hadn't the root directory in the Web service.
My current config is Railway Config File xiroi-apps/web/railway.toml. I still cannot build it.

*I'm skipping Server service for now.

1299108970727735300


6 months ago

would you mind if i pull your code, it will help me to understand what you have going on


xiroiTRIAL

6 months ago

Yeah, no problem. Let me know what you need for that


6 months ago

your permission is sufficient


6 months ago

in all this messing about somehow the providers option got emptied, add node back to it

1299110044620558300


xiroiTRIAL

6 months ago

Initially I deleted it because my (incorrect) Nix config


6 months ago

"npm": "please-use-yarn" lol


xiroiTRIAL

6 months ago

Great Brody!
Everything is building, except for one monorepo package. I'll dig into that one.


xiroiTRIAL

6 months ago

The last 1-2 years is quite common to use monorepos. I'd add cases to https://docs.railway.app/tutorials/deploying-a-monorepo for people


6 months ago

easier said than done unfortunately, your issues are incredibly specific to what you have already tried


xiroiTRIAL

6 months ago

😅
I didn't know I was unique 😁


6 months ago

your current build command is yarn build and your start command is yarn start shouldnt you be using workspace specific commands?


xiroiTRIAL

6 months ago

Because railway.toml is already in the frontend app (xiroi-apps/web/railway.toml with package.json's name @xiroi/apps-web), I guess it runs from the same directory xiroi-apps/web, instead of yarn workspace @xiroi/apps-web build that I run from the root directory.


6 months ago

your root directory is / thus the scripts from the root directory are used.


6 months ago

you want to be in the root directory, but you need to be using workspace specific build and start commands


xiroiTRIAL

6 months ago

That was it.
The build was successful, Brody!

For the deployment, what Railway config do you recommend?

[deploy]
startCommand = "yarn workspace @xiroi/apps-web preview"
healthcheckPath = "/"
healthcheckTimeout = 180
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

Logs

=== Successfully Built! ===

Run:
docker run -it us-west1.registry.rlwy.net/96a41a31-4b99-4571-9d8f-a0ea733a7fb2:42ff8b32-5ea2-4e0b-a559-8abcce8c5be3

Build time: 215.07 seconds

====================
Starting Healthcheck
====================
Path: /
Retry window: 3m0s

Attempt #1 failed with service unavailable. Continuing to retry for 2m49s
Attempt #2 failed with service unavailable. Continuing to retry for 2m38s
...

1/1 replicas never became healthy!
Healthcheck failed!

6 months ago

lemme look


6 months ago

well the deploy failed -

Error bundling tamagui config: The service was stopped: write EPIPE (run with DEBUG=tamagui to see stack)
No bundled config generated, maybe an error in bundling. Set DEBUG=tamagui and re-run to get logs.


6 months ago

and now you will need this -


xiroiTRIAL

6 months ago

Great!
I'm searching where UI library Tamagui breaks


6 months ago

just noticed you are running a dev server via vite


xiroiTRIAL

6 months ago

In /web?


6 months ago

yeah


xiroiTRIAL

6 months ago

The first time hosting with Vite. server field is for local development, you run it with vite. preview fields with vite preview


6 months ago

question, are you fixed on the .toml file, or can we move to a .json file? you would get a much better dx with a railway.json file since it has the schema


6 months ago

we will need to use caddy, ill get you setup, just working out a plan of attack


xiroiTRIAL

6 months ago

Ohhh cool, .toml is done.


6 months ago

you got it, give me a few mins and ill come up with a plan to get you running with a production web server


xiroiTRIAL

6 months ago

About the Tamagui issue. I can build and run the web locally with vite preview on [http://localhost:4173](http://localhost:4173).


6 months ago

i dont think we need to worry about that, that error does not appear in the build logs


6 months ago

and its not going to appear in the deploy logs after we only have caddy running


xiroiTRIAL

6 months ago

The first time I heard Caddy. The marketing website https://caddyserver.com/ is going high 😎

1299149144811769900


6 months ago

we wont be using most of its features, railway handles https for us, we just need a user friendly http server


xiroiTRIAL

6 months ago

A note: my web can work without my server, only needs Supabase (auth) and Powersync (db sync) connections. The rest is a local-first approach.


6 months ago

sounds good


6 months ago

does your frontend depend on another workspace other than shared-utils-logging ?


xiroiTRIAL

6 months ago

Ohh, good finding. "build": "yarn workspace @xiroi/shared-utils-logging build && tsc && vite build" is a past config. I gonna replace it for tsc && vite build


6 months ago

so is the frontend just depending on types from other packages?


xiroiTRIAL

6 months ago

Yes


xiroiTRIAL

6 months ago

Pushed the commit about deletion of this


6 months ago

this nixpacks stuff really annoying, how do you feel about a dockerfile?


xiroiTRIAL

6 months ago

Very comfortable. nixpacks' packages were really difficult when I experimented with them


xiroiTRIAL

6 months ago

No docker compose, only dockerfile?


6 months ago

only Dockerfile, ill write one for you


6 months ago

sorry this is taking so long, got it mostly running but yarn is still running in the background for some reason



6 months ago

you'll also need to change the config file path to use the .json extension now


6 months ago

this going from yarn running caddy, to just caddy when i switched to the dockerfile, ~250mb before

1299169253546655700


xiroiTRIAL

6 months ago

250 mb before? With this new config, it will be smaller and faster to build?


xiroiTRIAL

6 months ago

Good job! Now build time reduced from +3min to 2min


6 months ago

yeah faster builds since less nixpacks stuff, and faster deploys, since now the final image basically only contains the caddy binary and your dist folder


xiroiTRIAL

6 months ago

What do you think is going wrong https://web-staging-4672.up.railway.app/ ?


6 months ago

you would likely have a better idea than me -

vendor_react_native_web-Rn6azkte.js:1 Uncaught (in promise) TypeError: ss is not a function
    at vendor_react_native_web-Rn6azkte.js:1:9786

xiroiTRIAL

6 months ago

This happened in the localhost and just needed to refresh in a new tab… But apparently not working in production


xiroiTRIAL

6 months ago

I mark this ticket as SOLVED. Amazing help, Brody!


xiroiTRIAL

6 months ago

Say to the team that you're providing next level support 🪄


6 months ago

thank you, hopefully this support is worth an upgrade to Pro? 🙂


xiroiTRIAL

6 months ago

I'll see the demand the first months of the launch next month, and adjust the paid plan accordingly. I confidence I can grow with Railway after exploring many options.


xiroiTRIAL

6 months ago

And because it's a consumer web & app, hopefully the demand will high


6 months ago

i wish you the best of luck then!