about summary refs log tree commit diff
path: root/scripts/nix-reduce-build.in
blob: b9c9b864fa727e4bf22a17102d466c878c3b0353 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! @shell@

WORKING_DIRECTORY=$(mktemp -d "${TMPDIR:-/tmp}"/nix-reduce-build-XXXXXX);
cd "$WORKING_DIRECTORY";

if test -z "$1" ; then
	echo 'nix-reduce-build (paths or Nix expressions) -- (logins at remote computers)' >&2
	echo As in: >&2
	echo nix-reduce-build /etc/nixos/nixos -- user@somewhere.nowhere.example.org >&2
	exit;
fi;

while ! test "$1" = "--" || test "$1" = "" ; do 
	echo "$1" >> initial; >&2
	shift;
done
shift;
echo Will work on $(cat initial | wc -l) targets. >&2

while read ; do
	case "$REPLY" in 
		${NIX_STORE_PATH:-/nix/store}/*)
			echo "$REPLY" >> paths; >&2
			;;
		*)
			nix-instantiate "$REPLY" >> paths;
			;;
	esac;
done < initial;
echo Proceeding $(cat paths | wc -l) paths. >&2

while read; do
	case "$REPLY" in
		*.drv)
			echo "$REPLY" >> derivers; >&2
			;;
		*)
			nix-store --query --deriver "$REPLY" >>derivers;
			;;
	esac;
done < paths;
echo Found $(cat derivers | wc -l) derivers. >&2

cat derivers | xargs nix-store --query -R > derivers-closure;
echo Proceeding at most $(cat derivers-closure | wc -l) derivers. >&2

cat derivers-closure | egrep '[.]drv$' | xargs nix-store --query --outputs > wanted-paths;
cat derivers-closure | egrep -v '[.]drv$' >> wanted-paths;
echo Prepared $(cat wanted-paths | wc -l) paths to get. >&2

cat wanted-paths | xargs nix-store --check-validity --print-invalid > needed-paths;
echo We need $(cat needed-paths | wc -l) paths. >&2

if test -z "$1" ; then
	cat needed-paths;	
fi;

for i in "$@"; do 
	sshHost="${i#ssh://}";
	httpHost="${i#http://}";
	httpsHost="${i#https://}";
	filePath="${i#file:/}";
	if [ "$i" != "$sshHost" ]; then
		cat needed-paths | while read; do 
			echo "Getting $REPLY and its closure over ssh"
			nix-copy-closure --from "$sshHost" --gzip "$REPLY" </dev/null || true; 
		done;
	elif [ "$i" != "$httpHost" ] || [ "$i" != "$httpsHost" ]; then
		cat needed-paths | while read; do
			echo "Getting $REPLY over http/https"
			curl ${BAD_CERTIFICATE:+-k} -L "$i${REPLY##*/}" | gunzip | nix-store --import;
		done;
	elif [ "$i" != "filePath" ] ; then
		cat needed-paths | while read; do 
			echo "Installing $REPLY from file"
			gunzip < "$filePath/${REPLY##*/}".nar.gz | nix-store --import;
		done;
	fi;
	mv needed-paths wanted-paths;
	cat wanted-paths | xargs nix-store --check-validity --print-invalid > needed-paths;
	echo We still need $(cat needed-paths | wc -l) paths. >&2
done;

cd /
rm -r "$WORKING_DIRECTORY"