Skip to content

Comments

Add sandboxed building for FreeBSD using jails#9968

Open
rhelmot wants to merge 5 commits intoNixOS:masterfrom
rhelmot:freebsd
Open

Add sandboxed building for FreeBSD using jails#9968
rhelmot wants to merge 5 commits intoNixOS:masterfrom
rhelmot:freebsd

Conversation

@rhelmot
Copy link
Contributor

@rhelmot rhelmot commented Feb 8, 2024

Motivation

Build isolation is good! In Linux, this is accomplished with namespaces (containers). The equivalent technology on FreeBSD is jails.

Context

This is part of my ongoing project to make FreeBSD a first class citizen in the nix world.

This was a fairly simple patch, just needed to add parallel implementations for all the sandboxed Linux build pieces.

The most fragile part of this implementation is the fact that there is a lot of global state that gets set up in order to construct the jail - the chroot dir in the nix store, the nullfs mounts (the FreeBSD equivalent of a bind mount), and the jail ID itself. Lots of steps have been taken to make sure these all get cleaned up, both at the end of the build and at the start of any rebuilds. It seems to be resilient to interruption.

This has been live-fire tested with my fork of nixpkgs for FreeBSD. It is able to build the stdenv without issue.

Please squash-merge this PR! It includes some changes that were later reverted, which don’t belong in this repository but instead in the FreeBSD ports repository.

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@Ericson2314

This comment was marked as resolved.

@rhelmot

This comment was marked as resolved.

@Ericson2314

This comment was marked as resolved.

@edolstra

This comment was marked as resolved.

@Ericson2314

This comment was marked as resolved.

@rhelmot

This comment was marked as resolved.

@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Feb 9, 2024
@rhelmot rhelmot mentioned this pull request Feb 10, 2024
13 tasks
@Ericson2314

This comment was marked as resolved.

@rhelmot

This comment was marked as resolved.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-02-12-nix-team-meeting-minutes-123/39775/1

@L-as

This comment was marked as resolved.

@Ericson2314

This comment was marked as resolved.

@Ericson2314

This comment was marked as resolved.

Ericson2314 added a commit that referenced this pull request May 27, 2025
This is the utility changes from #9968, which were easier to rebase
first.
Ericson2314 added a commit that referenced this pull request May 27, 2025
This is the utility changes from #9968, which were easier to rebase
first.

I (@Ericson2314) didn't write this code; I just rebased it.

Co-Authored-By: Artemis Tosini <me@artem.ist>
Co-Authored-By: Audrey Dutcher <audrey@rhelmot.io>
@Ericson2314 Ericson2314 force-pushed the freebsd branch 5 times, most recently from 6e711ce to d1a4478 Compare May 27, 2025 20:19
Comment on lines +352 to +357
// There's also just no simple way to do this correctly, you have to manually
// inotify watch the files for changes on the outside and update the sandbox
// while the build is running (or at least that's what Flatpak does).
//
// I also just generally feel icky about modifying sandbox state under a build,
// even though it really shouldn't be a big deal. -K900
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean this needs proper attribution to k900? Is this somehow cherry-picked from Lix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be me. I thought I was just copying over code from @rhelmot and @artemist, much of which was also PR'd here too, but maybe at some point some other people also contributed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a second change id and a co-authored-by


void startChild() override
{
int jid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the local variable? We should be able to assign directly to the AutoRemoveJail and work with just a single variable instead of 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this good now? I kept the local variable with a very minimal scope so I wouldn't assign it with a negative number.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we don't need the del variable at all.

@Ericson2314
Copy link
Member

Ericson2314 commented Dec 15, 2025

I'm happy to work with @xokdvium to land these C++ best practices changes.

@rhelmot what would be a better use of your time and unique knowledge is retroactively reviewing the rest of what I did in #13281 that this PR doesn't touch. In particular it would be nice if we can get rid of the FreeBSD-only argument to _deletePath.

@Ericson2314 Ericson2314 force-pushed the freebsd branch 3 times, most recently from ce31fa1 to 209a5f1 Compare January 6, 2026 02:44
@zowoq

This comment was marked as resolved.

@Ericson2314 Ericson2314 force-pushed the freebsd branch 3 times, most recently from 47d8794 to 01fe2d5 Compare January 6, 2026 03:59
@Ericson2314
Copy link
Member

This one has been held up for a while, but there is one last PR I would like to hold it up for, which is #14788. That should hopefully be removed momentarily. The interaction is that it reworks the cleanup of DerivationBuilder slightly.

In the meantime, @xokdvium, what do you think of the "Clean up AutoRemoveJail and AutoDelete" commit I made? Can we land that right away?

@Ericson2314
Copy link
Member

@zowoq

@Ericson2314 Once #14921 is merged could you rebase this PR again please?

Done!

@zowoq
Copy link
Contributor

zowoq commented Jan 6, 2026

Before the recent rebases (9799fcb) it was working but seems to be broken now:

> nix build github:nixos/nixpkgs#stdenv --max-jobs 1

...

error: opening file '/bin/sh': Text file busy

Ericson2314 and others added 5 commits January 24, 2026 16:34
- Create methods for what the destructors call. Arguably `reset` should
  use this, to make it more consistent with smart pointers in the
  standard library.

- Use `std::filesystem::path` in `AutoRemoveJail`

- Remove `del` from `AutoRemoveJail` because it is not needed.
The <() process substitution syntax doesn't work for this one testcase
in bash for FreeBSD. The exact reason for this is unknown, possibly to
do with pipe vs file vs fifo EOF behavior. The prior behavior was this
test hanging forever, with no children of the bash process.

Change-Id: I71822a4b9dea6059b34300568256c5b7848109ac

(cherry picked from commit ae628d4)
Adapted from Change-Id I071e6ae7e220884690b788d94f480866f428db71

This will be used in the next commit.

Co-authored-by: K900 <me@0upti.me>
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority

Projects

Status: 🏁 Review

Development

Successfully merging this pull request may close these issues.

8 participants