From 5f411a7f057ef7a12dcf2b54ca57279a5311a2aa Mon Sep 17 00:00:00 2001 From: Pratham Patel Date: Thu, 13 Feb 2025 19:14:53 +0530 Subject: [PATCH] libcosmicAppHook: init --- .../li/libcosmicAppHook/libcosmic-app-hook.sh | 96 +++++++++++++++++++ pkgs/by-name/li/libcosmicAppHook/package.nix | 85 ++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 pkgs/by-name/li/libcosmicAppHook/libcosmic-app-hook.sh create mode 100644 pkgs/by-name/li/libcosmicAppHook/package.nix diff --git a/pkgs/by-name/li/libcosmicAppHook/libcosmic-app-hook.sh b/pkgs/by-name/li/libcosmicAppHook/libcosmic-app-hook.sh new file mode 100644 index 000000000000..e6a523191956 --- /dev/null +++ b/pkgs/by-name/li/libcosmicAppHook/libcosmic-app-hook.sh @@ -0,0 +1,96 @@ +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: Lily Foster +# Portions of this code are adapted from nixos-cosmic +# https://github.com/lilyinstarlight/nixos-cosmic + +# shellcheck shell=bash +libcosmicAppWrapperArgs=() + +libcosmicAppVergenHook() { + if [ -z "${VERGEN_GIT_COMMIT_DATE-}" ]; then + # shellcheck disable=SC2155 + export VERGEN_GIT_COMMIT_DATE="$(date --utc --date=@"$SOURCE_DATE_EPOCH" '+%Y-%m-%d')" + fi +} + +libcosmicAppLinkerArgsHook() { + # Force linking to certain libraries like libEGL, which are always dlopen()ed + local flags="CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS" + + export "$flags"="${!flags-} -C link-arg=-Wl,--push-state,--no-as-needed" + # shellcheck disable=SC2043 + for lib in @cargoLinkLibs@; do + export "$flags"="${!flags} -C link-arg=-l${lib}" + done + export "$flags"="${!flags} -C link-arg=-Wl,--pop-state" +} + +preConfigurePhases+=" libcosmicAppVergenHook libcosmicAppLinkerArgsHook" + +# Add shell hook for use in dev shells +if [ -n "${IN_NIX_SHELL-}" ] && [ -z "${shellHook-}" ]; then + shellHook="libcosmicAppLinkerArgsHook && export RUSTFLAGS=\$CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS CARGO_TARGET_@cargoLinkerVar@_RUSTFLAGS=" +fi + +libcosmicAppWrapperArgsHook() { + if [ -d "${prefix:?}/share" ]; then + libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "$prefix/share") + fi + + # add fallback schemas, icons, and settings paths + libcosmicAppWrapperArgs+=(--suffix XDG_DATA_DIRS : "@fallbackXdgDirs@") +} + +preFixupPhases+=" libcosmicAppWrapperArgsHook" + +wrapLibcosmicApp() { + local program="$1" + shift 1 + wrapProgram "$program" "${libcosmicAppWrapperArgs[@]}" "$@" +} + +# Note: $libcosmicAppWrapperArgs still gets defined even if ${dontWrapLibcosmicApp-} is set +libcosmicAppWrapHook() { + # guard against running multiple times (e.g. due to propagation) + [ -z "$libcosmicAppWrapHookHasRun" ] || return 0 + libcosmicAppWrapHookHasRun=1 + + if [[ -z "${dontWrapLibcosmicApp:-}" ]]; then + targetDirsThatExist=() + targetDirsRealPath=() + + # wrap binaries + targetDirs=("${prefix}/bin" "${prefix}/libexec") + for targetDir in "${targetDirs[@]}"; do + if [[ -d "${targetDir}" ]]; then + targetDirsThatExist+=("${targetDir}") + targetDirsRealPath+=("$(realpath "${targetDir}")/") + find "${targetDir}" -type f -executable -print0 | + while IFS= read -r -d '' file; do + echo "Wrapping program '${file}'" + wrapLibcosmicApp "${file}" + done + fi + done + + # wrap links to binaries that point outside targetDirs + # Note: links to binaries within targetDirs do not need + # to be wrapped as the binaries have already been wrapped + if [[ ${#targetDirsThatExist[@]} -ne 0 ]]; then + find "${targetDirsThatExist[@]}" -type l -xtype f -executable -print0 | + while IFS= read -r -d '' linkPath; do + linkPathReal=$(realpath "${linkPath}") + for targetPath in "${targetDirsRealPath[@]}"; do + if [[ "$linkPathReal" == "$targetPath"* ]]; then + echo "Not wrapping link: '$linkPath' (already wrapped)" + continue 2 + fi + done + echo "Wrapping link: '$linkPath'" + wrapLibcosmicApp "${linkPath}" + done + fi + fi +} + +fixupOutputHooks+=(libcosmicAppWrapHook) diff --git a/pkgs/by-name/li/libcosmicAppHook/package.nix b/pkgs/by-name/li/libcosmicAppHook/package.nix new file mode 100644 index 000000000000..d7e177c42cf3 --- /dev/null +++ b/pkgs/by-name/li/libcosmicAppHook/package.nix @@ -0,0 +1,85 @@ +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: Lily Foster +# Portions of this code are adapted from nixos-cosmic +# https://github.com/lilyinstarlight/nixos-cosmic + +{ + lib, + stdenv, + makeSetupHook, + makeBinaryWrapper, + pkg-config, + targetPackages, + libGL, + libxkbcommon, + xorg, + wayland, + vulkan-loader, + + includeSettings ? true, +}: + +makeSetupHook { + name = "libcosmic-app-hook"; + + propagatedBuildInputs = [ + makeBinaryWrapper + pkg-config + ]; + + # ensure deps for linking below are available + depsTargetTargetPropagated = + assert (lib.assertMsg (!targetPackages ? raw) "libcosmicAppHook must be in nativeBuildInputs"); + [ + libGL + libxkbcommon + xorg.libX11 + xorg.libXcursor + xorg.libXi + xorg.libxcb + ] + ++ lib.optionals (!stdenv.isDarwin) [ + wayland + vulkan-loader + ]; + + substitutions = { + fallbackXdgDirs = "${lib.optionalString includeSettings "${targetPackages.cosmic-settings}/share:"}${targetPackages.cosmic-icons}/share"; + + cargoLinkerVar = stdenv.hostPlatform.rust.cargoEnvVarTarget; + # force linking for all libraries that may be dlopen'd by libcosmic/iced apps + cargoLinkLibs = lib.escapeShellArgs ( + [ + # for wgpu-hal + "EGL" + # for xkbcommon-dl + "xkbcommon" + # for x11-dl, tiny-xlib, wgpu-hal + "X11" + # for x11-dl, tiny-xlib + "X11-xcb" + # for x11-dl + "Xcursor" + "Xi" + # for x11rb + "xcb" + ] + ++ lib.optionals (!stdenv.isDarwin) [ + # for wgpu-hal, wayland-sys + "wayland-client" + # for wgpu-hal + "wayland-egl" + "vulkan" + ] + ); + }; + + meta = { + description = "Setup hook for configuring and wrapping applications based on libcosmic"; + maintainers = with lib.maintainers; [ + HeitorAugustoLN + nyabinary + thefossguy + ]; + }; +} ./libcosmic-app-hook.sh