Skip to content

Add support for RFC108 imperative containers - #216025

Closed
m1cr0man wants to merge 1 commit into
NixOS:masterfrom
m1cr0man:rfc108-minimal
Closed

m1cr0man wants to merge 1 commit into
NixOS:masterfrom
m1cr0man:rfc108-minimal

Conversation

@m1cr0man

@m1cr0man m1cr0man commented Feb 12, 2023 •

Copy link
Copy Markdown
Contributor
Description of changes

This is a fork of #140669 in a minified and refactored form covering the changes which cannot be placed in a separate flake.

I have been working on python-nixos-nspawn which implements all the components for managing imperative RFC108 style containers. I have been able to refactor almost all parts of the original POC to work within this repo/flake however I could not find a satisfactory way to otherwise apply the changes covered in this PR.

  • Skip some more mountpoints within containers which are covered by systemd-nspawn
  • Avoid altering imperative containers and add the necessary logic to handle declarative containers in switch-to-configuration

With this, it would be possible to use python-nixos-nspawn as a tool or module to manage RFC108 containers on any system with networkd enabled + configured appropriately.

TODO

None - all done

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • 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/)
  • 23.05 Release Notes (or backporting 22.11 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.

CC @Ma27

@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 Feb 12, 2023
@ofborg ofborg Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Feb 12, 2023
@m1cr0man m1cr0man mentioned this pull request Jun 29, 2023
3 of 12 tasks
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. labels Jul 16, 2023
@ofborg ofborg Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Aug 19, 2023
@github-actions github-actions Bot added the 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. label Nov 15, 2023
@ofborg ofborg Bot removed the 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. label Nov 15, 2023
@m1cr0man
m1cr0man force-pushed the rfc108-minimal branch 2 times, most recently from 103994b to f8be66a Compare November 22, 2023 22:36
@github-actions github-actions Bot added 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. and removed 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. labels Dec 6, 2023
@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 23, 2026
@RossSmyth

RossSmyth commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Gave a look over the nix stuff and it looks fine, but unfortunately I do not have a lot of systemd-specific knowledge to say about the config changes.

It would also be nice to have some nixos tests dedicated to this feature.
https://github.com/NixOS/nixpkgs/tree/master/nixos/tests

I'll give the rust a look later.

@jmbaur

jmbaur commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

I don't think it's acceptable to add so much nspawn related logic hardcoded into switch-to-configuration. I'm struggling to see the use-case we're trying to solve here, since it doesn't look like any of the X-ActivationStrategy stuff is used. If the containers are imperative, shouldn't there be no interaction needed with switch-to-configuration at all?

@m1cr0man

Copy link
Copy Markdown
Contributor Author

It would also be nice to have some nixos tests dedicated to this feature.

Thanks for the review! I do actually have some NixOS tests for all of this in m1cr0man/python-nixos-nspawn which I used to validate the latest set of commits here. I will port those into NixOS if/when the whole of RFC108 can be mainlined. This PR is meant to cover only the changes which I cannot perform outside of nixpkgs.

I don't think it's acceptable to add so much nspawn related logic hardcoded into switch-to-configuration. I'm struggling to see the use-case we're trying to solve here, since it doesn't look like any of the X-ActivationStrategy stuff is used. If the containers are imperative, shouldn't there be no interaction needed with switch-to-configuration at all?

The main issue these changes solve is that imperative containers would otherwise be shut down by switch-to-configuration. Systemd-nspawn containers exist both as /etc/systemd/nspawn/$NAME.nspawn and /etc/systemd/system/systemd-nspawn@$NAME.service. This is a special relationship unique amongst systemd units to nspawn itself. If an imperative container is running when the config switch is invoked, the script will misinterpret the imperative container as having been deleted and shut it down.

Additionally, there is logic to handle the changing (and ignoring) of specific fields in the .nspawn file. Specifically, we need to handle changes to the Parameters which holds the path to the system init script, and X-ActivationStrategy which stores the activationStrategy. This field is added in the modules defined in my other repo, and is not part of this change. (Frankly, I would prefer if Nix ignored all X- prefixed fields for easy customisation of unit files.) Subject to the changes to either file, we will decide whether container needs to be started, stopped, restarted or reloaded. This is a feature explicitly documented in the RFC itself.

The intention of this PR has always been to add the minimal set of changes to nixpkgs to make RFC108 operational on NixOS. I really can't see a solution to making things work correctly which does not involve special code in switch-to-configuration. I don't like it either honestly, but given that unique setup of nspawn containers I mentioned, it is a necessary evil. Maintaining this patchset over the last 2 years has been quite painful - it started off being part of switch-to-configuration.pl and I had to then perform the Rust port. I'd really like to get this merged so I can focus on the container modules themselves.

@m1cr0man

Copy link
Copy Markdown
Contributor Author

I'm testing a solution which requires no changes to STC and no new unit attributes. Hope to report back soon.

@m1cr0man

m1cr0man commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

OK, it has been done. It wasn't zero changes to STC... because I found a bug. None of the changes are technically specific to RFC108 either - they just so happen to be necessary to make it work.

All my commit descriptions go into detail on what has been done here. Looking forward to the reviews 😄

@m1cr0man
m1cr0man force-pushed the rfc108-minimal branch 2 times, most recently from 6e73503 to 9567dd8 Compare April 2, 2026 22:38
Comment thread nixos/modules/config/resolvconf.nix Resolved
Comment thread nixos/modules/system/boot/resolved.nix Resolved
@jmbaur

jmbaur commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

OK, it has been done. It wasn't zero changes to STC... because I found a bug. None of the changes are technically specific to RFC108 either - they just so happen to be necessary to make it work.

All my commit descriptions go into detail on what has been done here. Looking forward to the reviews 😄

I think the main issue I see with the changes introduced in this PR is actually a surfacing of an issue with how NixOS manages systemd units. Systemd intends for distros to have three places that units can be managed:

  1. a read-only place managed by the vendor/manufacturer of the system
  2. a read-write place managed by the end-user
  3. a read-write, ephemeral place managed by software at runtime

NixOS has the latter two, but lacks the first one (traditionally located at /usr/lib) outside of the first-party units shipped with systemd itself (located in systemd's derivation output path). Because of that, switch-to-configuration and the etc builder for NixOS resorts to managing the units created at build time in /etc/, which ends up conflating the vendor managed units with the end-user managed units, and causes issues for use cases like this, where units are intentionally being managed outside of the NixOS configuration.

@jmbaur

jmbaur commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

@ofborg test switchTest

@m1cr0man

m1cr0man commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

conflating the vendor managed units with the end-user managed units

To be honest, I don't view this as a problem specific to NixOS. For any systemd distro, you don't really want to edit the vendored unit files. The overrides files are a sufficient way to extend unit configuration if you want to, and otherwise declaring a unit in full affects the result on NixOS the same as it would on another distro. It's a matter of making the correct and conscious decision on how you would like to declare your unit.

Network units, on the other hand, are a total pain. You can't mix the vendor config with your own as they use wildcard match patterns. I had some fun working around that in the rest of the module in my other repo.

@jmbaur

jmbaur commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

conflating the vendor managed units with the end-user managed units

To be honest, I don't view this as a problem specific to NixOS. For any systemd distro, you don't really want to edit the vendored unit files. The overrides files are a sufficient way to extend unit configuration if you want to, and otherwise declaring a unit in full affects the result on NixOS the same as it would on another distro. It's a matter of making the correct and conscious decision on how you would like to declare your unit.

Network units, on the other hand, are a total pain. You can't mix the vendor config with your own as they use wildcard match patterns. I had some fun working around that in the rest of the module in my other repo.

That's my exact point though, vendor/manufacturer units can be read-only in a dedicated directory tree (and even on a different read-only mountpoint). With NixOS, we're conflating units created by the vendor (the NixOS config) and the end-user at runtime into one place (/etc), which makes stc's job harder since it now has to differentiate which units it should care about managing, versus which ones it should probably ignore.

@m1cr0man

m1cr0man commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Ah yes, I get what you mean now.

BTW I ran switchTest locally (wow what a mammoth test that one is) and it unfortunately failed. I did find one bug already but it didn't fix the issue. Will continue researching but I'm also holding hope that a reviewer might spot something wrong. I just found a fix and pushed it 😀

@nixos-discourse

Copy link
Copy Markdown

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

https://discourse.nixos.org/t/prs-ready-for-review/3032/6739

@m1cr0man

Copy link
Copy Markdown
Contributor Author

Rebased on nixos-unstable.

@m1cr0man

Copy link
Copy Markdown
Contributor Author

@r-vdp I see you are working on refactoring STC-NG a bit. Would I be able to ask for your help getting this merged beforehand? I have been maintaining this PR for so long and I would really like to get it into master to stop having to refactor the STC-NG changes regularly.

@m1cr0man

Copy link
Copy Markdown
Contributor Author

Rebased on master and re-ran switchTest and activation-template-dropin test locally.

@m1cr0man

Copy link
Copy Markdown
Contributor Author

This has now been broken into 5 other PRs. Since none of the work is uniquely specific to RFC108 and has other applicability, plus the fact that this has been stuck for so long, hopefully we can get some stuff merged now.

@m1cr0man

m1cr0man commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

Big thanks to @r-vdp and @jmbaur for helping me get the switch-to-configuration-ng changes over the line and merged into master 😄

We're now one change away (#512577) from having RFC108 imperative container support in NixOS!

@nixpkgs-ci nixpkgs-ci Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for contributing to the documentation.

Follow the documentation style guide, especially:

  • Show, don’t tell. Start with a minimal working example. Explain after the code.
  • Avoid meta-commentary. Don’t write "This section explains how to…".
  • Use present tense, imperative mood, and active voice. For example: "Run the command," not "The user should run the following command."
  • Avoid vague references like "this" when the referent is unclear. Write "This command creates a folder," not "This will create a folder."
  • Be direct. Avoid "should", "might", "typically", and "usually".
  • Cut filler words like "simply", "just", "easily", and "basically". Use "to" instead of "in order to".

For larger changes, such as adding or removing whole sections, ask the NixOS documentation team for review.

Two major changes in this commit:

- Refactor options to use `.settings` pattern and use settingsToSections
  to generate the content. This brings the module in line with other
  modern systemd modules.
- Write individual units to etc/: This allows imperative container
  management to create unit files in /etc/systemd/nspawn
@m1cr0man

Copy link
Copy Markdown
Contributor Author

#512577 is merged! RFC108 imperative containers are now possible on master. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 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: 1 This PR was reviewed and approved by one person.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants