aboutsummaryrefslogtreecommitdiff
path: root/system/nix/rc.nix
blob: 86f54a25eac1e9629855f545d2ae7a8df4a95e1f (plain)
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
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

BASE=nix-daemon

UNSHARE=/usr/bin/unshare
NIX=/usr/bin/$BASE
NIX_PIDFILE=/var/run/$BASE.pid
NIX_LOG=/var/log/nix.log
NIX_OPTS=

if [ -f /etc/default/$BASE ]; then
	. /etc/default/$BASE
fi

# Check nix is present
if [ ! -x $NIX ]; then
	echo "$NIX not present or not executable"
	exit 1
fi

nix_start() {
  echo "starting $BASE ..."
  if [ -x ${NIX} ]; then
    # If there is an old PID file (no nix-daemon running), clean it up:
    if [ -r ${NIX_PIDFILE} ]; then
      if ! ps axc | grep nix-daemon 1> /dev/null 2> /dev/null ; then
        echo "Cleaning up old ${NIX_PIDFILE}."
        rm -f ${NIX_PIDFILE}
      fi
    fi
    nohup "${UNSHARE}" -m -- ${NIX} >> ${NIX_LOG} 2>&1 &
    echo $! > ${NIX_PIDFILE}
  fi
}

# Stop nix:
nix_stop() {
  echo "stopping $BASE ..."
  # If there is no PID file, ignore this request...
  if [ -r ${NIX_PIDFILE} ]; then
    kill $(cat ${NIX_PIDFILE})
  fi
  rm -f ${NIX_PIDFILE}
}

# Restart nix:
nix_restart() {
	nix_stop
	nix_start
}

case "$1" in
'start')
  nix_start
  ;;
'stop')
  nix_stop
  ;;
'restart')
  nix_restart
  ;;
'status')
  if [ -f ${NIX_PIDFILE} ] && ps -o cmd $(cat ${NIX_PIDFILE}) | grep -q $BASE ; then
	  echo "status of $BASE: running"
  else
	  echo "status of $BASE: stopped"
  fi
  ;;
*)
  echo "usage $0 start|stop|restart|status"
esac

exit 0