Skip to content

Commit 595d3e9

Browse files
rhelmotartemistK900Ericson2314xokdvium
committed
Add support for sandboxing on FreeBSD
New FreeBSD sandboxes are based on jails and chroots. They provide fairly similar capabilities to sandboxes on Linux and allow for pure builds of FreeBSD nixpkgs. Although it would also be possble to use jails for Linux emulation, that is not supported with this commit. Change-Id: I619e1e34c56de7aaa64a38408210a410bb13adba Change-Id: I071e6ae7e220884690b788d94f480866f428db71 Co-Authored-By: Artemis Tosini <me@artem.ist> Co-Authored-By: K900 <me@0upti.me> Co-Authored-By: John Ericson <John.Ericson@Obsidian.Systems> Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
1 parent 1b03c3a commit 595d3e9

5 files changed

Lines changed: 529 additions & 2 deletions

File tree

src/libstore/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ endif
151151
configdata_priv.set('HAVE_SECCOMP', seccomp.found().to_int())
152152
deps_private += seccomp
153153

154+
if host_machine.system() == 'freebsd'
155+
# libjail is not in freebsd.libc, but there's no discovery mechanism
156+
libjail = declare_dependency(link_args : [ '-ljail' ])
157+
deps_other += libjail
158+
endif
159+
154160
nlohmann_json = dependency('nlohmann_json', version : '>= 3.9')
155161
deps_public += nlohmann_json
156162

src/libstore/package.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
unixtools,
77
darwin,
8+
freebsd,
89

910
nix-util,
1011
boost,
@@ -65,6 +66,7 @@ mkMesonLibrary (finalAttrs: {
6566
sqlite
6667
]
6768
++ lib.optional stdenv.hostPlatform.isLinux libseccomp
69+
++ lib.optional stdenv.hostPlatform.isFreeBSD freebsd.libjail
6870
++ lib.optional withAWS aws-crt-cpp;
6971

7072
propagatedBuildInputs = [

src/libstore/unix/build/chroot-derivation-builder.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifdef __linux__
1+
#if defined(__linux__) || defined(__FreeBSD__)
22

33
namespace nix {
44

@@ -52,13 +52,16 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
5252
return buildUser->getGID();
5353
}
5454

55+
virtual void extraChrootParentDirCleanup(const std::filesystem::path & chrootParentDir) {}
56+
5557
void prepareSandbox() override
5658
{
5759
/* Create a temporary directory in which we set up the chroot
5860
environment using bind-mounts. We put it in the Nix store
5961
so that the build outputs can be moved efficiently from the
6062
chroot to their final location. */
6163
std::filesystem::path chrootParentDir = store.toRealPath(drvPath) + ".chroot";
64+
extraChrootParentDirCleanup(chrootParentDir);
6265
deletePath(chrootParentDir);
6366

6467
/* Clean up the chroot directory automatically. */

src/libstore/unix/build/derivation-builder.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,13 +1273,22 @@ void DerivationBuilderImpl::runChild(RunChildArgs args)
12731273
}
12741274
}
12751275

1276+
#if defined(__FreeBSD__)
1277+
/* Close all other file descriptors. This must happen before
1278+
* enterChroot for FreeBSD. */
1279+
unix::closeExtraFDs();
1280+
#endif
1281+
12761282
enterChroot();
12771283

12781284
if (chdir(tmpDirInSandbox().c_str()) == -1)
12791285
throw SysError("changing into %1%", tmpDir);
12801286

1281-
/* Close all other file descriptors. */
1287+
#if !defined(__FreeBSD__)
1288+
/* Close all other file descriptors. This must happen after
1289+
* enterChroot for Linux. */
12821290
unix::closeExtraFDs();
1291+
#endif
12831292

12841293
/* Disable core dumps by default. */
12851294
struct rlimit limit = {0, RLIM_INFINITY};
@@ -1964,6 +1973,7 @@ StorePath DerivationBuilderImpl::makeFallbackPath(const StorePath & path)
19641973
// FIXME: do this properly
19651974
#include "chroot-derivation-builder.cc"
19661975
#include "linux-derivation-builder.cc"
1976+
#include "freebsd-derivation-builder.cc"
19671977
#include "darwin-derivation-builder.cc"
19681978
#include "external-derivation-builder.cc"
19691979

@@ -2026,6 +2036,11 @@ std::unique_ptr<DerivationBuilder> makeDerivationBuilder(
20262036
return std::make_unique<ChrootLinuxDerivationBuilder>(store, std::move(miscMethods), std::move(params));
20272037

20282038
return std::make_unique<LinuxDerivationBuilder>(store, std::move(miscMethods), std::move(params));
2039+
#elif defined(__FreeBSD__)
2040+
if (useSandbox)
2041+
return std::make_unique<ChrootFreeBSDDerivationBuilder>(store, std::move(miscMethods), std::move(params));
2042+
2043+
return std::make_unique<FreeBSDDerivationBuilder>(store, std::move(miscMethods), std::move(params));
20292044
#else
20302045
if (useSandbox)
20312046
throw Error("sandboxing builds is not supported on this platform");

0 commit comments

Comments
 (0)