1#! @shell@
2
3parse_opts () {
4 while @coreutils@/test -n "$1" && @coreutils@/test "x$1" != x-- ; do
5 case "$1" in
6 --store-dir)
7 shift;
8 echo "STORE_DIR='$1'"
9 shift;
10 ;;
11 --priority)
12 shift;
13 echo "PRIORITY=$1";
14 shift;
15 ;;
16 --compression)
17 shift;
18 echo "COMPRESSION=$1";
19 shift;
20 ;;
21 --key)
22 shift;
23 echo "KEY=${1#*:}"
24 echo "KEYNAME=${1%%:*}"
25 shift;
26 ;;
27 --nix-remote)
28 shift;
29 echo "NIX_REMOTE=$1"
30 shift;
31 ;;
32 --mass-query)
33 shift;
34 echo "MASS_QUERY=$1"
35 shift;
36 ;;
37 --port)
38 shift;
39 echo "PORT=$1"
40 shift;
41 ;;
42 --ipv6)
43 shift;
44 echo "USE_IPV6=enable"
45 ;;
46 --help)
47 cat <<EOF >&2
48"$0": start the Nix binary cache serving the Nix store dynamically.
49
50Recognized options:
51
52--port server port
53--store-dir served Nix store
54
55--priority binary cache priority
56--mass-query 0 or 1 - whether binary cache expects queries for nix-env -qas
57
58--compression compression to use: bzip2 or xz
59--key name:/path/to/key - key to use for narinfo signing
60
61--nix-remote 'daemon' or empty string '' - whether to use daemon for store
62 operations
63
64--ipv6 enable ipv6
65
66--help show help and exit
67EOF
68 exit 1;
69 ;;
70 *) shift ;;
71 esac;
72 done
73}
74
75workingdir="$(@coreutils@/mktemp -d)"
76cd "$workingdir"
77
78PORT=8080
79(echo "STORE_DIR=${NIX_STORE_DIR:-/nix/store}"; parse_opts "$@"
80 ) > nix-binary-cache.conf || exit
81. "$workingdir/nix-binary-cache.conf"
82
83echo "
84server.port = $PORT
85server.modules = ( \"mod_cgi\", \"mod_setenv\", )
86server.document-root = \"$workingdir\"
87cgi.assign = ( \".cgi\" => \"@shell@\" )
88setenv.add-request-header = ( \"NIX_BINARY_CACHE_CONFIG\" => \"$workingdir/nix-binary-cache.conf\" )
89" > lighttpd.conf
90
91test -n "$USE_IPV6" && echo "
92\$SERVER[\"socket\"] == \"[::]:$PORT\" {
93server.use-ipv6 = \"enable\"
94}
95" >> lighttpd.conf
96
97cp @out@/nix-binary-cache.cgi .
98cp @out@/nix-binary-cache.cgi ./nix-bc.cgi
99
100ip="$(@iproute@/ip a | @gnugrep@/grep 'inet .* scope global' | @coreutils@/head -n 1)"
101ip="${ip%%/*}"
102ip="${ip##* }"
103
104url="http://$ip:$PORT/nix-bc.cgi?"
105
106echo "Working directory: $workingdir"
107echo
108echo "Address of the binary cache: $url"
109echo
110echo "Usage example: NIX_REMOTE= nix-store --option binary-caches '$url'"
111echo
112echo
113
114@lighttpd@/lighttpd -D -f "$workingdir/lighttpd.conf"
115
116cd /
117@coreutils@/rm -rf "$workingdir"