Created
March 2, 2018 12:49
-
-
Save ElvishJerricco/e8193a5b4734877bc492e870625bc92f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
args=() | |
while [ ! -z ${1+x} ]; do | |
case $1 in | |
-o|--out-link) | |
shift | |
if [ -z ${1:+x} ]; then | |
echo "error: flag '-o' requires 1 argument(s)" 1>&2 | |
exit 1 | |
else | |
out=$1 | |
fi | |
;; | |
--no-link) ;; # This is the default, and passing this through | |
# would break the smylinks we use to print | |
# outputs | |
*) | |
args+=("$1") | |
;; | |
esac | |
shift | |
done | |
if [ ! -z ${help+x} ]; then | |
nix build --help | |
exit 0 | |
fi | |
if [ -z ${out:+x} ]; then | |
outbase=out | |
else | |
outbase=$(basename $out) | |
outdir=$(dirname $out) | |
fi | |
DIR=$(mktemp -d --tmpdir) | |
trap "rm -rf $DIR" EXIT | |
nix build -o $DIR/$outbase -f ./default.nix "${args[@]}" | |
if test $(find $DIR -maxdepth 0 -empty); then | |
exit 0 | |
fi | |
readlink $(find $DIR -mindepth 1) | |
if [ ! -z ${outdir+x} ]; then | |
for f in $(ls $DIR); do | |
if ([ -L $outdir/$f ] && [[ $(readlink $outdir/$f) != /nix/store/* ]]) || ([ -e $outdir/$f ] && [ ! -L $outdir/$f ]); then | |
echo "error: cannot create symlink '$outdir/$f'; already exists" 1>&2 | |
exit 1 | |
fi | |
done | |
for f in $(ls $DIR); do | |
nix-store --add-root $outdir/$f --indirect -r $(readlink $DIR/$f) > /dev/null | |
done | |
fi | |
rm $DIR/* | |
nix-store --delete 2> /dev/null > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment