···11+# SPDX-License-Identifier: MIT
22+# SPDX-FileCopyrightText: Lily Foster <lily@lily.flowers>
33+# Portions of this code are adapted from nixos-cosmic
44+# https://github.com/lilyinstarlight/nixos-cosmic
55+66+# shellcheck shell=bash
77+libcosmicAppWrapperArgs=()
88+99+libcosmicAppVergenHook() {
1010+ if [ -z "${VERGEN_GIT_COMMIT_DATE-}" ]; then
1111+ # shellcheck disable=SC2155
1212+ export VERGEN_GIT_COMMIT_DATE="$(date --utc --date=@"$SOURCE_DATE_EPOCH" '+%Y-%m-%d')"
1313+ fi
1414+}
1515+1616+libcosmicAppLinkerArgsHook() {
1717+ # Force linking to certain libraries like libEGL, which are always dlopen()ed
1818+ local flags="CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS"
1919+2020+ export "$flags"="${!flags-} -C link-arg=-Wl,--push-state,--no-as-needed"
2121+ # shellcheck disable=SC2043
2222+ for lib in @cargoLinkLibs@; do
2323+ export "$flags"="${!flags} -C link-arg=-l${lib}"
2424+ done
2525+ export "$flags"="${!flags} -C link-arg=-Wl,--pop-state"
2626+}
2727+2828+preConfigurePhases+=" libcosmicAppVergenHook libcosmicAppLinkerArgsHook"
2929+3030+# Add shell hook for use in dev shells
3131+if [ -n "${IN_NIX_SHELL-}" ] && [ -z "${shellHook-}" ]; then
3232+ shellHook="libcosmicAppLinkerArgsHook && export RUSTFLAGS=\$CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS="
3333+fi
3434+3535+libcosmicAppWrapperArgsHook() {
3636+ if [ -d "${prefix:?}/share" ]; then
3737+ libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "$prefix/share")
3838+ fi
3939+4040+ # add fallback schemas, icons, and settings paths
4141+ libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "@fallbackXdgDirs@")
4242+}
4343+4444+preFixupPhases+=" libcosmicAppWrapperArgsHook"
4545+4646+wrapLibcosmicApp() {
4747+ local program="$1"
4848+ shift 1
4949+ wrapProgram "$program" "${libcosmicAppWrapperArgs[@]}" "$@"
5050+}
5151+5252+# Note: $libcosmicAppWrapperArgs still gets defined even if ${dontWrapLibcosmicApp-} is set
5353+libcosmicAppWrapHook() {
5454+ # guard against running multiple times (e.g. due to propagation)
5555+ [ -z "$libcosmicAppWrapHookHasRun" ] || return 0
5656+ libcosmicAppWrapHookHasRun=1
5757+5858+ if [[ -z "${dontWrapLibcosmicApp:-}" ]]; then
5959+ targetDirsThatExist=()
6060+ targetDirsRealPath=()
6161+6262+ # wrap binaries
6363+ targetDirs=("${prefix}/bin" "${prefix}/libexec")
6464+ for targetDir in "${targetDirs[@]}"; do
6565+ if [[ -d "${targetDir}" ]]; then
6666+ targetDirsThatExist+=("${targetDir}")
6767+ targetDirsRealPath+=("$(realpath "${targetDir}")/")
6868+ find "${targetDir}" -type f -executable -print0 |
6969+ while IFS= read -r -d '' file; do
7070+ echo "Wrapping program '${file}'"
7171+ wrapLibcosmicApp "${file}"
7272+ done
7373+ fi
7474+ done
7575+7676+ # wrap links to binaries that point outside targetDirs
7777+ # Note: links to binaries within targetDirs do not need
7878+ # to be wrapped as the binaries have already been wrapped
7979+ if [[ ${#targetDirsThatExist[@]} -ne 0 ]]; then
8080+ find "${targetDirsThatExist[@]}" -type l -xtype f -executable -print0 |
8181+ while IFS= read -r -d '' linkPath; do
8282+ linkPathReal=$(realpath "${linkPath}")
8383+ for targetPath in "${targetDirsRealPath[@]}"; do
8484+ if [[ "$linkPathReal" == "$targetPath"* ]]; then
8585+ echo "Not wrapping link: '$linkPath' (already wrapped)"
8686+ continue 2
8787+ fi
8888+ done
8989+ echo "Wrapping link: '$linkPath'"
9090+ wrapLibcosmicApp "${linkPath}"
9191+ done
9292+ fi
9393+ fi
9494+}
9595+9696+fixupOutputHooks+=(libcosmicAppWrapHook)
+85
pkgs/by-name/li/libcosmicAppHook/package.nix
···11+# SPDX-License-Identifier: MIT
22+# SPDX-FileCopyrightText: Lily Foster <lily@lily.flowers>
33+# Portions of this code are adapted from nixos-cosmic
44+# https://github.com/lilyinstarlight/nixos-cosmic
55+66+{
77+ lib,
88+ stdenv,
99+ makeSetupHook,
1010+ makeBinaryWrapper,
1111+ pkg-config,
1212+ targetPackages,
1313+ libGL,
1414+ libxkbcommon,
1515+ xorg,
1616+ wayland,
1717+ vulkan-loader,
1818+1919+ includeSettings ? true,
2020+}:
2121+2222+makeSetupHook {
2323+ name = "libcosmic-app-hook";
2424+2525+ propagatedBuildInputs = [
2626+ makeBinaryWrapper
2727+ pkg-config
2828+ ];
2929+3030+ # ensure deps for linking below are available
3131+ depsTargetTargetPropagated =
3232+ assert (lib.assertMsg (!targetPackages ? raw) "libcosmicAppHook must be in nativeBuildInputs");
3333+ [
3434+ libGL
3535+ libxkbcommon
3636+ xorg.libX11
3737+ xorg.libXcursor
3838+ xorg.libXi
3939+ xorg.libxcb
4040+ ]
4141+ ++ lib.optionals (!stdenv.isDarwin) [
4242+ wayland
4343+ vulkan-loader
4444+ ];
4545+4646+ substitutions = {
4747+ fallbackXdgDirs = "${lib.optionalString includeSettings "${targetPackages.cosmic-settings}/share:"}${targetPackages.cosmic-icons}/share";
4848+4949+ cargoLinkerVar = stdenv.hostPlatform.rust.cargoEnvVarTarget;
5050+ # force linking for all libraries that may be dlopen'd by libcosmic/iced apps
5151+ cargoLinkLibs = lib.escapeShellArgs (
5252+ [
5353+ # for wgpu-hal
5454+ "EGL"
5555+ # for xkbcommon-dl
5656+ "xkbcommon"
5757+ # for x11-dl, tiny-xlib, wgpu-hal
5858+ "X11"
5959+ # for x11-dl, tiny-xlib
6060+ "X11-xcb"
6161+ # for x11-dl
6262+ "Xcursor"
6363+ "Xi"
6464+ # for x11rb
6565+ "xcb"
6666+ ]
6767+ ++ lib.optionals (!stdenv.isDarwin) [
6868+ # for wgpu-hal, wayland-sys
6969+ "wayland-client"
7070+ # for wgpu-hal
7171+ "wayland-egl"
7272+ "vulkan"
7373+ ]
7474+ );
7575+ };
7676+7777+ meta = {
7878+ description = "Setup hook for configuring and wrapping applications based on libcosmic";
7979+ maintainers = with lib.maintainers; [
8080+ HeitorAugustoLN
8181+ nyabinary
8282+ thefossguy
8383+ ];
8484+ };
8585+} ./libcosmic-app-hook.sh