Skip to content

Commit

Permalink
Add NixOS support (apache#7697)
Browse files Browse the repository at this point in the history
  • Loading branch information
midchildan authored Apr 13, 2021
1 parent 3fd2c60 commit 19d3af4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
20 changes: 20 additions & 0 deletions config.layout
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,23 @@
logdir: /var/log+
cachedir: /var/lib+
</Layout>

<Layout NixOS>
prefix: /opt/trafficserver
exec_prefix: ${prefix}
bindir: ${exec_prefix}/bin
sbindir: ${exec_prefix}/sbin
libdir: ${exec_prefix}/lib
libexecdir: ${exec_prefix}/libexec
infodir: ${prefix}/share/info
mandir: ${prefix}/share/man
sysconfdir: /etc+
datadir: ${prefix}/share+
docdir: ${prefix}/share/doc+
installbuilddir: ${datadir}/build
includedir: ${prefix}/include
localstatedir: /var/lib+
runtimedir: /run+
logdir: /var/log+
cachedir: /var/cache+
</Layout>
34 changes: 29 additions & 5 deletions rc/trafficserver.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ elif [ "$UNAME_S" = "Darwin" ]; then
DISTRIB_ID="Darwin"
elif [ "$UNAME_S" = "FreeBSD" ]; then
DISTRIB_ID="FreeBSD"
elif nixos-version > /dev/null; then
DISTRIB_ID="nixos"
fi

# We might want to move over to use /etc/system-release for more distros?
Expand All @@ -90,7 +92,7 @@ test -x $ESED || ESED=sed
# Ubuntu: Do NOT "set -e"

#Ubuntu:PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin
test "$DISTRIB_ID" != nixos && PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="start/stop Traffic Server"
NAME=trafficserver
SLEEP_TIME=5
Expand Down Expand Up @@ -135,6 +137,10 @@ elif [ "$DISTRIB_ID" = "Darwin" ]; then
SCRIPTNAME=$0 # Darwin
elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
SCRIPTNAME=$0 # FreeBSD
elif [ "$DISTRIB_ID" = "nixos" ]; then
SCRIPTNAME=$0 # NixOS
echo "Warn: Use of this script is not recommended on NixOS. Instead, enable the module"
echo " 'services.trafficserver' and use systemctl to control Traffic Server."
else
echo "This script needs to be ported to this OS"
exit 1
Expand Down Expand Up @@ -183,7 +189,7 @@ if [ ! -d $TS_BASE@exp_runtimedir@ ]; then
fi

#
# Fedora:
# Fedora/NixOS:
# launches $* in the background
# and checks if program creates its own pid lock file
# redirects stdout and stderr to files
Expand All @@ -201,7 +207,7 @@ forkdaemon()
do
# check for regular file and size greater than 0
if [[ -f $TM_PIDFILE ]] && [[ -s $TM_PIDFILE ]]; then
success
success || true
return 0
fi

Expand All @@ -210,7 +216,7 @@ forkdaemon()

done

failure
failure || true
return 1
}

Expand Down Expand Up @@ -321,6 +327,9 @@ case "$1" in
echo -n "Starting ${TS_PACKAGE_NAME}"
startproc -p $TM_PIDFILE $TM_DAEMON $TM_DAEMON_ARGS
rc_status -v
elif [ "$DISTRIB_ID" = "nixos" ]; then
echo "Starting ${TS_PACKAGE_NAME}"
forkdaemon $TM_DAEMON $TM_DAEMON_ARGS
elif [ "$DISTRIB_ID" = "Darwin" ]; then
echo "Starting ${TS_PACKAGE_NAME}"
launchctl bsexec / launchctl list $TM_NAME > /dev/null 2>&1 && exit 0
Expand Down Expand Up @@ -359,6 +368,16 @@ case "$1" in
echo -n "Stopping ${TS_NAME}"
killproc -p $TS_PIDFILE $TS_DAEMON
rc_status -v
elif [ "$DISTRIB_ID" = "nixos" ]; then
echo "Stopping ${TS_PACKAGE_NAME}"
if [ -e "$TM_PIDFILE" ]; then
kill $(cat $TM_PIDFILE)
rm -f ${TM_PIDFILE}
fi
if [ -e "$TS_PIDFILE" ]; then
kill $(cat $TS_PIDFILE)
rm -f ${TS_PIDFILE}
fi
elif [ "$DISTRIB_ID" = "Darwin" ]; then
echo "Stopping ${TS_PACKAGE_NAME}"
launchctl bsexec / launchctl list $TM_NAME > /dev/null 2>&1 || exit 0
Expand Down Expand Up @@ -404,6 +423,9 @@ case "$1" in
echo -n "Reloading ${NAME}"
$TL_BINARY config reload
rc_status -v
elif [ "$DISTRIB_ID" = "nixos" ]; then
echo "Reloading ${NAME}"
$TL_BINARY config reload
elif [ "$DISTRIB_ID" = "Darwin" ]; then
echo "Reloading ${NAME}"
$TL_BINARY config reload
Expand Down Expand Up @@ -460,7 +482,7 @@ case "$1" in
status=$?
[ $status -eq 0 ] || /bin/echo -n "not "
echo "running."
elif [ "$DISTRIB_ID" = "FreeBSD" -o "$DISTRIB_ID" = "gentoo" ]; then
elif [ "$DISTRIB_ID" = "FreeBSD" -o "$DISTRIB_ID" = "gentoo" -o "$DISTRIB_ID" = "nixos" ]; then
if pgrep $TM_NAME > /dev/null ; then
echo "$TM_NAME running as pid `cat $TM_PIDFILE`" ; else
echo "$TM_NAME not running"
Expand Down Expand Up @@ -496,6 +518,8 @@ case "$1" in
echo "Usage: $0 {start|stop|reload|restart|status}"
elif [ "$DISTRIB_ID" = "suse" ]; then
echo "Usage: $0 {start|stop|restart|status}"
elif [ "$DISTRIB_ID" = "nixos" ]; then
echo "Usage: $0 {start|stop|reload|restart|status}"
elif [ "$DISTRIB_ID" = "Darwin" ]; then
echo "Usage: $0 {start|stop|restart|status}"
elif [ "$DISTRIB_ID" = "FreeBSD" ]; then
Expand Down
7 changes: 2 additions & 5 deletions rc/trafficserver.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
[Unit]
Description=Apache Traffic Server is a fast, scalable and extensible caching proxy server.
After=syslog.target network.target
After=network.target

[Service]
Type=simple
Expand All @@ -26,10 +26,7 @@ ExecStart=@exp_bindir@/traffic_manager $TM_DAEMON_ARGS
Restart=on-failure
RestartSec=5s
LimitNOFILE=1000000
ExecStopPost=/bin/sh -c ' \
export TM_PIDFILE=$(@exp_bindir@/traffic_layout 2>/dev/null | grep RUNTIMEDIR | cut -d: -f2)/manager.lock ; \
/bin/rm $TM_PIDFILE ; \
if [[ $? -ne 0 ]]; then echo "ERROR: Unable to delete PID"; exit 1; fi'
PIDFile=@exp_runtimedir@/manager.lock
TimeoutStopSec=5s
ExecReload=@exp_bindir@/traffic_ctl config reload
KillMode=process
Expand Down
3 changes: 3 additions & 0 deletions tools/tsxs.in
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ do
OBJ=$OPTARG
;;
i)
if [[ "$INSTALLDIR" = /nix/store* ]]; then
bail "unsupported option: -i is disabled on Nix"
fi
INSTALL=1
;;
c)
Expand Down

0 comments on commit 19d3af4

Please sign in to comment.