nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1#!@runtimeShell@
2# shellcheck shell=bash
3
4if [ -n "$DEBUG" ] ; then
5 set -x
6fi
7
8PATH="@path@:$PATH"
9apprun_opt=true
10OWD=$(readlink -f .)
11# can be read by appimages: https://docs.appimage.org/packaging-guide/environment-variables.html
12export OWD
13export APPIMAGE
14
15# src : AppImage
16# dest : let's unpack() create the directory
17unpack() {
18 local src="$1"
19 local out="$2"
20
21 # https://github.com/AppImage/libappimage/blob/ca8d4b53bed5cbc0f3d0398e30806e0d3adeaaab/src/libappimage/utils/MagicBytesChecker.cpp#L45-L63
22 local appimageSignature;
23 appimageSignature="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $10$11;}')"
24 local appimageType;
25 appimageType="$(LC_ALL=C readelf -h "$src" | awk 'NR==2{print $12;}')"
26
27 # check AppImage signature
28 if [ "$appimageSignature" != "4149" ]; then
29 echo "Not an AppImage file"
30 exit
31 fi
32
33 case "$appimageType" in
34 "01")
35 echo "Uncompress $(basename "$src") of type $appimageType"
36 mkdir "$out"
37 pv "$src" | bsdtar -x -C "$out" -f -
38 ;;
39
40 "02")
41 # This method avoid issues with non executable appimages,
42 # non-native packer, packer patching and squashfs-root destination prefix.
43
44 # multiarch offset one-liner using same method as AppImage
45 # see https://gist.github.com/probonopd/a490ba3401b5ef7b881d5e603fa20c93
46 offset=$(LC_ALL=C readelf -h "$src" | awk 'NR==13{e_shoff=$5} NR==18{e_shentsize=$5} NR==19{e_shnum=$5} END{print e_shoff+e_shentsize*e_shnum}')
47 echo "Uncompress $(basename "$src") of type $appimageType @ offset $offset"
48 unsquashfs -q -d "$out" -o "$offset" "$src"
49 chmod go-w "$out"
50 ;;
51
52 # "03")
53 # get ready, https://github.com/TheAssassin/type3-runtime
54
55 *)
56 echo Unsupported AppImage Type: "$appimageType"
57 exit
58 ;;
59 esac
60 echo "$(basename "$src") is now installed in $out"
61}
62
63apprun() {
64
65 SHA256=$(sha256sum "$APPIMAGE" | awk '{print $1}')
66 export APPDIR="${XDG_CACHE_HOME:-$HOME/.cache}/appimage-run/$SHA256"
67
68 #compatibility
69 if [ -x "$APPDIR/squashfs-root" ]; then APPDIR="$APPDIR/squashfs-root"; fi
70
71 if [ ! -x "$APPDIR" ]; then
72 mkdir -p "$(dirname "$APPDIR")"
73 unpack "$APPIMAGE" "$APPDIR"
74 else echo "$(basename "$APPIMAGE")" installed in "$APPDIR"
75 fi
76
77 # Fix potential for the appimages to try to import libraries from QT
78 # installed on the system, causing a version mismatch
79 unset QT_PLUGIN_PATH
80
81 export PATH="$PATH:$PWD/usr/bin"
82}
83
84wrap() {
85
86 # quite same in appimageTools
87 export APPIMAGE_SILENT_INSTALL=1
88
89 if [ -n "$APPIMAGE_DEBUG_EXEC" ]; then
90 cd "$APPDIR" || true
91 exec "$APPIMAGE_DEBUG_EXEC"
92 fi
93
94 exec "$APPDIR/AppRun" "$@"
95}
96
97usage() {
98 cat <<EOF
99Usage: appimage-run [appimage-run options] <AppImage> [AppImage options]
100
101-h show this message
102-d debug mode
103-x <directory> : extract appimage in the directory then exit.
104-w <directory> : run uncompressed appimage directory (used in appimageTools)
105
106[AppImage options]: Options are passed on to the appimage.
107If you want to execute a custom command in the appimage's environment, set the APPIMAGE_DEBUG_EXEC environment variable.
108
109EOF
110 exit 1
111}
112
113while getopts "x:w:dh" option; do
114 case "${option}" in
115 d) set -x
116 ;;
117 x) # eXtract
118 unpack_opt=true
119 APPDIR=${OPTARG}
120 ;;
121 w) # WrapAppImage
122 export APPDIR=${OPTARG}
123 wrap_opt=true
124 ;;
125 h) usage
126 ;;
127 *) usage
128 ;;
129 esac
130done
131shift "$((OPTIND-1))"
132
133if [ -n "$wrap_opt" ] && [ -d "$APPDIR" ]; then
134 wrap "$@"
135 exit
136else
137 APPIMAGE="$(realpath "$1")" || usage
138 shift
139fi
140
141if [ -n "$unpack_opt" ] && [ -f "$APPIMAGE" ]; then
142 unpack "$APPIMAGE" "$APPDIR"
143 exit
144fi
145
146if [ -n "$apprun_opt" ] && [ -f "$APPIMAGE" ]; then
147 apprun
148 wrap "$@"
149 exit
150fi