Building a sveltekit app with nix

I’m trying to build a svelte app with nix and mkYarnPackage:

npm init svelte@next web
yarn install
rm -rf node_modules

default.nix

{ pkgs ? import <nixpkgs> {}, ... }:
{
  web = pkgs.mkYarnPackage rec {
    name = "web-${version}";
    version = "0.0.1";

    src = ./.;
    packageJson = "${src}/package.json";
    yarnLock = "${src}/yarn.lock";

    installPhase = ''
      yarn --offline build

      mkdir $out
      mv build $out
    '';
    distPhase = "true";
  };
}

Out of the box this doesn’t work claiming it can’t find svelte-kit.js:

❯ nix build
builder for '/nix/store/q9kk5dm3dci5cvmqs6qv105w534wbls1-web-0.0.1.drv' failed with exit code 127; last 10 log lines:
  installing
  yarn run v1.22.10
  warning package.json: No license field
  warning Skipping preferred cache folder "/homeless-shelter/.cache/yarn" because it is not writable.
  warning Selected the next writable cache folder in the list, will be "/build/.yarn-cache-1000".
  $ svelte-kit.js build
  warning Cannot find a suitable global folder. Tried these: "/usr/local, /homeless-shelter/.yarn"
  /bin/sh: svelte-kit.js: not found
  error Command failed with exit code 127.
  info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[2 built (1 failed), 0.0 MiB DL]
error: build of '/nix/store/q9kk5dm3dci5cvmqs6qv105w534wbls1-web-0.0.1.drv' failed

If I update the package.json to point to the node_module directory for svelte-kit.js then I get this:

❯ nix build
builder for '/nix/store/y8pcrdfdwavmcwb7anp7m0gnapmjzbs2-web-0.0.1.drv' failed with exit code 1; last 10 log lines:
  > The service was stopped
  Error: The service was stopped
      at /build/web/deps/web/node_modules/esbuild/lib/main.js:1066:33
      at /build/web/deps/web/node_modules/esbuild/lib/main.js:568:9
      at Socket.afterClose (/build/web/deps/web/node_modules/esbuild/lib/main.js:546:7)
      at Socket.emit (events.js:327:22)
      at endReadableNT (internal/streams/readable.js:1327:12)
      at processTicksAndRejections (internal/process/task_queues.js:80:21)
  error Command failed with exit code 1.
  info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[2 built (1 failed), 0.0 MiB DL]
error: build of '/nix/store/y8pcrdfdwavmcwb7anp7m0gnapmjzbs2-web-0.0.1.drv' failed

yarn install && yarn build on the command line works; I’m using niv for dependencies so the yarn on my command line and derivation are the same. I’m not sure why this isn’t working. Do I need to create a nodePackage for the svelte-kit.js binary?

Finally had time to figure this out. Problem was that esbuild wasn’t installing because of the --ignore-scripts flag (which is correct). Instead I had to link to the nix version of esbuild (distributed in nixpkgs):

{
  web = pkgs.mkYarnPackage rec {
    name = "web-${version}";
    version = "0.0.1";

    src = ./.;

    configurePhase = ''
      for localDir in build node_modules; do
        if [[ -d $localDir || -L $localDir ]]; then
          echo "$localDir dir present. Removing."
          rm -rf $localDir
        fi
      done

      cp -r $node_modules node_modules
      chmod -R +w node_modules
      ln -sf ${esbuild}/bin/esbuild node_modules/esbuild/bin/esbuild
      ln -sf ${esbuild}/bin/esbuild node_modules/esbuild-linux-64/bin/esbuild
    '';
    installPhase = ''
      yarn --offline --frozen-lockfile build
      mkdir $out
      mv build/* $out
    '';
    distPhase = "true";
  };
}
1 Like

I bumped into esbuild issue when I tried to build a node app with Nix like this:

error: builder for '/nix/store/743s3jz4r06f7hg73s95wn3z168zi3y7-pdf-service-0.0.0.drv' failed with exit code 1;
       last 10 log lines:
       > npm ERR! errno 1
       > npm ERR! esbuild@0.11.23 postinstall: `node install.js`
       > npm ERR! Exit status 1
       > npm ERR!
       > npm ERR! Failed at the esbuild@0.11.23 postinstall script.
       > npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
       >
       > npm ERR! A complete log of this run can be found in:
       > npm ERR!     /build/.npm/_logs/2021-12-29T09_23_16_845Z-debug.log
       >
       For full logs, run 'nix log /nix/store/743s3jz4r06f7hg73s95wn3z168zi3y7-pdf-service-0.0.0.drv'.
error: 1 dependencies of derivation '/nix/store/f3q945ana9n609pal9xap1mi36wv5n61-portfolio-pdf-service-1.0.0.drv' failed to build

So seems like I found the answer from above, I should be using --ignore-scripts for npm install, but is there any other alternative to handle this automatically? Right now is doable but if there are some more automation it would be nice?