autoupdates

This commit is contained in:
ari committed 2026-07-09 01:02:10 -04:00
1 parent 6eb90819f0
commit 896d7480c1
7 files changed
+210 -12

No files matched your search

@@ -30,6 +30,15 @@ rec {
// lib.optionalAttrs (svcCfg.dependsOn != [ ]) {
depends_on = svcCfg.dependsOn;
}
// lib.optionalAttrs (svcCfg.devices != [ ]) {
devices = svcCfg.devices;
}
// lib.optionalAttrs (svcCfg.healthcheck != { }) {
healthcheck = svcCfg.healthcheck;
}
// lib.optionalAttrs (svcCfg.deploy != { }) {
deploy = svcCfg.deploy;
}
// svcCfg.extraConfig;
# Build the full compose document attrset for a group.
@@ -12,8 +12,10 @@ let
isDocker = containerLib.isDocker containersCfg.runtime;
composeBin = containerLib.composeBin containersCfg.runtime;
runtimeService = containerLib.runtimeService isDocker;
runtimeBin = containerLib.runtimeBin isDocker;
enabledGroups = lib.filterAttrs (_: g: g.enable) cfg;
autoUpdateGroups = lib.filterAttrs (_: g: g.autoUpdate.enable) enabledGroups;
optionsModule = import ./options.nix { inherit lib; };
in
@@ -74,7 +76,12 @@ in
(lib.filterAttrs
(_: grpCfg: filesLib.getAllFiles grpCfg != { })
enabledGroups)
);
)
// lib.mapAttrs'
(groupName: grpCfg:
let units = systemdLib.mkUpdateUnits groupName grpCfg composeBin runtimeBin;
in lib.nameValuePair "container-services-${groupName}-update" units.services."container-services-${groupName}-update")
autoUpdateGroups;
# Path units: one per group with env files
systemd.paths =
@@ -84,5 +91,13 @@ in
systemdLib.mkPathUnit groupName (containerLib.getAllEnvFiles grpCfg))
enabledGroups
);
# Timers: one per group with autoUpdate enabled
systemd.timers =
lib.mapAttrs'
(groupName: grpCfg:
let units = systemdLib.mkUpdateUnits groupName grpCfg composeBin runtimeBin;
in lib.nameValuePair "container-services-${groupName}-update" units.timers."container-services-${groupName}-update")
autoUpdateGroups;
};
}
@@ -18,6 +18,12 @@
runtimeService = isDocker:
if isDocker then "docker.service" else "podman.service";
# Runtime CLI binary (for operations without a compose equivalent, e.g. prune)
runtimeBin = isDocker:
if lib.hasAttrByPath [ "docker" ] pkgs
then (if isDocker then "${pkgs.docker}/bin/docker" else "${pkgs.podman}/bin/podman")
else (if isDocker then "docker" else "podman");
# Collect all env files from all services in a group
getAllEnvFiles = grpCfg:
lib.concatLists
@@ -16,7 +16,7 @@
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
options = {
enable = lib.mkEnableOption "this container service group" // { default = true; };
enable = lib.mkEnableOption "this container service group" // { default = false; };
stateDir = lib.mkOption {
type = lib.types.str;
@@ -84,6 +84,36 @@
description = "Sibling service names that must start first (compose depends_on:).";
};
devices = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "/dev/dri:/dev/dri" ];
description = "Host device mappings in HOST:CONTAINER format (compose devices:).";
};
healthcheck = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
example = {
test = "curl --fail http://0.0.0.0:8096 || exit 1";
interval = "60s";
timeout = "20s";
start_period = "30s";
};
description = "Container healthcheck definition (compose healthcheck:).";
};
deploy = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
example = {
resources.reservations.devices = [
{ driver = "cdi"; device_ids = [ "nvidia.com/gpu=all" ]; }
];
};
description = "Deploy block (compose deploy:), e.g. GPU device reservations.";
};
restart = lib.mkOption {
type = lib.types.str;
default = "unless-stopped";
@@ -117,7 +147,7 @@
description = ''
Free-form attrset merged directly into the compose service
block. Use this for keys not modelled above:
shm_size, cap_add, devices, healthcheck, etc.
shm_size, cap_add, ulimits, etc.
'';
};
@@ -156,6 +186,48 @@
'';
};
autoUpdate = lib.mkOption {
description = ''
Scheduled image pull + recreate for this group. When enabled, a
systemd timer periodically runs 'compose pull' followed by
'compose up -d', so containers whose image changed are recreated.
'';
default = { };
type = lib.types.submodule {
options = {
enable = lib.mkEnableOption "scheduled image pull + recreate for this group";
onCalendar = lib.mkOption {
type = lib.types.str;
default = "weekly";
example = "Sun 04:00";
description = "systemd OnCalendar expression controlling update frequency.";
};
randomizedDelaySec = lib.mkOption {
type = lib.types.int;
default = 3600;
description = ''
Random delay (seconds) added to each trigger, to spread load
across groups/hosts (systemd RandomizedDelaySec).
'';
};
persistent = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Run a missed update on next boot (systemd Persistent).";
};
pruneImages = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Run 'image prune -f' after updating to reclaim dangling images.";
};
};
};
};
extraConfig = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
@@ -24,6 +24,41 @@
};
};
# Create the update service + timer for a group's autoUpdate config.
mkUpdateUnits = groupName: grpCfg: composeBin: runtimeBin:
let
auCfg = grpCfg.autoUpdate;
stateDir = grpCfg.stateDir;
unitName = "container-services-${groupName}-update";
composeCmd = "${composeBin} -p ${groupName} -f ${stateDir}/compose.yml";
in
{
services."${unitName}" = {
description = "Pull latest images and recreate container service group '${groupName}'";
after = [ "container-services-${groupName}.service" ];
requires = [ "container-services-${groupName}.service" ];
serviceConfig = {
Type = "oneshot";
ExecStart =
[
"${composeCmd} pull"
"${composeCmd} up -d --remove-orphans"
]
++ lib.optional auCfg.pruneImages "${runtimeBin} image prune -f";
};
};
timers."${unitName}" = {
description = "Scheduled image update for container service group '${groupName}'";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = auCfg.onCalendar;
RandomizedDelaySec = auCfg.randomizedDelaySec;
Persistent = auCfg.persistent;
Unit = "${unitName}.service";
};
};
};
# Create a path unit that watches env files for changes.
mkPathUnit = groupName: envFiles:
lib.optionalAttrs (envFiles != [ ]) {