Skip to content

Commit

Permalink
Merge pull request #98325 from Atemu/fhsenv-bw-bash-lists
Browse files Browse the repository at this point in the history
buildFHSUserEnvBubblewrap: use arrays for constructing argument list
  • Loading branch information
kevincox committed Nov 22, 2020
2 parents 8a4cf08 + 74c4a55 commit 2727448
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions pkgs/build-support/build-fhs-userenv-bubblewrap/default.nix
Expand Up @@ -50,7 +50,7 @@ let
"ssl/certs"
"pki"
];
in concatStringsSep " \\\n "
in concatStringsSep "\n "
(map (file: "--ro-bind-try /etc/${file} /etc/${file}") files);

init = run: writeShellScriptBin "${name}-init" ''
Expand All @@ -59,46 +59,49 @@ let
'';

bwrapCmd = { initArgs ? "" }: ''
blacklist="/nix /dev /proc /etc"
ro_mounts=""
blacklist=(/nix /dev /proc /etc)
ro_mounts=()
for i in ${env}/*; do
path="/''${i##*/}"
if [[ $path == '/etc' ]]; then
continue
fi
ro_mounts="$ro_mounts --ro-bind $i $path"
blacklist="$blacklist $path"
ro_mounts+=(--ro-bind "$i" "$path")
blacklist+=("$path")
done
if [[ -d ${env}/etc ]]; then
for i in ${env}/etc/*; do
path="/''${i##*/}"
ro_mounts="$ro_mounts --ro-bind $i /etc$path"
ro_mounts+=(--ro-bind "$i" "/etc$path")
done
fi
declare -a auto_mounts
# loop through all directories in the root
for dir in /*; do
# if it is a directory and it is not in the blacklist
if [[ -d "$dir" ]] && grep -v "$dir" <<< "$blacklist" >/dev/null; then
if [[ -d "$dir" ]] && [[ ! "''${blacklist[@]}" =~ "$dir" ]]; then
# add it to the mount list
auto_mounts+=(--bind "$dir" "$dir")
fi
done
exec ${bubblewrap}/bin/bwrap \
--dev-bind /dev /dev \
--proc /proc \
--chdir "$(pwd)" \
--unshare-all \
--share-net \
--die-with-parent \
--ro-bind /nix /nix \
${etcBindFlags} \
$ro_mounts \
"''${auto_mounts[@]}" \
cmd=(
${bubblewrap}/bin/bwrap
--dev-bind /dev /dev
--proc /proc
--chdir "$(pwd)"
--unshare-all
--share-net
--die-with-parent
--ro-bind /nix /nix
${etcBindFlags}
"''${ro_mounts[@]}"
"''${auto_mounts[@]}"
${init runScript}/bin/${name}-init ${initArgs}
)
exec "''${cmd[@]}"
'';

bin = writeShellScriptBin name (bwrapCmd { initArgs = ''"$@"''; });
Expand Down

0 comments on commit 2727448

Please sign in to comment.