2021-01-25 08:26:54 +00:00
|
|
|
{ lib, callPackage, runCommandLocal, writeShellScriptBin, coreutils, bubblewrap }:
|
2020-07-31 13:58:03 +01:00
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
args @ {
|
2020-12-09 02:20:44 +00:00
|
|
|
name
|
|
|
|
, runScript ? "bash"
|
|
|
|
, extraInstallCommands ? ""
|
|
|
|
, meta ? {}
|
|
|
|
, passthru ? {}
|
|
|
|
, unshareUser ? true
|
|
|
|
, unshareIpc ? true
|
|
|
|
, unsharePid ? true
|
|
|
|
, unshareNet ? false
|
|
|
|
, unshareUts ? true
|
|
|
|
, unshareCgroup ? true
|
|
|
|
, ...
|
2019-02-20 15:22:17 +00:00
|
|
|
}:
|
2020-07-31 13:58:03 +01:00
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
with builtins;
|
2020-07-31 13:58:03 +01:00
|
|
|
let
|
2020-12-09 02:20:44 +00:00
|
|
|
buildFHSEnv = callPackage ./env.nix { };
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
env = buildFHSEnv (removeAttrs args [
|
|
|
|
"runScript" "extraInstallCommands" "meta" "passthru"
|
2020-12-09 02:20:44 +00:00
|
|
|
"unshareUser" "unshareCgroup" "unshareUts" "unshareNet" "unsharePid" "unshareIpc"
|
2019-02-20 15:22:17 +00:00
|
|
|
]);
|
2020-07-31 13:58:03 +01:00
|
|
|
|
2019-09-15 14:29:53 +01:00
|
|
|
etcBindFlags = let
|
|
|
|
files = [
|
|
|
|
# NixOS Compatibility
|
|
|
|
"static"
|
|
|
|
# Users, Groups, NSS
|
|
|
|
"passwd"
|
|
|
|
"group"
|
|
|
|
"shadow"
|
|
|
|
"hosts"
|
|
|
|
"resolv.conf"
|
|
|
|
"nsswitch.conf"
|
2021-01-26 00:41:50 +00:00
|
|
|
# User profiles
|
|
|
|
"profiles"
|
2019-09-15 14:29:53 +01:00
|
|
|
# Sudo & Su
|
|
|
|
"login.defs"
|
|
|
|
"sudoers"
|
|
|
|
"sudoers.d"
|
|
|
|
# Time
|
|
|
|
"localtime"
|
|
|
|
"zoneinfo"
|
|
|
|
# Other Core Stuff
|
|
|
|
"machine-id"
|
|
|
|
"os-release"
|
|
|
|
# PAM
|
|
|
|
"pam.d"
|
|
|
|
# Fonts
|
|
|
|
"fonts"
|
|
|
|
# ALSA
|
|
|
|
"asound.conf"
|
|
|
|
# SSL
|
|
|
|
"ssl/certs"
|
|
|
|
"pki"
|
|
|
|
];
|
2020-09-20 16:02:08 +01:00
|
|
|
in concatStringsSep "\n "
|
2019-09-15 14:29:53 +01:00
|
|
|
(map (file: "--ro-bind-try /etc/${file} /etc/${file}") files);
|
|
|
|
|
2019-02-20 15:22:17 +00:00
|
|
|
init = run: writeShellScriptBin "${name}-init" ''
|
|
|
|
source /etc/profile
|
|
|
|
exec ${run} "$@"
|
|
|
|
'';
|
|
|
|
|
2020-08-17 08:49:34 +01:00
|
|
|
bwrapCmd = { initArgs ? "" }: ''
|
2020-09-20 16:02:08 +01:00
|
|
|
blacklist=(/nix /dev /proc /etc)
|
|
|
|
ro_mounts=()
|
2019-02-20 15:22:17 +00:00
|
|
|
for i in ${env}/*; do
|
2020-07-31 13:58:03 +01:00
|
|
|
path="/''${i##*/}"
|
2019-09-15 14:29:53 +01:00
|
|
|
if [[ $path == '/etc' ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
2020-09-20 16:02:08 +01:00
|
|
|
ro_mounts+=(--ro-bind "$i" "$path")
|
|
|
|
blacklist+=("$path")
|
2020-07-31 13:58:03 +01:00
|
|
|
done
|
|
|
|
|
2019-09-15 14:29:53 +01:00
|
|
|
if [[ -d ${env}/etc ]]; then
|
|
|
|
for i in ${env}/etc/*; do
|
|
|
|
path="/''${i##*/}"
|
2020-09-20 16:02:08 +01:00
|
|
|
ro_mounts+=(--ro-bind "$i" "/etc$path")
|
2019-09-15 14:29:53 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2020-10-28 20:00:17 +00:00
|
|
|
declare -a auto_mounts
|
2019-02-20 15:22:17 +00:00
|
|
|
# loop through all directories in the root
|
|
|
|
for dir in /*; do
|
|
|
|
# if it is a directory and it is not in the blacklist
|
2020-09-20 16:02:08 +01:00
|
|
|
if [[ -d "$dir" ]] && [[ ! "''${blacklist[@]}" =~ "$dir" ]]; then
|
2019-02-20 15:22:17 +00:00
|
|
|
# add it to the mount list
|
2020-10-28 20:00:17 +00:00
|
|
|
auto_mounts+=(--bind "$dir" "$dir")
|
2019-02-20 15:22:17 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-07-31 13:58:03 +01:00
|
|
|
|
2020-09-20 16:02:08 +01:00
|
|
|
cmd=(
|
|
|
|
${bubblewrap}/bin/bwrap
|
|
|
|
--dev-bind /dev /dev
|
|
|
|
--proc /proc
|
|
|
|
--chdir "$(pwd)"
|
2020-12-09 02:20:44 +00:00
|
|
|
${lib.optionalString unshareUser "--unshare-user"}
|
|
|
|
${lib.optionalString unshareIpc "--unshare-ipc"}
|
|
|
|
${lib.optionalString unsharePid "--unshare-pid"}
|
|
|
|
${lib.optionalString unshareNet "--unshare-net"}
|
|
|
|
${lib.optionalString unshareUts "--unshare-uts"}
|
|
|
|
${lib.optionalString unshareCgroup "--unshare-cgroup"}
|
2020-09-20 16:02:08 +01:00
|
|
|
--die-with-parent
|
|
|
|
--ro-bind /nix /nix
|
|
|
|
${etcBindFlags}
|
|
|
|
"''${ro_mounts[@]}"
|
|
|
|
"''${auto_mounts[@]}"
|
2020-08-17 08:49:34 +01:00
|
|
|
${init runScript}/bin/${name}-init ${initArgs}
|
2020-09-20 16:02:08 +01:00
|
|
|
)
|
|
|
|
exec "''${cmd[@]}"
|
2020-07-31 13:58:03 +01:00
|
|
|
'';
|
|
|
|
|
2020-08-17 08:49:34 +01:00
|
|
|
bin = writeShellScriptBin name (bwrapCmd { initArgs = ''"$@"''; });
|
2019-02-20 15:22:17 +00:00
|
|
|
|
2020-07-31 13:58:03 +01:00
|
|
|
in runCommandLocal name {
|
|
|
|
inherit meta;
|
|
|
|
|
|
|
|
passthru = passthru // {
|
|
|
|
env = runCommandLocal "${name}-shell-env" {
|
2020-08-17 08:49:34 +01:00
|
|
|
shellHook = bwrapCmd {};
|
2020-07-31 13:58:03 +01:00
|
|
|
} ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** User chroot 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
2019-02-20 15:22:17 +00:00
|
|
|
ln -s ${bin}/bin/${name} $out/bin/${name}
|
2020-07-31 13:58:03 +01:00
|
|
|
${extraInstallCommands}
|
|
|
|
''
|