Subject: Re: bug#76924: 31.0.50; Wrong install location for systemd user unit file

From: Ulrich Müller <[email-masked]>
Date: Thu, 13 Mar 2025 12:22:40 +0100

Proposed patch attached, please review.

>From 19232af5cb08a321dde60fd59e92c8b30203d7f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ulrich=20M=C3=BCller?= <[email-masked]>
Date: Thu, 13 Mar 2025 12:16:37 +0100
Subject: [PATCH] New configure option --with-systemduserunitdir

* configure.ac (systemduserunitdir): New variable and option.
* Makefile.in (systemduserunitdir): New, set by configure.
(install-etc, uninstall): Don't install emacs.service when
systemduserunitdir is unspecified.
* etc/NEWS: Announce the new configure option.  (Bug#76924)
---
 Makefile.in  | 22 +++++++++-------------
 configure.ac | 16 ++++++++++++++++
 etc/NEWS     |  5 +++++
 3 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/Makefile.in b/Makefile.in
index ca6d991bce6..0ba11c9aa5b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -160,7 +160,7 @@ sharedstatedir=
 # a subdirectory of this.
 libexecdir=@libexecdir@
 
-# Currently only used for the systemd service file.
+# Where to install object code libraries.  Used for ELN_DESTDIR.
 libdir=@libdir@
 
 # Where to install emacs-module.h.
@@ -207,15 +207,7 @@ desktopdir=
 metainfodir=$(datarootdir)/metainfo
 
 # Where the etc/emacs.service file is to be installed.
-# The system value (typically /usr/lib/systemd/user) can be
-# obtained with: pkg-config --variable=systemduserunitdir systemd
-# but that does not respect configure's prefix.
-# It is not clear where we should install this file when
-# prefix != /usr (or /usr/local?) (eg for non-root installs).
-# Other options include ~/.config/systemd/user/,
-# $XDG_RUNTIME_DIR/systemd/user/
-# It seems the user may end up having to make a manual link...
-systemdunitdir=$(libdir)/systemd/user
+systemduserunitdir=@systemduserunitdir@
 
 # Where the etc/images/icons/hicolor directory is to be installed.
 icondir=$(datarootdir)/icons
@@ -886,14 +878,16 @@ install-etc:
 	  ${srcdir}/etc/emacs.metainfo.xml > $${tmp}; \
 	${INSTALL_DATA} $${tmp} "$(DESTDIR)${metainfodir}/${EMACS_NAME}.metainfo.xml"; \
 	rm -f $${tmp}
-	umask 022; $(MKDIR_P) "$(DESTDIR)$(systemdunitdir)"
+  ifneq ($(systemduserunitdir),)
+	umask 022; $(MKDIR_P) "$(DESTDIR)$(systemduserunitdir)"
 	tmp=etc/emacs.tmpservice; rm -f $${tmp}; \
 	sed -e '/^##/d' \
 	  -e "/^Documentation/ s/emacs(1)/${EMACS_NAME}(1)/" \
 	  -e "/^ExecStart/ s|emacs|${bindir}/${EMACS}|" \
 	  ${srcdir}/etc/emacs.service > $${tmp}; \
-	$(INSTALL_DATA) $${tmp} "$(DESTDIR)$(systemdunitdir)/${EMACS_NAME}.service"; \
+	$(INSTALL_DATA) $${tmp} "$(DESTDIR)$(systemduserunitdir)/${EMACS_NAME}.service"; \
 	rm -f $${tmp}
+  endif
 	thisdir=`pwd -P`; \
 	cd ${iconsrcdir} || exit 1; umask 022 ; \
 	for dir in */*/apps */*/mimetypes; do \
@@ -977,7 +971,9 @@ uninstall:
 	fi)
 	-rm -f "$(DESTDIR)${desktopdir}/${EMACS_NAME}.desktop"
 	-rm -f "$(DESTDIR)${metainfodir}/${EMACS_NAME}.metainfo.xml"
-	-rm -f "$(DESTDIR)$(systemdunitdir)/${EMACS_NAME}.service"
+  ifneq ($(systemduserunitdir),)
+	-rm -f "$(DESTDIR)$(systemduserunitdir)/${EMACS_NAME}.service"
+  endif
   ifneq (,$(use_gamedir))
 	for file in snake-scores tetris-scores; do \
 	  file="$(DESTDIR)${gamedir}/$${file}"; \
diff --git a/configure.ac b/configure.ac
index 4a7f326ab2b..033f1dc09ef 100644
--- a/configure.ac
+++ b/configure.ac
@@ -481,6 +481,22 @@ AC_DEFUN
 DUMPING=$with_dumping
 AC_SUBST([DUMPING])
 
+dnl Where the etc/emacs.service file is to be installed.
+dnl We use $(prefix)/lib/systemd/user as default, but this may not be
+dnl in systemd's search path when prefix is neither /usr nor /usr/local.
+dnl In that case (e.g. for non-root installs) the location can be
+dnl specified with the configure option.  (Bug#76924)
+AC_ARG_WITH([systemduserunitdir],
+  [AS_HELP_STRING([--with-systemduserunitdir=DIR],
+    [directory for systemd user unit file;
+    default 'PREFIX/usr/lib/systemd/user'])],
+  [systemduserunitdir=${withval}],
+  [systemduserunitdir='${prefix}/lib/systemd/user'])
+if test "${systemduserunitdir}" = "no"; then
+  systemduserunitdir=''
+fi
+AC_SUBST([systemduserunitdir])
+
 dnl FIXME currently it is not the last.
 dnl This should be the last --with option, because --with-x is
 dnl added later on when we find the file name of X, and it's best to
diff --git a/etc/NEWS b/etc/NEWS
index d79080c94e8..e7f13bbc567 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -37,6 +37,11 @@ host system supports SSE2) or '-fno-tree-sra' (if not).  These GCC
 options work around GCC bug 58416, which can cause Emacs to behave
 incorrectly in rare cases.
 
+---
+** New configure option '--with-systemduserunitdir'.
+This allows to specify the directory where the user unit file for
+systemd is installed; default is '${prefix}/usr/lib/systemd/user'.
+
 
 * Startup Changes in Emacs 31.1
 
-- 
2.48.1