[Unit]
Description=narOS: alias /lib/modules for the identity-tagged kernel release
Documentation=https://github.com/abkslm/nucleic/blob/main/docs/NAROS.md
# /lib/modules/`uname -r` is a real path: kmod, depmod and udev all resolve modules
# through it. Since naros-identity tags utsname.release (NAROS.md §2.3), the tagged name
# has no directory and module autoloading would fail on the tiers that load modules at all
# — the VM tiers, which boot an external kernel whose modules the payload bakes in under
# the UNTAGGED release. Symlinking the tagged name onto the real directory fixes every
# consumer at once, which env-var plumbing into each caller could not.
#
# Inert on the container tiers: naros-init is PID 1 there, so no unit ever runs, and
# containers never load modules anyway.
DefaultDependencies=no
Before=systemd-modules-load.service sysinit.target
After=systemd-remount-fs.service

[Service]
Type=oneshot
RemainAfterExit=yes
# Only $(...) substitutions, never bare $var: systemd expands $NAME in ExecStart itself,
# so shell variables here would arrive empty.
#
# Both guards matter. The first requires the real module directory to exist, so a kernel
# that ships no modules is left alone. The second requires the tagged path to be ABSENT,
# which is what makes this safe when the tag is not in effect (shim not installed, or
# passthrough): there tagged == real, the path exists, and we must not replace a real
# module directory with a symlink to itself.
ExecStart=/bin/sh -c '[ -d "/lib/modules/$(NAROS_UNAME_PASSTHROUGH=1 uname -r)" ] && [ ! -e "/lib/modules/$(uname -r)" ] && ln -sfnT "/lib/modules/$(NAROS_UNAME_PASSTHROUGH=1 uname -r)" "/lib/modules/$(uname -r)"; true'

[Install]
WantedBy=sysinit.target
