···1+# SPDX-License-Identifier: MIT
2+# SPDX-FileCopyrightText: Lily Foster <lily@lily.flowers>
3+# Portions of this code are adapted from nixos-cosmic
4+# https://github.com/lilyinstarlight/nixos-cosmic
5+6+# shellcheck shell=bash
7+libcosmicAppWrapperArgs=()
8+9+libcosmicAppVergenHook() {
10+ if [ -z "${VERGEN_GIT_COMMIT_DATE-}" ]; then
11+ # shellcheck disable=SC2155
12+ export VERGEN_GIT_COMMIT_DATE="$(date --utc --date=@"$SOURCE_DATE_EPOCH" '+%Y-%m-%d')"
13+ fi
14+}
15+16+libcosmicAppLinkerArgsHook() {
17+ # Force linking to certain libraries like libEGL, which are always dlopen()ed
18+ local flags="CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS"
19+20+ export "$flags"="${!flags-} -C link-arg=-Wl,--push-state,--no-as-needed"
21+ # shellcheck disable=SC2043
22+ for lib in @cargoLinkLibs@; do
23+ export "$flags"="${!flags} -C link-arg=-l${lib}"
24+ done
25+ export "$flags"="${!flags} -C link-arg=-Wl,--pop-state"
26+}
27+28+preConfigurePhases+=" libcosmicAppVergenHook libcosmicAppLinkerArgsHook"
29+30+# Add shell hook for use in dev shells
31+if [ -n "${IN_NIX_SHELL-}" ] && [ -z "${shellHook-}" ]; then
32+ shellHook="libcosmicAppLinkerArgsHook && export RUSTFLAGS=\$CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS="
33+fi
34+35+libcosmicAppWrapperArgsHook() {
36+ if [ -d "${prefix:?}/share" ]; then
37+ libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "$prefix/share")
38+ fi
39+40+ # add fallback schemas, icons, and settings paths
41+ libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "@fallbackXdgDirs@")
42+}
43+44+preFixupPhases+=" libcosmicAppWrapperArgsHook"
45+46+wrapLibcosmicApp() {
47+ local program="$1"
48+ shift 1
49+ wrapProgram "$program" "${libcosmicAppWrapperArgs[@]}" "$@"
50+}
51+52+# Note: $libcosmicAppWrapperArgs still gets defined even if ${dontWrapLibcosmicApp-} is set
53+libcosmicAppWrapHook() {
54+ # guard against running multiple times (e.g. due to propagation)
55+ [ -z "$libcosmicAppWrapHookHasRun" ] || return 0
56+ libcosmicAppWrapHookHasRun=1
57+58+ if [[ -z "${dontWrapLibcosmicApp:-}" ]]; then
59+ targetDirsThatExist=()
60+ targetDirsRealPath=()
61+62+ # wrap binaries
63+ targetDirs=("${prefix}/bin" "${prefix}/libexec")
64+ for targetDir in "${targetDirs[@]}"; do
65+ if [[ -d "${targetDir}" ]]; then
66+ targetDirsThatExist+=("${targetDir}")
67+ targetDirsRealPath+=("$(realpath "${targetDir}")/")
68+ find "${targetDir}" -type f -executable -print0 |
69+ while IFS= read -r -d '' file; do
70+ echo "Wrapping program '${file}'"
71+ wrapLibcosmicApp "${file}"
72+ done
73+ fi
74+ done
75+76+ # wrap links to binaries that point outside targetDirs
77+ # Note: links to binaries within targetDirs do not need
78+ # to be wrapped as the binaries have already been wrapped
79+ if [[ ${#targetDirsThatExist[@]} -ne 0 ]]; then
80+ find "${targetDirsThatExist[@]}" -type l -xtype f -executable -print0 |
81+ while IFS= read -r -d '' linkPath; do
82+ linkPathReal=$(realpath "${linkPath}")
83+ for targetPath in "${targetDirsRealPath[@]}"; do
84+ if [[ "$linkPathReal" == "$targetPath"* ]]; then
85+ echo "Not wrapping link: '$linkPath' (already wrapped)"
86+ continue 2
87+ fi
88+ done
89+ echo "Wrapping link: '$linkPath'"
90+ wrapLibcosmicApp "${linkPath}"
91+ done
92+ fi
93+ fi
94+}
95+96+fixupOutputHooks+=(libcosmicAppWrapHook)