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 export PATH="$PATH:$PWD/usr/bin"
78}
79
80wrap() {
81
82 # quite same in appimageTools
83 export APPIMAGE_SILENT_INSTALL=1
84
85 if [ -n "$APPIMAGE_DEBUG_EXEC" ]; then
86 cd "$APPDIR" || true
87 exec "$APPIMAGE_DEBUG_EXEC"
88 fi
89
90 exec "$APPDIR/AppRun" "$@"
91}
92
93usage() {
94 cat <<EOF
95Usage: appimage-run [appimage-run options] <AppImage> [AppImage options]
96
97-h show this message
98-d debug mode
99-x <directory> : extract appimage in the directory then exit.
100-w <directory> : run uncompressed appimage directory (used in appimageTools)
101
102[AppImage options]: Options are passed on to the appimage.
103If you want to execute a custom command in the appimage's environment, set the APPIMAGE_DEBUG_EXEC environment variable.
104
105EOF
106 exit 1
107}
108
109while getopts "x:w:dh" option; do
110 case "${option}" in
111 d) set -x
112 ;;
113 x) # eXtract
114 unpack_opt=true
115 APPDIR=${OPTARG}
116 ;;
117 w) # WrapAppImage
118 export APPDIR=${OPTARG}
119 wrap_opt=true
120 ;;
121 h) usage
122 ;;
123 *) usage
124 ;;
125 esac
126done
127shift "$((OPTIND-1))"
128
129if [ -n "$wrap_opt" ] && [ -d "$APPDIR" ]; then
130 wrap "$@"
131 exit
132else
133 APPIMAGE="$(realpath "$1")" || usage
134 shift
135fi
136
137if [ -n "$unpack_opt" ] && [ -f "$APPIMAGE" ]; then
138 unpack "$APPIMAGE" "$APPDIR"
139 exit
140fi
141
142if [ -n "$apprun_opt" ] && [ -f "$APPIMAGE" ]; then
143 apprun
144 wrap "$@"
145 exit
146fi