Skip to content

nixos/nixos-containers: actually eliminate costs if no containers are used - #427001

Merged
emilazy merged 1 commit into
NixOS:masterfrom
zimward:nixos-container-weirdness
Aug 25, 2025
Merged

emilazy merged 1 commit into
NixOS:masterfrom
zimward:nixos-container-weirdness

Conversation

@zimward

@zimward zimward commented Jul 20, 2025

Copy link
Copy Markdown
Contributor

currently the man page reads like this:

boot.enableContainers
           Whether to enable support for NixOS containers. Defaults to true (at no cost if containers are
           not actually used).

but the nixos-container package still gets added to the packages if they are actually not used, adding at least 64MiB of space used when one tries to not have perl in the system closure.

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

Add a 👍 reaction to pull requests you find important.

@MatthewCroughan MatthewCroughan 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.

I like this, but maybe there's some hidden impact I'm not considering

@adisbladis @emilazy

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 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/` 6.topic: nixos-container Imperative and declarative systemd-nspawn containers labels Jul 20, 2025
@zimward
zimward force-pushed the nixos-container-weirdness branch from 09985d2 to 20865f4 Compare July 29, 2025 19:26
@adisbladis

adisbladis commented Jul 29, 2025 •

Copy link
Copy Markdown
Member

This would break the workflow of people using imperative NixOS containers as this module is pulling it into systemPackages.

I'm not against changing this default, but it would at least require:

  • Guarding it by stateVersion to not break existing setups
  • Adding release notes

And also be posted to https://discourse.nixos.org/t/breaking-changes-announcement-for-unstable/17574.

@emilazy

emilazy commented Jul 30, 2025

Copy link
Copy Markdown
Member

It's only the CLI tool that would be affected, right, since we only generate systemd services for declaratively-specified containers anyway? I don't think we should use stateVersion for mere presence of a CLI tool. Should definitely get a release note and a list in the thread after merge though. (And probably we could drop it from the perlless profile too?)

@adisbladis

Copy link
Copy Markdown
Member

This entire snippet feels relevant:

        networking.dhcpcd.denyInterfaces = [
          "ve-*"
          "vb-*"
        ];

        services.udev.extraRules = optionalString config.networking.networkmanager.enable ''
          # Don't manage interfaces created by nixos-container.
          ENV{INTERFACE}=="v[eb]-*", ENV{NM_UNMANAGED}="1"
        '';

        environment.systemPackages = [
          nixos-container
        ];

        boot.kernelModules = [
          "bridge"
          "macvlan"
          "tap"
          "tun"
        ];

I don't think we should use stateVersion for mere presence of a CLI tool.

Maybe not, but setting the default enable value based on stateVersion is a pretty cheap way to just keep working the same as before.
Anyway: We have more config here that just systemPackages.

And probably we could drop it from the perlless profile too?

Good catch!

@emilazy

emilazy commented Jul 30, 2025

Copy link
Copy Markdown
Member

This entire snippet feels relevant:

As far as I’m aware, you have to explicitly nixos-container start … to use an imperatively‐defined container. So you would have to be manually using the tool anyway, at which point you will notice that it’s not there and that it would perhaps be a good idea to check the release notes; it wouldn’t break your server or anything. (I suppose we could also replace it with a stub explaining what’s up. But we’ve shuffled around default packages without that much ceremony before, I think.)

I guess there are two other possibilities:

  1. You could systemd.services."container@foo".wantedBy = [ "multi-user.target" ]; while having the container declared imperatively. So we should probably assert on that.

  2. You could, I guess, manually write a declarative service that does nixos-container start … (which would itself create another, separate systemd service)…

I feel that avoiding proliferation of stateVersion’s issues is probably worth it given the low compatibility risk here, so long as we can prepare an appropriate assertion?

(The boot.kernelModules should be automatically loaded as required and systemd-machined certainly does not need them to be force‐loaded. It looks like it was added to work around the hardened profile setting security.lockKernelModules, which feels dubious to me… it’s a boot‐time pessimization for all users, to work around the profile that breaks stuff setting an option that breaks stuff. Seems like it would be better to support listing allowed modules explicitly rather than disabling auto‐loading and then forcing everything any module could want to be loaded. Or at least that setting should be conditional. But I digress.)

@adisbladis

Copy link
Copy Markdown
Member

I feel that avoiding proliferation of stateVersion’s issues is probably worth it given the low compatibility risk here, so long as we can prepare an appropriate assertion?

I personally don't care enough to argue about it, but I've seen NixOS deployments using declarative NixOS base systems with imperative containers deployed on top so I know just changing the default value would at least break a channel bump for a few users out there.

I feel that avoiding proliferation of stateVersion’s issues is probably worth it given the low compatibility risk here, so long as we can prepare an appropriate assertion?

I don't see how stateVersion is at all problematic in this instance.

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.

When the default evaluates to something using config, you need to include a defaultText.

also the description needs an update, since it no longer defaults to true and it's not zero cost if it's set to true anyway.

@emilazy

emilazy commented Jul 30, 2025

Copy link
Copy Markdown
Member

I personally don't care enough to argue about it, but I've seen NixOS deployments using declarative NixOS base systems with imperative containers deployed on top so I know just changing the default value would at least break a channel bump for a few users out there.

That would be covered by the assertion I mentioned, right? Unless I’m being insufficiently creative imagining how people are automatically starting imperatively‐defined containers from their declarative configurations :)

@adisbladis

Copy link
Copy Markdown
Member

That would be covered by the assertion I mentioned, right?

The workflow I'm describing is a script that does something like:

  • nixos-rebuild --target-host foobar
  • ssh foobar 'nixos-container start'

@zimward
zimward force-pushed the nixos-container-weirdness branch from 20865f4 to b926f95 Compare August 5, 2025 07:45
@nixpkgs-ci nixpkgs-ci 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. 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Aug 5, 2025
@zimward

zimward commented Aug 5, 2025 •

Copy link
Copy Markdown
Contributor Author

@emilazy i just added the assertion you mentioned, but i feel like its a bit heavy to eval for an assertion. (keeping it in a separate commit for now in case changes are requested)

One edgecase this has is if someone defines their own service to handle container@foo.

Comment thread doc/release-notes/rl-2511.section.md Resolved
@zimward
zimward force-pushed the nixos-container-weirdness branch 2 times, most recently from e726799 to 444c938 Compare August 24, 2025 18:19
Comment thread doc/release-notes/rl-2511.section.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Whoops, sorry, I typo’d the option name here. Let’s use the link form since that would have caught this:

Suggested change
- `boot.enableContainers` is only turned on when a NixOS container is defined in `boot.containers`.
- [](#opt-boot.enableContainers) is only turned on when a declarative NixOS container is defined in [](#opt-containers).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

seems like we can't link to those options as they not part of the release notes

@zimward
zimward force-pushed the nixos-container-weirdness branch 2 times, most recently from a938da3 to b2ec1c8 Compare August 24, 2025 20:07
@zimward
zimward force-pushed the nixos-container-weirdness branch from b2ec1c8 to 29be71c Compare August 24, 2025 20:16

@emilazy emilazy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Huh, I could swear we have other release notes linking to the options. But this is fine, thank you!

Could you make a post in the breaking changes announcement thread to let unstable users know they need to turn on boot.enableContainers if they use the nixos-container tool for imperative container management?

@emilazy
emilazy merged commit 7c4b56b into NixOS:master Aug 25, 2025
@emilazy

emilazy commented Aug 25, 2025

Copy link
Copy Markdown
Member

Ah, and right as I hit merge I realize the NixOS tests will need adjusting…

@emilazy

emilazy commented Aug 25, 2025

Copy link
Copy Markdown
Member

#436855

donovanglover added a commit to donovanglover/nix-config that referenced this pull request Sep 6, 2025
- Fixed dwm alpha patch for dwm 6.6
- Fixed breaking change with declarative containers used imperatively
  NixOS/nixpkgs#427001
- Fixed obsolete options
@zimward
zimward deleted the nixos-container-weirdness branch May 16, 2026 10:45
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: 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: 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants