Skip to content

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Oct 2, 2024

This is a subset of aba55d1 (#67336)[1] that I (Ma27) am using for quite a while in my systemd-nspawn setup (without nixos-container) to have unprivileged containers.

Recently, @lheckemann reminded me that this isn't part of upstream NixOS and their setup fails like this when activating config in an nspawn instance (no shared store):

stderr) activating the configuration...
stdout) setting up /etc...
stderr) mount: /dev: permission denied.
stderr)        dmesg(1) may have more information after failed mount system call.
stderr) mount: /dev/pts: permission denied.
stderr)        dmesg(1) may have more information after failed mount system call.
stderr) mount: /dev/shm: permission denied.
stderr)        dmesg(1) may have more information after failed mount system call.
stderr) mount: /run: permission denied.
stderr)        dmesg(1) may have more information after failed mount system call.
stdout) Activation script snippet 'specialfs' failed (32)

So I decided to submit this portion again.

[1] Hence I retained the original authorship.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Oct 2, 2024
@Ma27 Ma27 requested a review from a team October 2, 2024 09:50
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Oct 2, 2024
@aanderse
Copy link
Member

aanderse commented Oct 2, 2024

isContainer is far reaching in implications, much further than it was ever intended

people use this value for all sorts of containers, not just nspawn, so at bare minimum we should consider this a potentially breaking change...

release notes would be good, but let's see if we can test this change out on a bunch of different container technologies and see if there is any impact?

does arion depend on this? what about incus? etc...

cc @adamcstephens for informational purposes

Copy link
Contributor

@adamcstephens adamcstephens left a comment

Choose a reason for hiding this comment

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

Thanks @aanderse for the ping. This is indeed a breaking change for incus (and probably lxc) containers.

Perhaps now is a good time to remove some of the overload on isContainer? Maybe this PR could instead introduce an option specifically for nspawn, and condition based on that?

@adamcstephens
Copy link
Contributor

I won't run ofborg on these yet, because they won't succeed but incus can be tested by building incus-lts.passthru.tests.container-systemd-init and incus-lts.passthru.tests.container-legacy-init

@Ma27
Copy link
Member Author

Ma27 commented Oct 2, 2024

isContainer is far reaching in implications, much further than it was ever intended

and even further than I was aware of I guess.

Maybe this PR could instead introduce an option specifically for nspawn, and condition based on that?

Yeah, sounds good.
This will be the only place I'll special-case for nspawn for now.

@aanderse
Copy link
Member

aanderse commented Oct 2, 2024

This will be the only place I'll special-case for nspawn for now.

the situation may not be so dire... in many cases we can deal directly with systemd units which have a convenient condition directive to check for nspawn... so this may be one of the only places we would actually need to use an option like this

@arianvp
Copy link
Member

arianvp commented Oct 4, 2024

I actually think we want to get rid of specialFileSystems at some point. All these filesystems are already getting mounted by systemd and now that we have systemd in initrd they also get mounted early enough

@aanderse
Copy link
Member

aanderse commented Oct 4, 2024

what about when we don't have initrd? looks like stage 2 references /run once

that single line should probably be fine to move to a systemd service (or even tmpfiles rule that only runs on boot?)... cool! great suggestion @arianvp

@Ma27
Copy link
Member Author

Ma27 commented Oct 6, 2024

I actually think we want to get rid of specialFileSystems at some point

Sounds reasonable.
That said, the issue is relatively trivial to fix (even when introducing a isNspawn-like option), I think it's still worth fixing.

@aanderse
Copy link
Member

aanderse commented Oct 6, 2024

worth fixing, but how?

so we still need special filesystems or can we already get rid of it and not worry about it anymore?

🤔

@Ma27
Copy link
Member Author

Ma27 commented Oct 6, 2024

Well, the reason I'm doing this change is to resolve the error I pasted above which occurs when having an nspawn container with its onw Nix store inside and activating a new system configuration in that.

For that problem, there's a fix and just because we should probably remove the entire specialFilesystems part (or even all of non-systemd initrd - which I agree with fwiw), it's still worth fixing problems for as long as it exists.

@github-actions github-actions bot added 8.has: documentation This PR adds or changes documentation 8.has: changelog This PR adds or changes release notes labels Oct 18, 2024
@Ma27
Copy link
Member Author

Ma27 commented Oct 18, 2024

Pushed another commit addressing these concerns. WDYT @aanderse @adamcstephens ?

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Nov 10, 2024
Copy link
Contributor

@adamcstephens adamcstephens left a comment

Choose a reason for hiding this comment

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

Sorry I dropped the ball on this. Looks good to me.

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Nov 24, 2024
@wegank wegank added 12.approvals: 2 This PR was reviewed and approved by two persons. and removed 12.approvals: 1 This PR was reviewed and approved by one person. labels Dec 30, 2024
@nixpkgs-ci nixpkgs-ci bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 5, 2025
@Ma27 Ma27 force-pushed the unprivileged-containers-fix branch 2 times, most recently from 7a2dc72 to c98f5a1 Compare August 12, 2025 21:55
@nixpkgs-ci nixpkgs-ci bot removed 2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md labels Aug 12, 2025
@Ma27 Ma27 force-pushed the unprivileged-containers-fix branch from c98f5a1 to bc9ab1e Compare August 12, 2025 22:28
@Ma27 Ma27 requested a review from ck3d August 12, 2025 22:28
@nixpkgs-ci nixpkgs-ci bot added the 6.topic: nixos-container Imperative and declarative systemd-nspawn containers label Aug 13, 2025
@Ma27 Ma27 force-pushed the unprivileged-containers-fix branch from 2acfc45 to 421ce3b Compare August 13, 2025 05:28
This is a subset of aba55d1 (NixOS#67336)[1]
that I (Ma27) am using for quite a while in my systemd-nspawn setup
(without `nixos-container`) to have unprivileged containers.

Recently, Linus reminded me that this isn't part of upstream NixOS and
their setup fails like this when activating config in an nspawn
instance (no shared store):

    stderr) activating the configuration...
    stdout) setting up /etc...
    stderr) mount: /dev: permission denied.
    stderr)        dmesg(1) may have more information after failed mount system call.
    stderr) mount: /dev/pts: permission denied.
    stderr)        dmesg(1) may have more information after failed mount system call.
    stderr) mount: /dev/shm: permission denied.
    stderr)        dmesg(1) may have more information after failed mount system call.
    stderr) mount: /run: permission denied.
    stderr)        dmesg(1) may have more information after failed mount system call.
    stdout) Activation script snippet 'specialfs' failed (32)

So I decided to submit this portion again.

[1] Hence I retained the original authorship.

Co-authored-by: Maximilian Bosch <maximilian@mbosch.me>
@Ma27 Ma27 force-pushed the unprivileged-containers-fix branch from 421ce3b to 0bb2e4b Compare August 23, 2025 13:27
Ma27 added 2 commits August 30, 2025 13:02
There are a bunch of components such as incus or LXC that also use
`boot.isContainer`, so we'd have to differentiate between "OS container"
and "actually nspawn".

This became necessary for the file-systems part where nspawn takes care
of setting up special filesystems like `/proc`, `/dev` etc., but others
don't.

To allow for a `boot.isContainer` being less overloaded, this introduces
`boot.isNspawnContainer` that is exclusively used for nspawn-specific
things. When `true`, `boot.isContainer = true;` is implied.
@Ma27 Ma27 force-pushed the unprivileged-containers-fix branch from 0bb2e4b to 8edd84e Compare August 30, 2025 11:02
@Ma27 Ma27 requested a review from ElvishJerricco August 30, 2025 11:05
@nixpkgs-ci nixpkgs-ci bot added 12.approvals: 3+ This PR was reviewed and approved by three or more persons. 2.status: merge conflict This PR has merge conflicts with the target branch and removed 12.approvals: 2 This PR was reviewed and approved by two persons. labels Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: nixos-container Imperative and declarative systemd-nspawn containers 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 3+ This PR was reviewed and approved by three or more persons.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants