#!/nix/store/7b3lr24wwfabw2wx4337ai2d6vjwk1kh-bash-5.3p3/bin/sh
# NOTE: systemctl is not available for systemd-generators
set -eu

# disable localization (faster grep)
export LC_ALL=C

## Helper functions
# is_lxc_container succeeds if we're running inside a LXC container
is_lxc_container() {
	grep -q --text container=lxc /proc/1/environ
}

is_lxc_privileged_container() {
	# The full positive 32-bit range is available
	grep -qw 4294967295$ /proc/self/uid_map
}

## Fix functions
# fix_ro_paths avoids udevd issues with /sys and /proc being writable
fix_ro_paths() {
	mkdir -p "/run/systemd/system/${1}.d"
	cat <<-EOF > "/run/systemd/system/${1}.d/zzz-lxc-ropath.conf"
		# This file was created by distrobuilder
		[Service]
		BindReadOnlyPaths=/sys
		EOF
}

# fix_ro_run is a workaround for units failing to write in /run due to ProtectSystem=strict
fix_ro_run() {
	mkdir -p "/run/systemd/system/${1}.d"
	cat <<-EOF > "/run/systemd/system/${1}.d/zzz-lxc-rorun.conf"
		# This file was created by distrobuilder
		[Service]
		ReadWritePaths=/run
		EOF
}

# fix_systemd_override_unit generates a unit specific override
fix_systemd_override_unit() {
	dropin_dir="/run/systemd/${1}.d"
	mkdir -p "${dropin_dir}"

	{
		echo "[Service]";
		[ "${SYSTEMD}" -ge 247 ] && echo "ProcSubset=all";
		[ "${SYSTEMD}" -ge 247 ] && echo "ProtectProc=default";
		[ "${SYSTEMD}" -ge 232 ] && echo "ProtectControlGroups=no";
		[ "${SYSTEMD}" -ge 232 ] && echo "ProtectKernelTunables=no";
		[ "${SYSTEMD}" -ge 239 ] && echo "NoNewPrivileges=no";
		[ "${SYSTEMD}" -ge 249 ] && echo "LoadCredential=";
		[ "${SYSTEMD}" -ge 254 ] && echo "PrivateNetwork=no";
		[ "${SYSTEMD}" -ge 256 ] && echo "ImportCredential=";

		# Additional settings for privileged containers
		if is_lxc_privileged_container; then
			echo "ProtectHome=no";
			echo "ProtectSystem=no";
			echo "PrivateDevices=no";
			echo "PrivateTmp=no";
			[ "${SYSTEMD}" -ge 244 ] && echo "ProtectKernelLogs=no";
			[ "${SYSTEMD}" -ge 232 ] && echo "ProtectKernelModules=no";
			[ "${SYSTEMD}" -ge 231 ] && echo "ReadWritePaths=";
			[ "${SYSTEMD}" -ge 254 ] && [ "${SYSTEMD}" -lt 256 ] && echo "ImportCredential=";
		fi

		true;
	} > "${dropin_dir}/zzz-lxc-service.conf"
}

# fix_systemd_mask masks the systemd unit
fix_systemd_mask() {
	ln -sf /dev/null "/run/systemd/system/${1}"
}

# fix_systemd_udev_trigger overrides the systemd-udev-trigger.service to match the latest version
# of the file which uses "ExecStart=-" instead of "ExecStart=".
fix_systemd_udev_trigger() {
	udev=/run/current-system/sw/bin/udevadm

	mkdir -p /run/systemd/system/systemd-udev-trigger.service.d
	cat <<-EOF > /run/systemd/system/systemd-udev-trigger.service.d/zzz-lxc-override.conf
		# This file was created by distrobuilder
		[Service]
		ExecStart=
		ExecStart=-${udev} trigger --type=subsystems --action=add
		ExecStart=-${udev} trigger --type=devices --action=add
		EOF
}

## Main logic
# Exit immediately if not an Incus/LXC container
is_lxc_container || exit 0

# Determine systemd version
SYSTEMD="$(/run/current-system/sw/lib/systemd/systemd --version | head -n1 | cut -d' ' -f2 | cut -d'~' -f1)"




# Overriding some systemd features is only needed if security.nesting=false
# in which case, /dev/.lxc will be missing
# Adding this conditional back for NixOS as we do not have the reported
# problems, and the overrides could reduce potential service hardening
if [ ! -d /dev/.lxc ]; then
  # Apply systemd overrides
  if [ "${SYSTEMD}" -ge 244 ]; then
    fix_systemd_override_unit system/service
  else
    # Setup per-unit overrides
    find /lib/systemd /etc/systemd /run/systemd /usr/lib/systemd -name "*.service" -type f | sed 's#/\(lib\|etc\|run\|usr/lib\)/systemd/##g'| while read -r service_file; do
      fix_systemd_override_unit "${service_file}"
    done
  fi

  # Workarounds for unprivileged containers.
  if ! is_lxc_privileged_container; then
    fix_ro_paths systemd-networkd.service
    fix_ro_paths systemd-resolved.service
  fi
fi

# Ignore failures on some units.
fix_systemd_udev_trigger

# Fix issues with /run not being writable.
fix_ro_run systemd-nsresourced.service

# Mask some units.
fix_systemd_mask dev-hugepages.mount
fix_systemd_mask run-ribchester-general.mount
fix_systemd_mask systemd-hwdb-update.service
fix_systemd_mask systemd-journald-audit.socket
fix_systemd_mask systemd-modules-load.service
fix_systemd_mask systemd-pstore.service
fix_systemd_mask ua-messaging.service
fix_systemd_mask systemd-firstboot.service
fix_systemd_mask systemd-binfmt.service
if [ ! -e /dev/tty1 ]; then
	fix_systemd_mask vconsole-setup-kludge@tty1.service
fi

if [ -d /etc/udev ]; then
	mkdir -p /run/udev/rules.d
	cat <<-EOF > /run/udev/rules.d/90-lxc-net.rules
		# This file was created by distrobuilder.
		#
		# Its purpose is to convince NetworkManager to treat the eth0 veth
		# interface like a regular Ethernet. NetworkManager ordinarily doesn't
		# like to manage the veth interfaces, because they are typically configured
		# by container management tooling for specialized purposes.

		ACTION=="add|change|move", ENV{ID_NET_DRIVER}=="veth", ENV{INTERFACE}=="eth[0-9]*", ENV{NM_UNMANAGED}="0"
		EOF
fi

# Allow masking units created by the lxc system-generator.
for d in /etc/systemd/system /usr/lib/systemd/system /lib/systemd/system; do
	if ! [ -d "${d}" ]; then
		continue
	fi

	find "${d}" -maxdepth 1 -type l | while read -r f; do
		unit="$(basename "${f}")"

		if [ "${unit}" = "network-device-down.service" ] && [ "$(readlink "${f}")" = "/dev/null" ]; then
			fix_systemd_mask "${unit}"
		fi
	done
done
