Skip to content

Instantly share code, notes, and snippets.

@albertov
Last active March 13, 2017 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertov/5e8ff8e1f2c5cca45a8f4b5e74f0f2ef to your computer and use it in GitHub Desktop.
Save albertov/5e8ff8e1f2c5cca45a8f4b5e74f0f2ef to your computer and use it in GitHub Desktop.
Make a portable binary distribution from any nix package
#!/usr/bin/env bash
set -e
# Path to thhe nix file containing the derivation we want to package
NIXFILE=./path/to/some/nixfile.nix
# Refer to the attribute in the nix file containing the derivation we want to package
ATTRARG="-A foo"
# "drv" will be set to the path inside /nix/store which contains the resulting the derivation
drv=$(nix-build $NIXFILE $ATTRARG)
# output filename
tarball=$(pwd)/bindist.tar.xz
# temporary working directory
tmpdir=$(mktemp -d)
trap "rm -rf $tmpdir" EXIT
cd $tmpdir
# create symlinks inside target's usr/local/bin to every executable in the derivation (non-transitively)
mkdir -p usr/local/bin
for binary in $(find ${drv}/bin); do
ln -s ${binary} usr/local/bin/$(basename ${binary})
done
# finally, pack the full closure of the derivation with the symlinks
# we just created
tar cvJf $tarball --owner 0 --group 0 usr $(nix-store -qR $drv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment