Node.js: Difference between revisions
Add override nodejs app example |
Move packaging contents to "Packaging" section |
||
| Line 9: | Line 9: | ||
See <code>nix search nixpkgs nodejs</code> for additional versions like <code>nodejs-12_x</code>, etc. | See <code>nix search nixpkgs nodejs</code> for additional versions like <code>nodejs-12_x</code>, etc. | ||
== Packaging == | |||
=== Packaging with <code>buildNpmPackage</code> === | |||
From the [https://nixos.org/manual/nixpkgs/stable/#javascript-tool-specific Nixpkgs manual]: "<code>buildNpmPackage</code> allows you to package npm-based projects in Nixpkgs without the use of an auto-generated dependencies file (as used in node2nix). It works by utilizing npm’s cache functionality – creating a reproducible cache that contains the dependencies of a project, and pointing npm to it." | |||
'''To better understand what happens under the hood and see the latest features see''' | |||
https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/node/build-npm-package/default.nix | |||
==== Packaging electron applications ==== | |||
When trying to <code>npm i</code> electron, by default electron will try to download binaries from the internet, which does not work in the Nix sandbox. To stop it from trying to access the internet, set the environment variable <code>ELECTRON_SKIP_BINARY_DOWNLOAD = "1";</code>. | |||
For examples of packaging electron applications, search [https://github.com/search?q=buildNpmPackage+electron+repo%3ANixOS%2Fnixpkgs&type=code&l=NixOS%2Fnixpkgs Nixpkgs] for the terms "buildNpmPackage" and "electron". | |||
=== Packaging with <code>yarn2nix</code> === | |||
yarn2nix uses the yarn nodejs tool to create a file called yarn.lock, which in return can be used by yarn2nix to generate a usable yarn expression. | |||
This is what was needed to convert a small application server [https://git.shackspace.de/rz/muellshack/tree/3be09715911628b164fa1cf346387555ca26a5b1 shackspace muellshack]: | |||
<syntaxhighlight lang="console"> | |||
$ nix-shell -p yarn yarn2nix | |||
$ yarn install | |||
# creates yarn.lock | |||
$ yarn2nix > yarn.nix | |||
$ vim package.json | |||
# add: "bin": "app.js", | |||
$ cat > default.nix <<EOF | |||
with (import <nixpkgs> {}); | |||
rec { | |||
muellshack = mkYarnPackage { | |||
name = "muellshack"; | |||
src = ./.; | |||
packageJSON = ./package.json; | |||
yarnLock = ./yarn.lock; | |||
yarnNix = ./yarn.nix; | |||
}; | |||
} | |||
EOF | |||
$ sed -i '1i#!/usr/bin/env node' app.js | |||
$ chmod +x app.js | |||
$ nix-build | |||
$ result/bin/muellshack | |||
</syntaxhighlight> | |||
The complete diff can be found at [https://git.shackspace.de/rz/muellshack/commit/f5e498acd47695c4947dc1b5ddebfad2eee8d653 the respective diff] | |||
== Troubleshooting == | == Troubleshooting == | ||
| Line 176: | Line 221: | ||
== Tips and tricks == | == Tips and tricks == | ||
=== Example nix flake shell for Node.js development === | === Example nix flake shell for Node.js development === | ||
`flake.nix` example: | `flake.nix` example: | ||