diff --git a/hosts/hl-minipc1/services/public-gitea.nix b/hosts/hl-minipc1/services/public-gitea.nix index 87e8cd2..d69d38e 100644 --- a/hosts/hl-minipc1/services/public-gitea.nix +++ b/hosts/hl-minipc1/services/public-gitea.nix @@ -30,7 +30,6 @@ in }; volumes = [ "public-gitea-data:/data" - # Local project logos, served by Gitea at /assets/skyg/. "${./public-gitea-assets}:/data/custom/public/assets/skyg:ro" ]; files."/data/custom/templates/home.tmpl" = diff --git a/modules/nixos/common/containers/container-services/ABOUTME.md b/modules/nixos/common/containers/container-services/ABOUTME.md index 1a117c5..6bab152 100644 --- a/modules/nixos/common/containers/container-services/ABOUTME.md +++ b/modules/nixos/common/containers/container-services/ABOUTME.md @@ -4,14 +4,14 @@ Custom compose-style container service groups for systemd. ## Files -| File | Purpose | -| ------------- | ----------------------------------------------------------------------------------- | -| `default.nix` | Main module entry point; imports options and wires everything | -| `options.nix` | All `skyg.nixos.common.container-services.*` option declarations | -| `lib.nix` | Shared helpers (runtime dispatch, path sanitization, env file collection) | -| `compose.nix` | YAML composition (mkComposeService, mkComposeAttrs, mkComposeFile, mkOverridesFile) | -| `files.nix` | File mounting logic (getAllFiles, mkFileVolumesForService, mkFilesService) | -| `systemd.nix` | Systemd unit builders (mkSystemdService, mkPathUnit, mkEnvReloadService) | +| File | Purpose | +| ------------- | --------------------------------------------------------------------------------------------- | +| `default.nix` | Main module entry point; imports options and wires everything | +| `options.nix` | All `skyg.nixos.common.container-services.*` option declarations | +| `lib.nix` | Shared helpers (runtime dispatch, path sanitization, env file collection) | +| `compose.nix` | YAML composition (mkComposeService, mkComposeAttrs, mkComposeFile, mkOverridesFile) | +| `files.nix` | File mounting logic (getAllFiles, mkFileVolumesForService, mkFilesScriptFile, mkFilesService) | +| `systemd.nix` | Systemd unit builders (mkSystemdService, mkPathUnit, mkEnvReloadService) | ## How it works @@ -49,5 +49,8 @@ If adding a new per-service option: `/run/current-system/sw/bin` (Docker worked without this only because `docker-compose` v2 talks to the socket directly) - Custom files are collected and written by a separate unit (`container-services--files.service`) +- The group's unit sets `restartTriggers` to the files-write script, so editing any + `files` entry restarts the stack on the next switch (services that read files at + startup, like Gitea templates, otherwise keep serving the old content) - Env file changes trigger a path unit that restarts the stack (`container-services--env-reload.*`) - Compose files are rendered to the store and staged to `/var/lib/container-services//compose.yml` diff --git a/modules/nixos/common/containers/container-services/default.nix b/modules/nixos/common/containers/container-services/default.nix index dd91db1..b808f32 100644 --- a/modules/nixos/common/containers/container-services/default.nix +++ b/modules/nixos/common/containers/container-services/default.nix @@ -81,10 +81,14 @@ in else composeLib.mkComposeFile pkgs groupName grpCfg fileVolumes; overridesFile = mkOverridesFileFor groupName grpCfg; hasFiles = grpCfg.composeFile == null && filesLib.getAllFiles grpCfg != { }; + # Restart the stack when its files change, so services that read + # them at startup (e.g. Gitea templates) pick up the new content. + restartTriggers = + lib.optional hasFiles (filesLib.mkFilesScriptFile groupName grpCfg pkgs); in lib.nameValuePair "container-services-${groupName}" - (systemdLib.mkSystemdService groupName grpCfg effectiveComposeFile overridesFile composeBin runtimeService runtimePkg hasFiles)) + (systemdLib.mkSystemdService groupName grpCfg effectiveComposeFile overridesFile composeBin runtimeService runtimePkg hasFiles restartTriggers)) enabledGroups // lib.foldAttrs lib.recursiveUpdate { } ( lib.mapAttrsToList diff --git a/modules/nixos/common/containers/container-services/files.nix b/modules/nixos/common/containers/container-services/files.nix index fe11410..eb6d325 100644 --- a/modules/nixos/common/containers/container-services/files.nix +++ b/modules/nixos/common/containers/container-services/files.nix @@ -63,12 +63,18 @@ rec { in lib.concatStringsSep "\n" writeCommands; + # The script derivation that writes a group's files. Its store path changes + # whenever any file content changes, so it doubles as a systemd restart + # trigger for the group's unit. + mkFilesScriptFile = groupName: grpCfg: pkgs: + pkgs.writeShellScript "write-files-${groupName}" (mkFilesScript groupName grpCfg); + # Create a systemd service that writes files for a group. # Only created if the group has files. mkFilesService = groupName: grpCfg: pkgs: let filesMap = getAllFiles grpCfg; - scriptFile = pkgs.writeShellScript "write-files-${groupName}" (mkFilesScript groupName grpCfg); + scriptFile = mkFilesScriptFile groupName grpCfg pkgs; in lib.optionalAttrs (filesMap != { }) { "container-services-${groupName}-files" = { diff --git a/modules/nixos/common/containers/container-services/systemd.nix b/modules/nixos/common/containers/container-services/systemd.nix index b7aa0d3..456d52c 100644 --- a/modules/nixos/common/containers/container-services/systemd.nix +++ b/modules/nixos/common/containers/container-services/systemd.nix @@ -3,7 +3,7 @@ { # Create a systemd oneshot service that manages the compose stack. # `runtimePkg` is put on PATH because podman-compose shells out to `podman`. - mkSystemdService = groupName: grpCfg: composeFile: overridesFile: composeBin: runtimeService: runtimePkg: hasFiles: + mkSystemdService = groupName: grpCfg: composeFile: overridesFile: composeBin: runtimeService: runtimePkg: hasFiles: restartTriggers: let stateDir = grpCfg.stateDir; fileServiceDep = if hasFiles then "container-services-${groupName}-files.service" else null; @@ -26,6 +26,9 @@ ++ lib.optional hasFiles fileServiceDep; requires = [ runtimeService ] ++ lib.optional hasFiles fileServiceDep; + # Restart the stack when its mounted files change (their content is baked + # into the files-write script, so its store path changes on any edit). + restartTriggers = restartTriggers; path = [ runtimePkg pkgs.coreutils ]; startLimitIntervalSec = 300; # 5 minutes window startLimitBurst = 6; # allow 6 failures before giving up diff --git a/modules/nixos/common/containers/user-guide.md b/modules/nixos/common/containers/user-guide.md index f1a719b..22650a5 100644 --- a/modules/nixos/common/containers/user-guide.md +++ b/modules/nixos/common/containers/user-guide.md @@ -128,7 +128,8 @@ Files are: - Written to `/var/lib/container-services//files/` on the host - Mounted read-only into the container at the specified paths - Updated automatically when you rebuild -- Changes trigger an automatic restart (via the `container-services--files` unit) +- Changes trigger an automatic restart of the stack on the next `nixos-rebuild switch` + (the group's unit uses the files-write script as a `restartTriggers` entry) ---