2012-09-13 14:35:35 +02:00
|
|
|
{ stdenv, runCommand, nodejs, neededNatives}:
|
2012-07-25 23:46:21 +02:00
|
|
|
|
2015-01-24 22:54:03 +01:00
|
|
|
{
|
2015-03-21 17:09:38 +01:00
|
|
|
name, version ? "", src,
|
2015-01-24 22:54:03 +01:00
|
|
|
|
2015-02-23 14:27:19 +01:00
|
|
|
# by default name of nodejs interpreter e.g. "nodejs-${name}"
|
|
|
|
namePrefix ? nodejs.interpreterName + "-",
|
|
|
|
|
2015-01-25 01:41:39 +01:00
|
|
|
# Node package name
|
2015-03-21 17:09:38 +01:00
|
|
|
pkgName ?
|
|
|
|
if version != "" then stdenv.lib.removeSuffix "-${version}" name else
|
|
|
|
(builtins.parseDrvName name).name,
|
2015-01-25 01:41:39 +01:00
|
|
|
|
2015-01-24 22:54:03 +01:00
|
|
|
# List or attribute set of dependencies
|
|
|
|
deps ? {},
|
|
|
|
|
|
|
|
# List or attribute set of peer depencies
|
2015-03-21 17:09:38 +01:00
|
|
|
peerDependencies ? {},
|
|
|
|
|
|
|
|
# List or attribute set of optional dependencies
|
|
|
|
optionalDependencies ? {},
|
|
|
|
|
|
|
|
# List of optional dependencies to skip
|
|
|
|
skipOptionalDependencies ? [],
|
2015-01-24 22:54:03 +01:00
|
|
|
|
|
|
|
# Whether package is binary or library
|
2015-03-21 17:09:38 +01:00
|
|
|
bin ? false,
|
2015-01-24 22:54:03 +01:00
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
# Additional flags passed to npm install
|
|
|
|
flags ? "",
|
2015-01-24 22:54:03 +01:00
|
|
|
|
|
|
|
# Command to be run before shell hook
|
|
|
|
preShellHook ? "",
|
|
|
|
|
|
|
|
# Command to be run after shell hook
|
|
|
|
postShellHook ? "",
|
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
# Same as https://docs.npmjs.com/files/package.json#os
|
|
|
|
os ? [],
|
|
|
|
|
|
|
|
# Same as https://docs.npmjs.com/files/package.json#cpu
|
|
|
|
cpu ? [],
|
|
|
|
|
2015-01-24 22:54:03 +01:00
|
|
|
# Attribute set of already resolved deps (internal),
|
|
|
|
# for avoiding infinite recursion
|
|
|
|
resolvedDeps ? {},
|
|
|
|
|
|
|
|
...
|
|
|
|
} @ args:
|
2012-07-25 23:46:21 +02:00
|
|
|
|
2012-09-13 14:35:35 +02:00
|
|
|
with stdenv.lib;
|
|
|
|
|
2013-06-05 18:25:41 +02:00
|
|
|
let
|
2015-03-17 08:53:33 +01:00
|
|
|
self = let
|
2015-03-21 17:09:38 +01:00
|
|
|
sources = runCommand "node-sources" {} ''
|
|
|
|
tar --no-same-owner --no-same-permissions -xf ${nodejs.src}
|
|
|
|
mv $(find . -type d -mindepth 1 -maxdepth 1) $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
platforms = if os == [] then nodejs.meta.platforms else
|
|
|
|
fold (entry: platforms:
|
|
|
|
let
|
|
|
|
filterPlatforms =
|
|
|
|
stdenv.lib.platforms.${removePrefix "!" entry} or [];
|
|
|
|
in
|
|
|
|
# Ignore unknown platforms
|
2015-04-25 15:18:54 +02:00
|
|
|
if filterPlatforms == [] then (if platforms == [] then nodejs.meta.platforms else platforms)
|
2015-03-21 17:09:38 +01:00
|
|
|
else
|
|
|
|
if hasPrefix "!" entry then
|
|
|
|
subtractLists (intersectLists filterPlatforms nodejs.meta.platforms) platforms
|
|
|
|
else
|
|
|
|
platforms ++ (intersectLists filterPlatforms nodejs.meta.platforms)
|
|
|
|
) [] os;
|
|
|
|
|
|
|
|
mapDependencies = deps: f: rec {
|
|
|
|
# Convert deps to attribute set
|
|
|
|
attrDeps = if isAttrs deps then deps else
|
|
|
|
(listToAttrs (map (dep: nameValuePair dep.name dep) deps));
|
|
|
|
|
|
|
|
# All required node modules, without already resolved dependencies
|
|
|
|
# Also override with already resolved dependencies
|
|
|
|
requiredDeps = mapAttrs (name: dep:
|
|
|
|
dep.override {
|
|
|
|
resolvedDeps = resolvedDeps // { "${name}" = self; };
|
|
|
|
}
|
|
|
|
) (filterAttrs f (removeAttrs attrDeps (attrNames resolvedDeps)));
|
|
|
|
|
|
|
|
# Recursive dependencies that we want to avoid with shim creation
|
|
|
|
recursiveDeps = filterAttrs f (removeAttrs attrDeps (attrNames requiredDeps));
|
|
|
|
};
|
|
|
|
|
|
|
|
_dependencies = mapDependencies deps (name: dep:
|
|
|
|
dep.pkgName != pkgName);
|
|
|
|
_optionalDependencies = mapDependencies optionalDependencies (name: dep:
|
|
|
|
(builtins.tryEval dep).success &&
|
|
|
|
!(elem dep.pkgName skipOptionalDependencies)
|
|
|
|
);
|
|
|
|
_peerDependencies = mapDependencies peerDependencies (name: dep:
|
|
|
|
dep.pkgName != pkgName);
|
|
|
|
|
|
|
|
requiredDependencies =
|
|
|
|
_dependencies.requiredDeps //
|
|
|
|
_optionalDependencies.requiredDeps //
|
|
|
|
_peerDependencies.requiredDeps;
|
|
|
|
|
|
|
|
recursiveDependencies =
|
|
|
|
_dependencies.recursiveDeps //
|
|
|
|
_optionalDependencies.recursiveDeps //
|
|
|
|
_peerDependencies.recursiveDeps;
|
2014-09-09 22:46:11 +02:00
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
patchShebangs = dir: ''
|
2015-03-21 17:09:38 +01:00
|
|
|
node=`type -p node`
|
|
|
|
coffee=`type -p coffee || true`
|
|
|
|
find -L ${dir} -type f -print0 | xargs -0 grep -Il . | \
|
|
|
|
xargs sed --follow-symlinks -i \
|
|
|
|
-e 's@#!/usr/bin/env node@#!'"$node"'@' \
|
|
|
|
-e 's@#!/usr/bin/env coffee@#!'"$coffee"'@' \
|
|
|
|
-e 's@#!/.*/node@#!'"$node"'@' \
|
|
|
|
-e 's@#!/.*/coffee@#!'"$coffee"'@' || true
|
2014-11-10 16:59:00 +01:00
|
|
|
'';
|
2014-09-09 22:46:11 +02:00
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
in stdenv.mkDerivation ({
|
2014-09-09 22:46:11 +02:00
|
|
|
inherit src;
|
|
|
|
|
2015-01-24 22:52:20 +01:00
|
|
|
configurePhase = ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
${patchShebangs "./"}
|
|
|
|
|
|
|
|
# Some version specifiers (latest, unstable, URLs, file paths) force NPM
|
|
|
|
# to make remote connections or consult paths outside the Nix store.
|
|
|
|
# The following JavaScript replaces these by * to prevent that:
|
2015-02-07 15:04:35 +01:00
|
|
|
# Also some packages require a specific npm version because npm may
|
|
|
|
# resovle dependencies differently, but npm is not used by Nix for dependency
|
|
|
|
# reslution, so these requirements are dropped.
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
(
|
|
|
|
cat <<EOF
|
|
|
|
var fs = require('fs');
|
|
|
|
var url = require('url');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replaces an impure version specification by *
|
|
|
|
*/
|
|
|
|
function replaceImpureVersionSpec(versionSpec) {
|
|
|
|
var parsedUrl = url.parse(versionSpec);
|
|
|
|
|
2017-10-22 16:45:16 +02:00
|
|
|
if(versionSpec == "" || versionSpec == "latest" || versionSpec == "unstable" ||
|
2014-11-10 16:59:00 +01:00
|
|
|
versionSpec.substr(0, 2) == ".." || dependency.substr(0, 2) == "./" || dependency.substr(0, 2) == "~/" || dependency.substr(0, 1) == '/' || /^[^/]+\/[^/]+$/.test(versionSpec))
|
|
|
|
return '*';
|
|
|
|
else if(parsedUrl.protocol == "git:" || parsedUrl.protocol == "git+ssh:" || parsedUrl.protocol == "git+http:" || parsedUrl.protocol == "git+https:" ||
|
|
|
|
parsedUrl.protocol == "http:" || parsedUrl.protocol == "https:")
|
|
|
|
return '*';
|
|
|
|
else
|
|
|
|
return versionSpec;
|
|
|
|
}
|
2014-09-09 22:46:11 +02:00
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
var packageObj = JSON.parse(fs.readFileSync('./package.json'));
|
2014-09-09 22:46:11 +02:00
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
/* Replace dependencies */
|
|
|
|
if(packageObj.dependencies !== undefined) {
|
|
|
|
for(var dependency in packageObj.dependencies) {
|
|
|
|
var versionSpec = packageObj.dependencies[dependency];
|
|
|
|
packageObj.dependencies[dependency] = replaceImpureVersionSpec(versionSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace development dependencies */
|
|
|
|
if(packageObj.devDependencies !== undefined) {
|
|
|
|
for(var dependency in packageObj.devDependencies) {
|
|
|
|
var versionSpec = packageObj.devDependencies[dependency];
|
|
|
|
packageObj.devDependencies[dependency] = replaceImpureVersionSpec(versionSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace optional dependencies */
|
|
|
|
if(packageObj.optionalDependencies !== undefined) {
|
|
|
|
for(var dependency in packageObj.optionalDependencies) {
|
|
|
|
var versionSpec = packageObj.optionalDependencies[dependency];
|
|
|
|
packageObj.optionalDependencies[dependency] = replaceImpureVersionSpec(versionSpec);
|
|
|
|
}
|
2014-09-09 22:46:11 +02:00
|
|
|
}
|
2012-07-25 23:46:21 +02:00
|
|
|
|
2015-02-07 15:04:35 +01:00
|
|
|
/* Ignore npm version requirement */
|
|
|
|
if(packageObj.engines) {
|
|
|
|
delete packageObj.engines.npm;
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
/* Write the fixed JSON file */
|
|
|
|
fs.writeFileSync("package.json", JSON.stringify(packageObj));
|
|
|
|
EOF
|
|
|
|
) | node
|
|
|
|
|
2015-01-24 22:52:20 +01:00
|
|
|
# We do not handle shrinkwraps yet
|
|
|
|
rm npm-shrinkwrap.json 2>/dev/null || true
|
2014-11-10 16:59:00 +01:00
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
mkdir ../build-dir
|
2014-11-10 16:59:00 +01:00
|
|
|
(
|
2015-03-21 17:09:38 +01:00
|
|
|
cd ../build-dir
|
2014-11-10 16:59:00 +01:00
|
|
|
mkdir node_modules
|
|
|
|
|
|
|
|
# Symlink or copy dependencies for node modules
|
|
|
|
# copy is needed if dependency has recursive dependencies,
|
|
|
|
# because node can't follow symlinks while resolving recursive deps.
|
2015-01-24 22:54:03 +01:00
|
|
|
${concatMapStrings (dep:
|
2014-11-10 16:59:00 +01:00
|
|
|
if dep.recursiveDeps == [] then ''
|
2015-01-24 22:54:03 +01:00
|
|
|
ln -sv ${dep}/lib/node_modules/${dep.pkgName} node_modules/
|
2014-11-10 16:59:00 +01:00
|
|
|
'' else ''
|
2015-01-24 22:54:03 +01:00
|
|
|
cp -R ${dep}/lib/node_modules/${dep.pkgName} node_modules/
|
2014-11-10 16:59:00 +01:00
|
|
|
''
|
2015-03-21 17:09:38 +01:00
|
|
|
) (attrValues requiredDependencies)}
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
# Create shims for recursive dependenceies
|
2015-01-24 22:54:03 +01:00
|
|
|
${concatMapStrings (dep: ''
|
|
|
|
mkdir -p node_modules/${dep.pkgName}
|
|
|
|
cat > node_modules/${dep.pkgName}/package.json <<EOF
|
2014-11-10 16:59:00 +01:00
|
|
|
{
|
2015-01-24 22:54:03 +01:00
|
|
|
"name": "${dep.pkgName}",
|
|
|
|
"version": "${getVersion dep}"
|
2014-11-10 16:59:00 +01:00
|
|
|
}
|
|
|
|
EOF
|
2015-03-21 17:09:38 +01:00
|
|
|
'') (attrValues recursiveDependencies)}
|
2014-11-10 16:59:00 +01:00
|
|
|
)
|
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
export HOME=$PWD/../build-dir
|
2014-09-09 22:46:11 +02:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
# If source was a file, repackage it, so npm pre/post publish hooks are not triggered,
|
|
|
|
if [[ -f $src ]]; then
|
2015-03-21 17:09:38 +01:00
|
|
|
GZIP=-1 tar -czf ../build-dir/package.tgz ./
|
2014-11-10 16:59:00 +01:00
|
|
|
export src=$HOME/package.tgz
|
|
|
|
else
|
|
|
|
export src=$PWD
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Install package
|
2015-03-21 17:09:38 +01:00
|
|
|
(cd $HOME && npm --registry http://www.example.com --nodedir=${sources} install $src --fetch-retries 0 ${flags})
|
2014-11-10 16:59:00 +01:00
|
|
|
|
2014-09-09 22:46:11 +02:00
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
|
2014-11-10 16:59:00 +01:00
|
|
|
(
|
|
|
|
cd $HOME
|
|
|
|
|
|
|
|
# Remove shims
|
2015-01-24 22:54:03 +01:00
|
|
|
${concatMapStrings (dep: ''
|
|
|
|
rm node_modules/${dep.pkgName}/package.json
|
|
|
|
rmdir node_modules/${dep.pkgName}
|
2015-03-21 17:09:38 +01:00
|
|
|
'') (attrValues recursiveDependencies)}
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
mkdir -p $out/lib/node_modules
|
|
|
|
|
|
|
|
# Install manual
|
2015-01-24 22:54:03 +01:00
|
|
|
mv node_modules/${pkgName} $out/lib/node_modules
|
|
|
|
rm -fR $out/lib/node_modules/${pkgName}/node_modules
|
|
|
|
cp -r node_modules $out/lib/node_modules/${pkgName}/node_modules
|
|
|
|
|
|
|
|
if [ -e "$out/lib/node_modules/${pkgName}/man" ]; then
|
|
|
|
mkdir -p $out/share
|
|
|
|
for dir in "$out/lib/node_modules/${pkgName}/man/"*; do
|
|
|
|
mkdir -p $out/share/man/$(basename "$dir")
|
|
|
|
for page in "$dir"/*; do
|
|
|
|
ln -sv $page $out/share/man/$(basename "$dir")
|
2014-09-09 22:46:11 +02:00
|
|
|
done
|
2015-01-24 22:54:03 +01:00
|
|
|
done
|
|
|
|
fi
|
2014-11-10 16:59:00 +01:00
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
# Move peer dependencies to node_modules
|
2015-01-24 22:54:03 +01:00
|
|
|
${concatMapStrings (dep: ''
|
|
|
|
mv node_modules/${dep.pkgName} $out/lib/node_modules
|
2015-03-21 17:09:38 +01:00
|
|
|
'') (attrValues _peerDependencies.requiredDeps)}
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
# Install binaries and patch shebangs
|
|
|
|
mv node_modules/.bin $out/lib/node_modules 2>/dev/null || true
|
|
|
|
if [ -d "$out/lib/node_modules/.bin" ]; then
|
|
|
|
ln -sv $out/lib/node_modules/.bin $out/bin
|
|
|
|
${patchShebangs "$out/lib/node_modules/.bin/*"}
|
2014-09-09 22:46:11 +02:00
|
|
|
fi
|
2014-11-10 16:59:00 +01:00
|
|
|
)
|
|
|
|
|
2014-09-09 22:46:11 +02:00
|
|
|
runHook postInstall
|
|
|
|
'';
|
2012-08-22 21:35:00 +02:00
|
|
|
|
2015-01-24 22:54:03 +01:00
|
|
|
preFixup = ''
|
2014-09-09 22:46:11 +02:00
|
|
|
find $out -type f -print0 | xargs -0 sed -i 's|${src}|${src.name}|g'
|
2015-01-24 22:54:03 +01:00
|
|
|
'';
|
2014-09-09 22:46:11 +02:00
|
|
|
|
|
|
|
shellHook = ''
|
|
|
|
${preShellHook}
|
|
|
|
export PATH=${nodejs}/bin:$(pwd)/node_modules/.bin:$PATH
|
|
|
|
mkdir -p node_modules
|
2015-01-24 22:54:03 +01:00
|
|
|
${concatMapStrings (dep: ''
|
|
|
|
ln -sfv ${dep}/lib/node_modules/${dep.pkgName} node_modules/
|
2015-03-21 17:09:38 +01:00
|
|
|
'') (attrValues requiredDependencies)}
|
2014-09-09 22:46:11 +02:00
|
|
|
${postShellHook}
|
|
|
|
'';
|
2015-01-24 22:54:03 +01:00
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
# Stipping does not make a lot of sense in node packages
|
|
|
|
dontStrip = true;
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
inherit platforms;
|
|
|
|
maintainers = [ stdenv.lib.maintainers.offline ];
|
|
|
|
};
|
|
|
|
|
2015-01-24 22:54:03 +01:00
|
|
|
passthru.pkgName = pkgName;
|
2015-03-21 17:09:38 +01:00
|
|
|
} // (filterAttrs (n: v: all (k: n != k) ["deps" "resolvedDeps" "optionalDependencies"]) args) // {
|
2015-02-23 14:27:19 +01:00
|
|
|
name = namePrefix + name;
|
2014-09-09 22:46:11 +02:00
|
|
|
|
|
|
|
# Run the node setup hook when this package is a build input
|
|
|
|
propagatedNativeBuildInputs = (args.propagatedNativeBuildInputs or []) ++ [ nodejs ];
|
|
|
|
|
2015-03-21 17:09:38 +01:00
|
|
|
nativeBuildInputs =
|
|
|
|
(args.nativeBuildInputs or []) ++ neededNatives ++
|
|
|
|
(attrValues requiredDependencies);
|
2014-11-10 16:59:00 +01:00
|
|
|
|
|
|
|
# Expose list of recursive dependencies upstream, up to the package that
|
|
|
|
# caused recursive dependency
|
2015-03-21 17:09:38 +01:00
|
|
|
recursiveDeps =
|
|
|
|
(flatten (
|
|
|
|
map (dep: remove name dep.recursiveDeps) (attrValues requiredDependencies)
|
|
|
|
)) ++
|
|
|
|
(attrNames recursiveDependencies);
|
2014-09-09 22:46:11 +02:00
|
|
|
});
|
2013-05-29 17:53:36 +02:00
|
|
|
|
2014-09-09 22:46:11 +02:00
|
|
|
in self
|