making sure files will cause a restart
This commit is contained in:
1 parent
c4056e45cb
commit
17b14b33e5
6 files changed
+29
-13
No files matched your search
@@ -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" =
|
||||
|
||||
@@ -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-<group>-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-<group>-env-reload.*`)
|
||||
- Compose files are rendered to the store and staged to `/var/lib/container-services/<group>/compose.yml`
|
||||
@@ -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
|
||||
|
||||
@@ -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" = {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -128,7 +128,8 @@ Files are:
|
||||
- Written to `/var/lib/container-services/<group>/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-<group>-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)
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in new issue
Block a user