-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap.sh
executable file
·310 lines (259 loc) · 6.25 KB
/
wrap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
bwrap_opts=()
# environment variables alwyas shared with the wrapped process
# for usability and convenience
env_vars=(
EDITOR
HOME
INFOPATH
LD_LIBRARY_PATH
LIBEXEC_PATH
PAGER
PATH
PKG_CONFIG_PATH
PWD
SHELL
TERM
TERMINFO
TERMINFO_DIRS
XDG_BIN_HOME
XDG_CACHE_HOME
XDG_CONFIG_DIRS
XDG_CONFIG_HOME
XDG_CURRENT_DESKTOP
XDG_DATA_DIRS
XDG_DATA_HOME
XDG_DESKTOP_PORTAL_DIR
XDG_RUNTIME_DIR XDG_SEAT
XDG_SESSION_CLASS
XDG_SESSION_ID
XDG_SESSION_TYPE
XDG_VTNR
NIXPKGS_ALLOW_UNFREE
NIXPKGS_CONFIG
NIX_PATH
NIX_PROFILES
NIX_USER_PROFILE_DIR
LANG
LC_ADDRESS
LC_IDENTIFICATION
LC_MEASUREMENT
LC_MONETARY
LC_NAME
LC_NUMERIC
LC_PAPER
LC_TELEPHONE
LC_TIME
LOCALE_ARCHIVE
LOCALE_ARCHIVE_2_27
TZDIR
)
# environment variables only shared with the wrapped process
# when running with -d desktop access
env_vars_desktop=(
DISPLAY
WAYLAND_DISPLAY
DESKTOP_STARTUP_ID
BROWSER
GDK_BACKEND
GTK2_RC_FILES
GTK_A11Y
GTK_PATH
MOZ_ENABLE_WAYLAND
NIXOS_OZONE_WL
QT_QPA_PLATFORM
QT_WAYLAND_DISABLE_WINDOWDECORATION
QT_WAYLAND_FORCE_DPI
SDL_VIDEODRIVER
VDPAU_DRIVER
WLR_LIBINPUT_NO_DEVICES
XCURSOR_PATH
XCURSOR_SIZE
XCURSOR_THEME
)
# paths shared read only be default
paths_general=(
/nix
/etc/nix
/etc/static/nix
/bin
/usr/bin
)
usage() {
cat <<END_OF_LOGO
_ __ (_)_ ____ ___ __ __ _ _ __
| '_ \\| \\ \\/ /\\ \\ /\\ / / '__/ _\` | '_ \\
| | | | |> < \\ V V /| | | (_| | |_) |
|_| |_|_/_/\\_\\ \\_/\\_/ |_| \\__,_| .__/
|_|
END_OF_LOGO
cat <<END_OF_USAGE
Usage: wrap [OPTIONS] -- [bwrap args] [program to wrap with args]
OPTIONS:
-w PATH Mount PATH into sandbox in read write mode.
-r PATH Mount PATH into sandbox in read-only mode.
-d Allow Wayland display and rendering hardware access.
-b Allow DBus access.
-n Allow Network access.
-a Allow Audio access.
-c Allow Camera access.
-u Allow System user information access.
-e VAR Allow env var VAR access.
-v Verbose output for debugging.
ADVANCED OPTIONS:
-p Do not share current working directory. By default wrap will share
the current working directory as a write mount and cd into it
before running the program. With this option, wrap will not share
the directory and leave the current directory untouched.
-m Manual unsharing. By default wrap unshares ipc, net, pid, and uts
and tries to unshare (continue on failues) user and cgroup
namespaces. With this option, wrap does not automatically unshare
any namespaces. Use together with bwrap --unshare-* options
(man bwrap(1)) to unshare manually.
END_OF_USAGE
}
TMPDIR=$(mktemp -d)
cleanup() {
rm -rf "$TMPDIR"
}
trap cleanup EXIT
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
unshare_all=1
share_cwd=1
while getopts "r:w:e:abcdhmnpuv" opt; do
case "$opt" in
# bind / mount a path readonly in sandbox to the same path as host
r)
bwrap_opts+=(--ro-bind "$OPTARG" "$OPTARG")
;;
# bind / mount a path read/write in sandbox to the same path as host
w)
bwrap_opts+=(--bind "$OPTARG" "$OPTARG")
;;
# grant access to camera device
c)
bwrap_opts+=(--dev-bind "/dev/v4l" "/dev/v4l")
for i in /dev/video*; do
bwrap_opts+=(--dev-bind "$i" "$i")
done
;;
# grant access to dbus system
b)
dbus_socket="$(echo "$DBUS_SESSION_BUS_ADDRESS" | cut -d= -f2)"
bwrap_opts+=(--bind "$dbus_socket" "$dbus_socket")
env_vars+=(DBUS_SESSION_BUS_ADDRESS)
;;
# grant desktop access, wayland, X11, DRI
d)
bwrap_opts+=(--bind "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY")
if [ -f /dev/dri ]; then
bwrap_opts+=(--dev-bind /dev/dri /dev/dri)
fi
if [ -f /run/opengl-driver ]; then
bwrap_opts+=(--ro-bind /run/opengl-driver /run/opengl-driver)
fi
if [ -f /sys ]; then
bwrap_opts+=(--ro-bind /sys/ /sys/)
fi
if [ -f /etc/fonts ]; then
bwrap_opts+=(--ro-bind /etc/fonts /etc/fonts)
fi
env_vars+=("${env_vars_desktop[@]}")
;;
# grant network access
n)
bwrap_opts+=(--share-net)
bwrap_opts+=(--ro-bind /etc/resolv.conf /etc/resolv.conf)
bwrap_opts+=(--ro-bind /etc/ssl /etc/ssl)
bwrap_opts+=(--ro-bind /etc/static/ssl /etc/static/ssl)
;;
# grant audio access
a)
bwrap_opts+=(--bind-try "$XDG_RUNTIME_DIR/pulse/native" "$XDG_RUNTIME_DIR/pulse/native")
bwrap_opts+=(--bind "$XDG_RUNTIME_DIR/pipewire-0" "$XDG_RUNTIME_DIR/pipewire-0")
bwrap_opts+=(--bind "$XDG_RUNTIME_DIR/pipewire-0.lock" "$XDG_RUNTIME_DIR/pipewire-0.lock")
;;
# grant accss to user information
u)
bwrap_opts+=(--ro-bind /etc/passwd /etc/passwd)
bwrap_opts+=(--ro-bind /etc/group /etc/group)
env_vars+=(USER)
;;
# manually grant access to additional environment variables
e)
env_vars+=("$OPTARG")
;;
# by default nixwrap will run bwrap with --unshare-all,
# this disables it, see man bwrap(1)
m)
unshare_all=0
;;
# by default nixwrap will share the current working directory
# with the wrapped process, this disables it
p)
share_cwd=0
;;
# verbose script outputs for debugging
v)
set -x
;;
# help - display usage information
h)
usage
exit 0
;;
\?)
usage
exit 1
;;
:)
usage
exit 1
;;
*) ;;
esac
done
# Shift off the options and optional -- off $@.
shift $((OPTIND - 1))
if [[ $unshare_all -eq 1 ]]; then
bwrap_opts+=(--unshare-all "${bwrap_opts[@]}")
fi
if [[ $share_cwd -eq 1 ]]; then
cwd="$(pwd)"
bwrap_opts+=(--bind "$cwd" "$cwd")
bwrap_opts+=(--chdir "$cwd")
fi
if [ -v NIX_PROFILES ]; then
OLDIFS=$IFS
IFS=" "
for p in ${NIX_PROFILES}; do
if [[ -d "$p" ]]; then
bwrap_opts+=(--ro-bind "$p" "$p")
fi
done
IFS=$OLDIFS
fi
for p in "${paths_general[@]}"; do
if [ -d $p ]; then
bwrap_opts+=(--ro-bind "$p" "$p")
fi
done
for e in "${env_vars[@]}"; do
if [ -v "$e" ]; then
bwrap_opts+=(--setenv "$e" "${!e}")
fi
done
bwrap \
--clearenv \
--dev /dev \
--proc /proc \
--tmpfs /tmp \
--bind "$TMPDIR" "$TMPDIR" \
--dir "$HOME" \
"${bwrap_opts[@]}" \
"$@"