1 { lib, stdenv, fetchurl, openssl, python, zlib, libuv, util-linux, http-parser, bash
2 , pkg-config, which, buildPackages
3 # for `.pkgs` attribute
6 , writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell
12 { enableNpm ? true, version, sha256, patches ? [] } @args:
15 inherit (darwin.apple_sdk.frameworks) CoreServices ApplicationServices;
17 isCross = stdenv.hostPlatform != stdenv.buildPlatform;
19 majorVersion = lib.versions.major version;
20 minorVersion = lib.versions.minor version;
22 pname = if enableNpm then "nodejs" else "nodejs-slim";
24 useSharedHttpParser = !stdenv.isDarwin && lib.versionOlder "${majorVersion}.${minorVersion}" "11.4";
26 sharedLibDeps = { inherit openssl zlib libuv; } // (lib.optionalAttrs useSharedHttpParser { inherit http-parser; });
28 sharedConfigureFlags = lib.concatMap (name: [
30 "--shared-${name}-libpath=${lib.getLib sharedLibDeps.${name}}/lib"
31 /** Closure notes: we explicitly avoid specifying --shared-*-includes,
32 * as that would put the paths into bin/nodejs.
33 * Including pkg-config in build inputs would also have the same effect!
35 ]) (builtins.attrNames sharedLibDeps) ++ [
36 "--with-intl=system-icu"
37 "--openssl-use-def-ca-store"
42 (name: "${lib.getDev sharedLibDeps.${name}}/include/*")
43 (builtins.attrNames sharedLibDeps);
45 extraConfigFlags = lib.optionals (!enableNpm) [ "--without-npm" ];
46 self = stdenv.mkDerivation {
47 inherit pname version;
50 url = "https://nodejs.org/dist/v${version}/node-v${version}.tar.xz";
56 env = lib.optionalAttrs (stdenv.isDarwin && stdenv.isx86_64) {
57 # Make sure libc++ uses `posix_memalign` instead of `aligned_alloc` on x86_64-darwin.
58 # Otherwise, nodejs would require the 11.0 SDK and macOS 10.15+.
59 NIX_CFLAGS_COMPILE = "-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=101300";
64 depsBuildBuild = [ buildPackages.stdenv.cc openssl libuv zlib icu ];
66 # NB: technically, we do not need bash in build inputs since all scripts are
67 # wrappers over the corresponding JS scripts. There are some packages though
68 # that use bash wrappers, e.g. polaris-web.
69 buildInputs = lib.optionals stdenv.isDarwin [ CoreServices ApplicationServices ]
70 ++ [ zlib libuv openssl http-parser icu bash ];
72 nativeBuildInputs = [ which pkg-config python ]
73 ++ lib.optionals stdenv.isDarwin [ xcbuild ];
75 outputs = [ "out" "libv8" ];
76 setOutputFlags = false;
80 inherit (stdenv.hostPlatform) gcc isAarch32;
81 in sharedConfigureFlags ++ lib.optionals (lib.versionOlder version "19") [
83 ] ++ (lib.optionals isCross [
85 "--dest-cpu=${let platform = stdenv.hostPlatform; in
86 if platform.isAarch32 then "arm"
87 else if platform.isAarch64 then "arm64"
88 else if platform.isMips32 && platform.isLittleEndian then "mipsel"
89 else if platform.isMips32 && !platform.isLittleEndian then "mips"
90 else if platform.isMips64 && platform.isLittleEndian then "mips64el"
91 else if platform.isPower && platform.is32bit then "ppc"
92 else if platform.isPower && platform.is64bit then "ppc64"
93 else if platform.isx86_64 then "x86_64"
94 else if platform.isx86_32 then "x86"
95 else if platform.isS390 && platform.is64bit then "s390x"
96 else if platform.isRiscV && platform.is64bit then "riscv64"
97 else throw "unsupported cpu ${stdenv.hostPlatform.uname.processor}"}"
98 ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "fpu" gcc) [
99 "--with-arm-fpu=${gcc.fpu}"
100 ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "float-abi" gcc) [
101 "--with-arm-float-abi=${gcc.float-abi}"
102 ]) ++ extraConfigFlags;
104 configurePlatforms = [];
106 dontDisableStatic = true;
108 enableParallelBuilding = true;
110 # Don't allow enabling content addressed conversion as `nodejs`
111 # checksums it's image before conversion happens and image loading
113 # $ nix build -f. nodejs --arg config '{ contentAddressedByDefault = true; }'
114 # $ ./result/bin/node
115 # Check failed: VerifyChecksum(blob).
116 __contentAddressed = false;
118 passthru.interpreterName = "nodejs";
120 passthru.pkgs = callPackage ../../node-packages/default.nix {
124 setupHook = ./setup-hook.sh;
126 pos = builtins.unsafeGetAttrPos "version" args;
130 doCheck = lib.versionAtLeast version "16"; # some tests fail on v14
132 # Some dependencies required for tools/doc/node_modules (and therefore
133 # test-addons, jstest and others) target are not included in the tarball.
134 # Run test targets that do not require network access.
135 checkTarget = lib.concatStringsSep " " [
136 "build-js-native-api-tests"
137 "build-node-api-tests"
142 # Do not create __pycache__ when running tests.
143 checkFlags = [ "PYTHONDONTWRITEBYTECODE=1" ];
146 HOST_PATH=$out/bin patchShebangs --host $out
148 ${lib.optionalString (enableNpm) ''
149 mkdir -p $out/share/bash-completion/completions
150 ln -s $out/lib/node_modules/npm/lib/utils/completion.sh \
151 $out/share/bash-completion/completions/npm
152 for dir in "$out/lib/node_modules/npm/man/"*; do
153 mkdir -p $out/share/man/$(basename "$dir")
154 for page in "$dir"/*; do
155 ln -rs $page $out/share/man/$(basename "$dir")
160 # install the missing headers for node-gyp
161 cp -r ${lib.concatStringsSep " " copyLibHeaders} $out/include/node
163 # assemble a static v8 library and put it in the 'libv8' output
165 pushd out/Release/obj.target
166 find . -path "./torque_*/**/*.o" -or -path "./v8*/**/*.o" | sort -u >files
167 ${if stdenv.buildPlatform.isGnu then ''
168 ar -cqs $libv8/lib/libv8.a @files
170 # llvm-ar supports response files, so take advantage of it if it’s available.
171 if [ "$(basename $(readlink -f $(command -v ar)))" = "llvm-ar" ]; then
172 ar -cqs $libv8/lib/libv8.a @files
174 cat files | while read -r file; do
175 ar -cqS $libv8/lib/libv8.a $file
182 cp -r deps/v8/include $libv8/
184 # create a pkgconfig file for v8
185 major=$(grep V8_MAJOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
186 minor=$(grep V8_MINOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
187 patch=$(grep V8_PATCH_LEVEL deps/v8/include/v8-version.h | cut -d ' ' -f 3)
188 mkdir -p $libv8/lib/pkgconfig
189 cat > $libv8/lib/pkgconfig/v8.pc << EOF
191 Description: V8 JavaScript Engine
192 Version: $major.$minor.$patch
193 Libs: -L$libv8/lib -lv8 -pthread -licui18n -licuuc
194 Cflags: -I$libv8/include
198 passthru.updateScript = import ./update.nix {
199 inherit writeScript coreutils gnugrep jq curl common-updater-scripts gnupg nix runtimeShell;
201 inherit majorVersion;
205 description = "Event-driven I/O framework for the V8 JavaScript engine";
206 homepage = "https://nodejs.org";
207 changelog = "https://github.com/nodejs/node/releases/tag/v${version}";
208 license = licenses.mit;
209 maintainers = with maintainers; [ goibhniu gilligan cko ];
210 platforms = platforms.linux ++ platforms.darwin;
211 mainProgram = "node";
212 knownVulnerabilities = optional (versionOlder version "18") "This NodeJS release has reached its end of life. See https://nodejs.org/en/about/releases/.";
214 # Node.js build system does not have separate host and target OS
215 # configurations (architectures are defined as host_arch and target_arch,
216 # but there is no such thing as host_os and target_os).
218 # We may be missing something here, but it doesn’t look like it is
219 # possible to cross-compile between different operating systems.
220 broken = stdenv.buildPlatform.parsed.kernel.name != stdenv.hostPlatform.parsed.kernel.name;
223 passthru.python = python; # to ensure nodeEnv uses the same version