forked from nix-community/todomvc-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-docker.sh
executable file
·67 lines (61 loc) · 1.47 KB
/
shell-docker.sh
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
#!/usr/bin/env bash
# Run nix under docker
set -euo pipefail
show_usage() {
local exe=$(basename "$0")
cat <<USAGE
Usage:
$exe [shell] - *enters the nix-shell
$exe run <cmd> [args...] - *runs a command inside of the container
$exe start - starts the container
$exe stop - stops the container
$exe rm [-f] - deletes the container
$exe logs [-f] - show the container logs
$exe -h | --help - show this help
* these commands also automatically start the container
USAGE
}
## Main ##
cd "$(dirname "$0")"
container_name=$(basename "$(pwd)")
container_options=(
--name "$container_name"
-v $PWD:/src
--workdir /src
)
start_container() {
if ! docker inspect "$container_name" &>/dev/null; then
docker create "${container_options[@]}" nixos/nix /bin/sh -c "nix-env -f https://nixos.org/channels/nixos-17.09/nixexprs.tar.xz -iA nixUnstable bashInteractive cacert && exec nix-daemon"
fi
docker start "$container_name"
}
cmd=${1:-}
if [[ -z "$cmd" ]]; then
cmd=shell
fi
shift || true
case "$cmd" in
"shell")
start_container
docker exec -ti -e NIX_REMOTE=daemon "$container_name" nix-shell "$@"
;;
"run")
start_container
docker exec -ti -e NIX_REMOTE=daemon "$container_name" "$@"
;;
"start")
start_container
;;
"stop")
docker stop "$@" "$container_name"
;;
"rm")
docker rm "$@" "$container_name"
;;
"logs")
docker logs "$@" "$container_name"
;;
*)
show_usage
;;
esac