Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bin builds #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,100 @@ in
bw --help
touch $out
'';

# This tests and also serves as an example on how to handle bin
# dependencies.
#
# Typically bin dependencies are shipped with the npm package. These
# packages have a postinstall step that checks if the binary works and
# fallbacks to compiling from source otherwise. Because the binary isn't
# linked against NixOS paths the binary fails.
#
# So here we use the approach of skipping the postinstall, then using
# autopatchelf to fix all the binaries, and finally run the postinstall
# manually.
#
# NOTE: there are other phases such as "preinstall" and "install" that are
# being ignored right now. In my opinion any npm package that depends
# on those is broken.
# See https://docs.npmjs.com/misc/scripts
bin-deps = buildPackage ./test/bin-deps {
nativeBuildInputs = with pkgs; [
autoPatchelfHook

nodejs.passthru.python

# These tend to be needed when
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... when?

autoconf
automake
gettext
gnum4
gnused
nasm
pkgconfig
];

postConfigure = ''
${nodejs-headers-installer}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember having to do something like this, and I think it worked without having to write anything to HOME. Though maybe it was something different?

''
...
export CPATH="${pkgs.nodejs-10_x}/include/node:$CPATH"
npm install --nodedir=${pkgs.nodejs-10_x}/include/node
...
''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I managed to avoid failing to download the headers with npmCommands = ["npm install --nodedir=${pkgs.nodejs}/include/node --loglevel verbose"];

(Although it eventually failed because there was no /usr/bin/env in a sandboxed build)

'';

buildInputs = with pkgs; [
libGL
libpng
libsass
libtool
xlibs.libX11
xlibs.libXi
xlibs.libXxf86vm
zlib
];

npmCommands = [
# Just download and unpack all the npm packages.
"npm install --ignore-scripts"
];

postBuild = ''
mkdir -p node_modules/cwebp-bin/vendor
ln -s ${pkgs.libwebp}/bin/cwebp node_modules/cwebp-bin/vendor/cwebp

mkdir -p node_modules/mozjpeg/vendor
ln -s ${pkgs.mozjpeg}/bin/cjpeg node_modules/mozjpeg/vendor/cjpeg

mkdir -p node_modules/optipng-bin/vendor
ln -s ${pkgs.optipng}/bin/optipng node_modules/optipng-bin/vendor/optipng

ln -s ${pkgs.pngquant}/bin/pngquant node_modules/pngquant-bin/vendor/pngquant

runPostinstall() (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we just inline this below?

cd "$1"
if ! [[ -f package.json ]]; then
return
fi
postinstall=$(jq -r .scripts.postinstall < package.json)
if [[ $postinstall = null ]]; then
return
fi
echo "$1: npm run postinstall"
npm run postinstall
)

# Fix all the binaries with the .so from buildInputs. Magic!
autoPatchelf node_modules

# Now that all the binaries are patched, run all the postinstall
# scripts.
for dep in node_modules/*; do
runPostinstall "$dep"
done

# Test that things are working
npm run build
'';

installPhase = ''
mkdir $out
cp -r node_modules $out/
'';
};
}
Loading