Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/container-modules/swarm-container.nix
maia c12e231d32 fix(matrix): the deferred starter logs the state its boot ended in
A degraded guest still starts the services, and now the journal says so next
to the start. The VM test pins the line.
2026-11-28 09:48:50 +01:00

127 lines
5.5 KiB
Nix

# What every hyperhive service nixos-container sets for itself, imported
# inside the container's own `config`.
#
# The host module that declares `containers.<name>` sets the options below.
# They restate host-side facts the container cannot read on its own: its
# evaluation is nested inside `containers.<name>`, and reading the host side
# back from in here recurses.
#
# ⚠️ The container is `Type=notify`: the host's start job lasts until this
# guest's job queue is empty and ends in a kill at the container's
# `timeoutStartSec`, one minute unless the host module sets more. A unit in that
# queue, wanted by `multi-user.target` or required by a unit that is, states a
# `TimeoutStartSec` below that limit; a fetch that needs longer fails fast,
# restarts with a growing delay, and starts what depends on it with
# `systemctl start --no-block` (./swarm-journal-forwarder.nix is the pattern).
# `module-eval-swarm-services-switch` fails on a unit whose budget, stated or the
# manager's 90 s default, reaches the limit, unless the unit is listed there with
# a reason. It also adds the budgets of units ordered one after another by
# `after`/`before`, and fails when the longest such chain reaches the limit (a
# unit listed there adds 0 s) or the ordering has a cycle.
#
# A daemon whose own start can outlast that limit (a database open that replays
# a large log) is named in `startAfterReady`: it leaves the boot transaction, so
# the container is up without it, and a unit that waits for the boot to end
# starts it `--no-block`.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.hyperhive.swarmContainer;
in
{
options.services.hyperhive.swarmContainer = {
privateNetwork = lib.mkOption {
type = lib.types.bool;
description = ''
Must equal the host-side `containers.<name>.privateNetwork`. When
`false` the container shares the host netns, so its own
firewall.service would rewrite the HOST ruleset at every boot; the
container's firewall is turned off and the host firewall owns all
filtering.
'';
};
startAfterReady = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "tuwunel" ];
description = ''
Names of this container's services (without `.service`) that start
after the container's boot instead of in it. Each stops being wanted
by `multi-user.target`, and `swarm-container-deferred-start.service`,
which is, starts them with `systemctl start --no-block` once the
guest's boot is over. The host's
start job for the container then ends when the guest's boot does,
whatever the service's own start takes. The service keeps its own
`TimeoutStartSec`, and whatever depends on it has to cope with a
service that is still starting.
'';
};
writesOwnResolvConf = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Something in this container writes `/etc/resolv.conf` itself (the
`swarm-container-resolver.nix` unit, or a static file), so
resolvconf is forced off. Left on, host-tracking would regenerate the
file empty, since the host's copy doesn't cross the boundary after
start.
'';
};
};
config = lib.mkMerge [
{
# A container that sets its own keeps it. Changing this value changes
# it for every container that doesn't, and that is a state migration
# for each of them.
system.stateVersion = lib.mkDefault "26.05";
}
(lib.mkIf (!cfg.privateNetwork) {
networking.firewall.enable = false;
})
# A private-netns container with an interface takes its address from the
# hive's DHCP. dhcpcd counts an IPv4LL address as an address, so without
# these `network-online.target` is reached on 169.254.x before the lease.
(lib.mkIf cfg.privateNetwork {
networking.dhcpcd.extraConfig = "noipv4ll";
networking.dhcpcd.wait = "ipv4";
})
(lib.mkIf cfg.writesOwnResolvConf {
networking.resolvconf.enable = lib.mkForce false;
})
(lib.mkIf (cfg.startAfterReady != [ ]) {
systemd.services =
lib.genAttrs cfg.startAfterReady (_: {
wantedBy = lib.mkForce [ ];
})
// {
# A `--no-block` start still queues the service's start job, and the
# container's start ends when the guest's job queue is empty
# (nixos-containers.nix passes `--notify-ready=yes`, and the host
# unit is `Type=notify`). So the job is queued after the boot, not
# in it: this unit is `Type=simple`, whose own start job ends at the
# fork, and it waits for the manager to leave `starting` before it
# queues the services.
swarm-container-deferred-start = {
description = "start the services that outlast the container's start limit, once its boot is over";
wantedBy = [ "multi-user.target" ];
path = [ pkgs.systemd ];
serviceConfig.Type = "simple";
script = ''
# `degraded` (a unit of the boot failed) exits 1 and is still over;
# the state goes to the journal next to the start.
state=$(systemctl is-system-running --wait || true)
echo "swarm-container-deferred-start: the boot ended $state; starting ${lib.concatStringsSep " " cfg.startAfterReady}"
systemctl start --no-block ${lib.concatMapStringsSep " " (n: "${n}.service") cfg.startAfterReady}
'';
};
};
})
];
}