nodePackages: refactor, add support for overrides

This commit is contained in:
Jaka Hudoklin 2015-01-24 22:54:03 +01:00
parent a00131bb32
commit 628c84c831
3 changed files with 121 additions and 64 deletions

View File

@ -1,6 +1,32 @@
{ stdenv, runCommand, nodejs, neededNatives}:
args @ { name, src, deps ? {}, peerDependencies ? [], flags ? [], preShellHook ? "", postShellHook ? "", resolvedDeps ? {}, bin ? null, ... }:
{
name, src,
# List or attribute set of dependencies
deps ? {},
# List or attribute set of peer depencies
peerDependencies ? [],
# Whether package is binary or library
bin ? null,
# Flags passed to npm install
flags ? [],
# Command to be run before shell hook
preShellHook ? "",
# Command to be run after shell hook
postShellHook ? "",
# Attribute set of already resolved deps (internal),
# for avoiding infinite recursion
resolvedDeps ? {},
...
} @ args:
with stdenv.lib;
@ -12,6 +38,9 @@ let
mv *node* $out
'';
# Package name without version
pkgName = (builtins.parseDrvName name).name;
# Convert deps to attribute set
attrDeps = if isAttrs deps then deps else
(listToAttrs (map (dep: nameValuePair dep.name dep) deps));
@ -22,10 +51,7 @@ let
# Recursive dependencies that we want to avoid with shim creation
recursiveDeps = removeAttrs attrDeps (attrNames requiredDeps);
peerDeps = listToAttrs (concatMap (dep: map (name: {
inherit name;
value = dep;
}) (filter (nm: !(elem nm (args.passthru.names or []))) dep.names)) (peerDependencies));
peerDeps = filter (dep: dep.pkgName != pkgName) peerDependencies;
self = let
# Pass resolved dependencies to dependencies of this package
@ -121,29 +147,29 @@ let
# 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.
${concatStrings (concatMap (dep: map (name:
${concatMapStrings (dep:
if dep.recursiveDeps == [] then ''
ln -sv ${dep}/lib/node_modules/${name} node_modules/
ln -sv ${dep}/lib/node_modules/${dep.pkgName} node_modules/
'' else ''
cp -R ${dep}/lib/node_modules/${name} node_modules/
cp -R ${dep}/lib/node_modules/${dep.pkgName} node_modules/
''
) dep.names) deps)}
) deps}
# Symlink peer dependencies
${concatStrings (mapAttrsToList (name: dep: ''
ln -sv ${dep}/lib/node_modules/${name} node_modules/
'') peerDeps)}
${concatMapStrings (dep: ''
ln -sv ${dep}/lib/node_modules/${dep.pkgName} node_modules/
'') peerDeps}
# Create shims for recursive dependenceies
${concatStrings (concatMap (dep: map (name: ''
mkdir -p node_modules/${name}
cat > node_modules/${name}/package.json <<EOF
${concatMapStrings (dep: ''
mkdir -p node_modules/${dep.pkgName}
cat > node_modules/${dep.pkgName}/package.json <<EOF
{
"name": "${name}",
"version": "${(builtins.parseDrvName dep.name).version}"
"name": "${dep.pkgName}",
"version": "${getVersion dep}"
}
EOF
'') dep.names) (attrValues recursiveDeps))}
'') (attrValues recursiveDeps)}
)
export HOME=$PWD/build-dir
@ -174,34 +200,32 @@ let
cd $HOME
# Remove shims
${concatStrings (concatMap (dep: map (name: ''
rm node_modules/${name}/package.json
rmdir node_modules/${name}
'') dep.names) (attrValues recursiveDeps))}
${concatMapStrings (dep: ''
rm node_modules/${dep.pkgName}/package.json
rmdir node_modules/${dep.pkgName}
'') (attrValues recursiveDeps)}
mkdir -p $out/lib/node_modules
# Install manual
${concatStrings (map (name: ''
mv node_modules/${name} $out/lib/node_modules
rm -fR $out/lib/node_modules/${name}/node_modules
cp -r node_modules $out/lib/node_modules/${name}/node_modules
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/${name}/man" ]; then
if [ -e "$out/lib/node_modules/${pkgName}/man" ]; then
mkdir -p $out/share
for dir in "$out/lib/node_modules/${name}/man/"*; do
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")
done
done
fi
'') args.passthru.names)}
# Symlink dependencies
${concatStrings (mapAttrsToList (name: dep: ''
mv node_modules/${name} $out/lib/node_modules
'') peerDeps)}
${concatMapStrings (dep: ''
mv node_modules/${dep.pkgName} $out/lib/node_modules
'') peerDeps}
# Install binaries and patch shebangs
mv node_modules/.bin $out/lib/node_modules 2>/dev/null || true
@ -214,19 +238,21 @@ let
runHook postInstall
'';
preFixup = concatStringsSep "\n" (map (src: ''
preFixup = ''
find $out -type f -print0 | xargs -0 sed -i 's|${src}|${src.name}|g'
'') src);
'';
shellHook = ''
${preShellHook}
export PATH=${nodejs}/bin:$(pwd)/node_modules/.bin:$PATH
mkdir -p node_modules
${concatStrings (concatMap (dep: map (name: ''
ln -sfv ${dep}/lib/node_modules/${name} node_modules/
'') dep.names) deps)}
${concatMapStrings (dep: ''
ln -sfv ${dep}/lib/node_modules/${dep.pkgName} node_modules/
'') deps}
${postShellHook}
'';
passthru.pkgName = pkgName;
} // (filterAttrs (n: v: n != "deps" && n != "resolvedDeps") args) // {
name = "${
if bin == true then "bin-" else if bin == false then "node-" else ""

View File

@ -1664,11 +1664,9 @@ let
nodejs = callPackage ../development/web/nodejs { };
nodejs-unstable = callPackage ../development/web/nodejs { unstableVersion = true; };
nodePackages = recurseIntoAttrs (callPackage ./node-packages.nix {
inherit pkgs stdenv nodejs fetchurl fetchgit;
neededNatives = [python] ++ lib.optional (lib.elem system lib.platforms.linux) utillinux;
self = pkgs.nodePackages;
});
nodePackages = recurseIntoAttrs (
callPackage ./node-packages.nix { self = nodePackages; }
);
ldapvi = callPackage ../tools/misc/ldapvi { };

View File

@ -1,23 +1,56 @@
{ pkgs, stdenv, nodejs, fetchurl, fetchgit, neededNatives, self, generated ? ./node-packages-generated.nix }:
{
stdenv, pkgs, nodejs
# Self-reference
, self
# Needed natives for installation
, neededNatives ? [pkgs.python] ++ stdenv.lib.optionals stdenv.isLinux [ pkgs.utillinux ]
# Attribute set of generated packages
, generated ? pkgs.callPackage ./node-packages-generated.nix { inherit self; }
# Attribute set of overrides
, overrides ? {}
, ...
} @ args:
with stdenv.lib;
rec {
nativeDeps = {
"node-expat" = [ pkgs.expat ];
"node-stringprep" = [ pkgs.icu pkgs.which ];
"rbytes" = [ pkgs.openssl ];
"phantomjs" = [ pkgs.phantomjs ];
"node-protobuf" = [ pkgs.protobuf ];
};
overrides = {
phantomjs.buildInputs = [ pkgs.phantomjs ];
"node-expat".buildInputs = [ pkgs.expat ];
"node-stringprep".buildInputs = [ pkgs.icu pkgs.which ];
"node-protobuf".buildInputs = [ pkgs.protobuf ];
"rbytes".buildInputs = [ pkgs.openssl ];
} // args.overrides or {};
buildNodePackage = import ../development/web/nodejs/build-node-package.nix {
inherit stdenv nodejs neededNatives;
inherit (pkgs) runCommand;
};
# Apply overrides and back compatiblity transformations
buildNodePackage = {...} @ args:
let
pkg = makeOverridable (
pkgs.callPackage ../development/web/nodejs/build-node-package.nix {
inherit nodejs neededNatives;
}
) (args // {
# Backwards compatibility
src = if isList args.src then head args.src else args.src;
pkgName = (builtins.parseDrvName args.name).name;
});
override = overrides.${args.name} or overrides.${pkg.pkgName} or {};
in pkg.override override;
# Backwards compatibility
patchSource = fn: srcAttrs: fn srcAttrs;
# Backwards compat
patchLatest = patchSource fetchurl;
patchLatest = patchSource pkgs.fetchurl;
/* Put manual packages below here (ideally eventually managed by npm2nix */
} // import generated { inherit self fetchurl fetchgit; inherit (pkgs) lib; }
} // (
if isAttrs generated then generated
# Backwards compatiblity
else pkgs.callPackage generated { inherit self; }
)