summary refs log tree commit diff
path: root/pkgs/servers/http/nix-binary-cache/nix-binary-cache.cgi.in
blob: 890e68765b4093aed95e6e289f908a6396e85e6a (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! @shell@

STORE_DIR="${NIX_STORE_DIR:-/nix/store}"
MASS_QUERY=0
PRIORITY=75
COMPRESSION=bzip2
KEY=
KEYNAME=na

export NIX_REMOTE=daemon

config="${NIX_BINARY_CACHE_CONFIG:-${HTTP_NIX_BINARY_CACHE_CONFIG:-/etc/nix/nix-binary-cache.cgi.conf}}"
config="$(cd "$(@coreutils@/dirname "$config")";
  @coreutils@/pwd)/$(@coreutils@/basename "$config")"
@coreutils@/test -e "$config" && . "$config"

header(){
	echo "Content-Type: text/plain; charset=utf-8"
	echo
}

header404(){
	echo "Status: 404 Not Found"
	echo
}

clean_path() {
	@gnused@/sed -re "s@^$STORE_DIR/?@@" | @findutils@/xargs
}

storeq(){
	@nix@/nix-store -q "$@"
}

sign(){
	test -n "$1" &&
	  @coreutils@/sha256sum | @gnused@/sed -e 's/ .*//' |
	  @openssl@/openssl rsautl -sign -inkey "$@" | @coreutils@/base64 -w 0
}

case "$QUERY_STRING" in
	"")
		header
		echo "Hello, this is a dynamically-generated Nix binary cache"
		;;
	/debug)
		header
		set
		;;
	/nix-cache-info)
		header
		echo "StoreDir: $STORE_DIR"
		echo "WantMassQuery: $MASS_QUERY"
		echo "Priority: $PRIORITY"
		;;
	*.narinfo)
		hash=${QUERY_STRING%.narinfo}
		hash=${hash#/}
		path="$(echo "$STORE_DIR/$hash-"* | @coreutils@/sort | @coreutils@/head -n 1)"
		if [ -n "$path" ] && [ -e "$path" ]; then
			header
			info="$(
			echo "StorePath: $path"
			echo "URL: $(@coreutils@/basename "$path"
			  ).nar.$COMPRESSION"
			echo "Compression: $COMPRESSION"
			echo "NarHash: $(storeq --hash "$path")"
			echo "NarSize: $(storeq --size "$path")"
			echo "References: $(storeq --references "$path" |
			  @coreutils@/tac | clean_path )"
			echo "Deriver: $(storeq --deriver "$path" |
			  clean_path )"
			)"
			signature="$(echo "$info" | sign "$KEY")"

			echo "$info"
			echo "Signature: 1;$KEYNAME;$signature"

		else
			header404
			exit 1
		fi
		;;
	*.nar.xz)
		path="$STORE_DIR${QUERY_STRING%.nar.xz}"
		if [ -n "$path" ] && [ -e "$path" ]; then
			header
			@nix@/nix-store --dump "$path" | @xz@/xz
		else
			header404
			exit 1
		fi
		;;
	*.nar.bzip2)
		path="$STORE_DIR${QUERY_STRING%.nar.bzip2}"
		echo "$path" >&2;
		if [ -n "$path" ] && [ -e "$path" ]; then
			header
			@nix@/nix-store --dump "$path" | @bzip2@/bzip2
		else
			header404
			exit 1
		fi
		;;
esac