1{ lib, stdenv, fetchurl, openssl, python, zlib, libuv, util-linux, http-parser
2, pkg-config, which, buildPackages
3# for `.pkgs` attribute
4, callPackage
5# Updater dependencies
6, writeScript, coreutils, gnugrep, jq, curl, common-updater-scripts, nix, runtimeShell
7, gnupg
8, darwin, xcbuild
9, procps, icu
10}:
11
12{ enableNpm ? true, version, sha256, patches ? [] } @args:
13
14let
15 inherit (darwin.apple_sdk.frameworks) CoreServices ApplicationServices;
16
17 majorVersion = lib.versions.major version;
18 minorVersion = lib.versions.minor version;
19
20 pname = if enableNpm then "nodejs" else "nodejs-slim";
21
22 useSharedHttpParser = !stdenv.isDarwin && lib.versionOlder "${majorVersion}.${minorVersion}" "11.4";
23
24 sharedLibDeps = { inherit openssl zlib libuv; } // (lib.optionalAttrs useSharedHttpParser { inherit http-parser; });
25
26 sharedConfigureFlags = lib.concatMap (name: [
27 "--shared-${name}"
28 "--shared-${name}-libpath=${lib.getLib sharedLibDeps.${name}}/lib"
29 /** Closure notes: we explicitly avoid specifying --shared-*-includes,
30 * as that would put the paths into bin/nodejs.
31 * Including pkg-config in build inputs would also have the same effect!
32 */
33 ]) (builtins.attrNames sharedLibDeps) ++ [
34 "--with-intl=system-icu"
35 ];
36
37 copyLibHeaders =
38 map
39 (name: "${lib.getDev sharedLibDeps.${name}}/include/*")
40 (builtins.attrNames sharedLibDeps);
41
42 extraConfigFlags = lib.optionals (!enableNpm) [ "--without-npm" ];
43 self = stdenv.mkDerivation {
44 inherit pname version;
45
46 src = fetchurl {
47 url = "https://nodejs.org/dist/v${version}/node-v${version}.tar.xz";
48 inherit sha256;
49 };
50
51 CC_host = "cc";
52 CXX_host = "c++";
53 depsBuildBuild = [ buildPackages.stdenv.cc openssl libuv zlib ];
54
55 buildInputs = lib.optionals stdenv.isDarwin [ CoreServices ApplicationServices ]
56 ++ [ zlib libuv openssl http-parser icu ];
57
58 nativeBuildInputs = [ which pkg-config python ]
59 ++ lib.optionals stdenv.isDarwin [ xcbuild ];
60
61 outputs = [ "out" "libv8" ];
62 setOutputFlags = false;
63 moveToDev = false;
64
65 configureFlags = let
66 isCross = stdenv.hostPlatform != stdenv.buildPlatform;
67 inherit (stdenv.hostPlatform) gcc isAarch32;
68 in sharedConfigureFlags ++ lib.optionals (lib.versionOlder version "19") [
69 "--without-dtrace"
70 ] ++ (lib.optionals isCross [
71 "--cross-compiling"
72 "--without-intl"
73 "--without-snapshot"
74 "--dest-cpu=${let platform = stdenv.hostPlatform; in
75 if platform.isAarch32 then "arm"
76 else if platform.isAarch64 then "arm64"
77 else if platform.isMips32 && platform.isLittleEndian then "mipsel"
78 else if platform.isMips32 && !platform.isLittleEndian then "mips"
79 else if platform.isMips64 && platform.isLittleEndian then "mips64el"
80 else if platform.isPower && platform.is32bit then "ppc"
81 else if platform.isPower && platform.is64bit then "ppc64"
82 else if platform.isx86_64 then "x86_64"
83 else if platform.isx86_32 then "x86"
84 else if platform.isS390 && platform.is64bit then "s390x"
85 else if platform.isRiscV && platform.is64bit then "riscv64"
86 else throw "unsupported cpu ${stdenv.hostPlatform.uname.processor}"}"
87 ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "fpu" gcc) [
88 "--with-arm-fpu=${gcc.fpu}"
89 ]) ++ (lib.optionals (isCross && isAarch32 && lib.hasAttr "float-abi" gcc) [
90 "--with-arm-float-abi=${gcc.float-abi}"
91 ]) ++ extraConfigFlags;
92
93 configurePlatforms = [];
94
95 dontDisableStatic = true;
96
97 enableParallelBuilding = true;
98
99 # Don't allow enabling content addressed conversion as `nodejs`
100 # checksums it's image before conversion happens and image loading
101 # breaks:
102 # $ nix build -f. nodejs --arg config '{ contentAddressedByDefault = true; }'
103 # $ ./result/bin/node
104 # Check failed: VerifyChecksum(blob).
105 __contentAddressed = false;
106
107 passthru.interpreterName = "nodejs";
108
109 passthru.pkgs = callPackage ../../node-packages/default.nix {
110 nodejs = self;
111 };
112
113 setupHook = ./setup-hook.sh;
114
115 pos = builtins.unsafeGetAttrPos "version" args;
116
117 inherit patches;
118
119 postPatch = ''
120 patchShebangs .
121
122 # fix tests
123 for a in test/parallel/test-child-process-env.js \
124 test/parallel/test-child-process-exec-env.js \
125 test/parallel/test-child-process-default-options.js \
126 test/fixtures/syntax/good_syntax_shebang.js \
127 test/fixtures/syntax/bad_syntax_shebang.js ; do
128 substituteInPlace $a \
129 --replace "/usr/bin/env" "${coreutils}/bin/env"
130 done
131 '' + lib.optionalString stdenv.isDarwin ''
132 sed -i -e "s|tr1/type_traits|type_traits|g" \
133 -e "s|std::tr1|std|" src/util.h
134 '';
135
136 nativeCheckInputs = [ procps ];
137 doCheck = false; # fails 4 out of 1453 tests
138
139 postInstall = ''
140 PATH=$out/bin:$PATH patchShebangs $out
141
142 ${lib.optionalString (enableNpm && stdenv.hostPlatform == stdenv.buildPlatform) ''
143 mkdir -p $out/share/bash-completion/completions/
144 HOME=$TMPDIR $out/bin/npm completion > $out/share/bash-completion/completions/npm
145 for dir in "$out/lib/node_modules/npm/man/"*; do
146 mkdir -p $out/share/man/$(basename "$dir")
147 for page in "$dir"/*; do
148 ln -rs $page $out/share/man/$(basename "$dir")
149 done
150 done
151 ''}
152
153 # install the missing headers for node-gyp
154 cp -r ${lib.concatStringsSep " " copyLibHeaders} $out/include/node
155
156 # assemble a static v8 library and put it in the 'libv8' output
157 mkdir -p $libv8/lib
158 pushd out/Release/obj.target
159 find . -path "./torque_*/**/*.o" -or -path "./v8*/**/*.o" | sort -u >files
160 ${if stdenv.buildPlatform.isGnu then ''
161 ar -cqs $libv8/lib/libv8.a @files
162 '' else ''
163 cat files | while read -r file; do
164 ar -cqS $libv8/lib/libv8.a $file
165 done
166 ''}
167 popd
168
169 # copy v8 headers
170 cp -r deps/v8/include $libv8/
171
172 # create a pkgconfig file for v8
173 major=$(grep V8_MAJOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
174 minor=$(grep V8_MINOR_VERSION deps/v8/include/v8-version.h | cut -d ' ' -f 3)
175 patch=$(grep V8_PATCH_LEVEL deps/v8/include/v8-version.h | cut -d ' ' -f 3)
176 mkdir -p $libv8/lib/pkgconfig
177 cat > $libv8/lib/pkgconfig/v8.pc << EOF
178 Name: v8
179 Description: V8 JavaScript Engine
180 Version: $major.$minor.$patch
181 Libs: -L$libv8/lib -lv8 -pthread -licui18n
182 Cflags: -I$libv8/include
183 EOF
184 '';
185
186 passthru.updateScript = import ./update.nix {
187 inherit writeScript coreutils gnugrep jq curl common-updater-scripts gnupg nix runtimeShell;
188 inherit lib;
189 inherit majorVersion;
190 };
191
192 meta = with lib; {
193 description = "Event-driven I/O framework for the V8 JavaScript engine";
194 homepage = "https://nodejs.org";
195 changelog = "https://github.com/nodejs/node/releases/tag/v${version}";
196 license = licenses.mit;
197 maintainers = with maintainers; [ goibhniu gilligan cko marsam ];
198 platforms = platforms.linux ++ platforms.darwin;
199 mainProgram = "node";
200 knownVulnerabilities = optional (versionOlder version "18") "This NodeJS release has reached its end of life. See https://nodejs.org/en/about/releases/.";
201 };
202
203 passthru.python = python; # to ensure nodeEnv uses the same version
204 };
205in self