diff --git a/doc/stdenv/stdenv.xml b/doc/stdenv/stdenv.xml
index f97c2a145af5..e85e2ccb0ec4 100644
--- a/doc/stdenv/stdenv.xml
+++ b/doc/stdenv/stdenv.xml
@@ -1834,6 +1834,19 @@ addEnvHooks "$hostOffset" myBashFunction
+
+
+ move-systemd-user-units.sh
+
+
+
+ This setup hook moves any systemd user units installed in the lib
+ subdirectory into share. In addition, a link is provided from share to
+ lib for compatibility. This is needed for systemd to find user services
+ when installed into the user profile.
+
+
+
set-source-date-epoch-to-latest.sh
diff --git a/nixos/modules/config/system-path.nix b/nixos/modules/config/system-path.nix
index 67305e8499cb..c46937f80081 100644
--- a/nixos/modules/config/system-path.nix
+++ b/nixos/modules/config/system-path.nix
@@ -142,6 +142,7 @@ in
"/share/kservices5"
"/share/kservicetypes5"
"/share/kxmlgui5"
+ "/share/systemd"
];
system.path = pkgs.buildEnv {
diff --git a/pkgs/build-support/setup-hooks/move-systemd-user-units.sh b/pkgs/build-support/setup-hooks/move-systemd-user-units.sh
new file mode 100755
index 000000000000..5963d87c7515
--- /dev/null
+++ b/pkgs/build-support/setup-hooks/move-systemd-user-units.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+
+# This setup hook, for each output, moves everything in
+# $output/lib/systemd/user to $output/share/systemd/user, and replaces
+# $output/lib/systemd/user with a symlink to
+# $output/share/systemd/user.
+
+fixupOutputHooks+=(_moveSystemdUserUnits)
+
+_moveSystemdUserUnits() {
+ if [ "${dontMoveSystemdUserUnits:-0}" = 1 ]; then return; fi
+ if [ ! -e "${prefix:?}/lib/systemd/user" ]; then return; fi
+ local source="$prefix/lib/systemd/user"
+ local target="$prefix/share/systemd/user"
+ echo "moving $source/* to $target"
+ mkdir -p "$target"
+ (
+ shopt -s dotglob
+ for i in "$source"/*; do
+ mv "$i" "$target"
+ done
+ )
+ rmdir "$source"
+ ln -s "$target" "$source"
+}
diff --git a/pkgs/stdenv/generic/default.nix b/pkgs/stdenv/generic/default.nix
index b57989786904..eeddb584a3d5 100644
--- a/pkgs/stdenv/generic/default.nix
+++ b/pkgs/stdenv/generic/default.nix
@@ -61,7 +61,10 @@ let
]
# FIXME this on Darwin; see
# https://github.com/NixOS/nixpkgs/commit/94d164dd7#commitcomment-22030369
- ++ lib.optional hostPlatform.isLinux ../../build-support/setup-hooks/audit-tmpdir.sh
+ ++ lib.optionals hostPlatform.isLinux [
+ ../../build-support/setup-hooks/audit-tmpdir.sh
+ ../../build-support/setup-hooks/move-systemd-user-units.sh
+ ]
++ [
../../build-support/setup-hooks/multiple-outputs.sh
../../build-support/setup-hooks/move-sbin.sh