libcosmicAppHook: init

+181
+96
pkgs/by-name/li/libcosmicAppHook/libcosmic-app-hook.sh
··· 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)
+85
pkgs/by-name/li/libcosmicAppHook/package.nix
··· 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 + { 7 + lib, 8 + stdenv, 9 + makeSetupHook, 10 + makeBinaryWrapper, 11 + pkg-config, 12 + targetPackages, 13 + libGL, 14 + libxkbcommon, 15 + xorg, 16 + wayland, 17 + vulkan-loader, 18 + 19 + includeSettings ? true, 20 + }: 21 + 22 + makeSetupHook { 23 + name = "libcosmic-app-hook"; 24 + 25 + propagatedBuildInputs = [ 26 + makeBinaryWrapper 27 + pkg-config 28 + ]; 29 + 30 + # ensure deps for linking below are available 31 + depsTargetTargetPropagated = 32 + assert (lib.assertMsg (!targetPackages ? raw) "libcosmicAppHook must be in nativeBuildInputs"); 33 + [ 34 + libGL 35 + libxkbcommon 36 + xorg.libX11 37 + xorg.libXcursor 38 + xorg.libXi 39 + xorg.libxcb 40 + ] 41 + ++ lib.optionals (!stdenv.isDarwin) [ 42 + wayland 43 + vulkan-loader 44 + ]; 45 + 46 + substitutions = { 47 + fallbackXdgDirs = "${lib.optionalString includeSettings "${targetPackages.cosmic-settings}/share:"}${targetPackages.cosmic-icons}/share"; 48 + 49 + cargoLinkerVar = stdenv.hostPlatform.rust.cargoEnvVarTarget; 50 + # force linking for all libraries that may be dlopen'd by libcosmic/iced apps 51 + cargoLinkLibs = lib.escapeShellArgs ( 52 + [ 53 + # for wgpu-hal 54 + "EGL" 55 + # for xkbcommon-dl 56 + "xkbcommon" 57 + # for x11-dl, tiny-xlib, wgpu-hal 58 + "X11" 59 + # for x11-dl, tiny-xlib 60 + "X11-xcb" 61 + # for x11-dl 62 + "Xcursor" 63 + "Xi" 64 + # for x11rb 65 + "xcb" 66 + ] 67 + ++ lib.optionals (!stdenv.isDarwin) [ 68 + # for wgpu-hal, wayland-sys 69 + "wayland-client" 70 + # for wgpu-hal 71 + "wayland-egl" 72 + "vulkan" 73 + ] 74 + ); 75 + }; 76 + 77 + meta = { 78 + description = "Setup hook for configuring and wrapping applications based on libcosmic"; 79 + maintainers = with lib.maintainers; [ 80 + HeitorAugustoLN 81 + nyabinary 82 + thefossguy 83 + ]; 84 + }; 85 + } ./libcosmic-app-hook.sh