From 896d7480c13d838d7ee91044fbe7ab45b589d90a Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 9 Jul 2026 01:02:10 -0400 Subject: [PATCH] autoupdates --- hosts/hl-bigbox1.nix | 66 ++++++++++++++++ hosts/hl-minipc2.nix | 13 +--- .../containers/container-services/compose.nix | 9 +++ .../containers/container-services/default.nix | 17 ++++- .../containers/container-services/lib.nix | 6 ++ .../containers/container-services/options.nix | 76 ++++++++++++++++++- .../containers/container-services/systemd.nix | 35 +++++++++ 7 files changed, 210 insertions(+), 12 deletions(-) diff --git a/hosts/hl-bigbox1.nix b/hosts/hl-bigbox1.nix index c6ad06b..b0e10b7 100644 --- a/hosts/hl-bigbox1.nix +++ b/hosts/hl-bigbox1.nix @@ -42,6 +42,7 @@ # Dockge container service group skyg.nixos.common.container-services.dockge = { + enable = false; services.dockge = { image = "louislam/dockge:1"; ports = [ "5001:5001" ]; @@ -71,6 +72,71 @@ }; }; }; + skyg.nixos.common.container-services.jellyfin = { + enable = true; + autoUpdate.enable = true; + services = { + jellyfin = { + image = "jellyfin/jellyfin"; + deploy = { + resources = { + reservations = { + devices = [ + { + driver = "cdi"; + device_ids = [ "nvidia.com/gpu=all" ]; + capabilities = [ "gpu" ]; + } + ]; + }; + }; + }; + volumes = [ + "/opt/jellyfin:/mnt/apps/jellyfin" + "torrents:/torrents" + "family-videos:/family-videos" + ]; + devices = [ "/dev/dri:/dev/dri" ]; + restart = "always"; + healthcheck = { + test = "curl --fail http://0.0.0.0:8096 || exit 1"; + interval = "60s"; + timeout = "20s"; + start_period = "30s"; + }; + environment = { + JELLYFIN_DATA_DIR = "/mnt/apps/jellyfin/data"; + JELLYFIN_CACHE_DIR = "/mnt/apps/jellyfin/cache"; + JELLYFIN_CONFIG_DIR = "/mnt/apps/jellyfin/config"; + NVIDIA_VISIBLE_DEVICES = "all"; + }; + ports = [ "8096:8096" ]; + }; + }; + volumes = { + torrents = { + driver_opts = { + "type" = "nfs"; + "o" = "addr=tnas1.lab.internal,nfsvers=4"; + "device" = ":/mnt/EightTerra/DownloadedTorrents"; + }; + }; + family-videos = { + driver_opts = { + "type" = "nfs"; + "o" = "addr=tnas1.lab.internal,nfsvers=4"; + "device" = ":/mnt/EightTerra/FamilyStorage/Video"; + }; + }; + jellyfin = { + driver_opts = { + "type" = "nfs"; + "o" = "addr=tnas1.lab.internal,nfsvers=4"; + "device" = ":/mnt/Data/apps/jellyfin"; + }; + }; + }; + }; services.displayManager.defaultSession = "gnome"; services.displayManager.gdm.enable = false; diff --git a/hosts/hl-minipc2.nix b/hosts/hl-minipc2.nix index cbc0625..2b22162 100644 --- a/hosts/hl-minipc2.nix +++ b/hosts/hl-minipc2.nix @@ -23,6 +23,7 @@ # Dockge container service group skyg.nixos.common.container-services.dockge = { + enable = true; services.dockge = { image = "louislam/dockge:1"; ports = [ "5001:5001" ]; @@ -55,6 +56,8 @@ # Scrypted container service group skyg.nixos.common.container-services.scrypted = { + enable = true; + autoUpdate.enable = true; services.scrypted = { image = "ghcr.io/koush/scrypted"; volumes = [ @@ -81,7 +84,7 @@ }; }; - # Scrypted backup & autoupdate timers + # Scrypted backup timer (image auto-update handled by container-services autoUpdate) skyg.server.timers = { scrypted-backups = { OnCalendar = "daily"; @@ -91,13 +94,5 @@ ${pkgs.rsync}/bin/rsync -avpzP --delete /opt/homelab/scrypted /homelab/terra1/Data/apps/ ''; }; - scrypted-autoupdate = { - OnCalendar = "weekly"; - script = '' - set -eu - ${pkgs.docker}/bin/docker pull ghcr.io/koush/scrypted - systemctl restart container-services-scrypted.service - ''; - }; }; } diff --git a/modules/nixos/common/containers/container-services/compose.nix b/modules/nixos/common/containers/container-services/compose.nix index 74cb395..2ce78d2 100644 --- a/modules/nixos/common/containers/container-services/compose.nix +++ b/modules/nixos/common/containers/container-services/compose.nix @@ -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. diff --git a/modules/nixos/common/containers/container-services/default.nix b/modules/nixos/common/containers/container-services/default.nix index 6d873af..d0957a9 100644 --- a/modules/nixos/common/containers/container-services/default.nix +++ b/modules/nixos/common/containers/container-services/default.nix @@ -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; }; } diff --git a/modules/nixos/common/containers/container-services/lib.nix b/modules/nixos/common/containers/container-services/lib.nix index 87cb84f..e65d7de 100644 --- a/modules/nixos/common/containers/container-services/lib.nix +++ b/modules/nixos/common/containers/container-services/lib.nix @@ -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 diff --git a/modules/nixos/common/containers/container-services/options.nix b/modules/nixos/common/containers/container-services/options.nix index 9630eb1..846aa95 100644 --- a/modules/nixos/common/containers/container-services/options.nix +++ b/modules/nixos/common/containers/container-services/options.nix @@ -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 = { }; diff --git a/modules/nixos/common/containers/container-services/systemd.nix b/modules/nixos/common/containers/container-services/systemd.nix index 9356dd0..f728bc5 100644 --- a/modules/nixos/common/containers/container-services/systemd.nix +++ b/modules/nixos/common/containers/container-services/systemd.nix @@ -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 != [ ]) {