This is a thin wrapper around
node2nix
that creates a node
environment with the dependencies defined in package-lock.json
. It provides
convenience (not having to keep several .nix files up-to-date with
package.json
) at the expense of longer build times.
The package provides an overlay with a single function mkNodeModules
, that
takes the following arguments:
- src: the path to your project. Usually
./.
- node2nix (optional):
node2nix
package to use. Defaults topkgs.nodePackages.node2nix
- nodejs (optional):
nodejs
package to use. Defaults topkgs.nodePackages.nodejs-16_x
- fixNodeGyp (default:
false
): set totrue
if you’re having an installation error with node-gyp. See this issue for more information
Assuming you have a project with a package.json
and package-lock.json
, you can use the following:
{
inputs.mk-node.url = "github:sephii/mk-node";
outputs = { self, nixpkgs, mk-node }:
let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; overlays = [ mk-node.overlay ]; };
nodejs = pkgs.nodejs-16_x;
nodeModules = pkgs.mkNodeModules { src = ./.; inherit nodejs; };
in {
# Include anything else you need for your derivation (eg. use `buildPythonApplication`, `mkPoetryApplication`, etc)
defaultPackage.${system} = stdenv.mkDerivation {
buildPhase = ''
ln -s ${nodeModules}/lib/node_modules ./node_modules
export PATH="${nodeModules}/bin:$PATH"
${nodejs}/bin/npm run build
rm -rf ./node_modules
'';
};
};
}