{ lib, pkgs, containerLib }: rec { # Collect all files from all services in a group. # Returns: { "." = { target = "..."; content = "..."; }; } getAllFiles = grpCfg: lib.foldAttrs lib.recursiveUpdate { } (lib.mapAttrsToList (svcName: svcCfg: lib.mapAttrs' (target: content: let filename = containerLib.pathToFilename target; in lib.nameValuePair "${svcName}/${filename}" { inherit target content; } ) svcCfg.files ) grpCfg.services ); # Build file volume mounts for a specific service. # Returns: [ "/var/lib/container-services//files/::ro" ] mkFileVolumesForService = groupName: svcCfg: lib.mapAttrsToList (target: _: let filename = containerLib.pathToFilename target; hostPath = "/var/lib/container-services/${groupName}/files/${filename}"; in "${hostPath}:${target}:ro" ) svcCfg.files; # Build file volume mounts for all services in a group. # Returns: { "svcName" = [ "...mount..." ]; } mkFileVolumesForGroup = groupName: grpCfg: lib.mapAttrs (svcName: svcCfg: mkFileVolumesForService groupName svcCfg) grpCfg.services; # Generate a shell script that writes all files for a group. mkFilesScript = groupName: grpCfg: let filesMap = getAllFiles grpCfg; writeCommands = lib.mapAttrsToList (path: fileData: let filename = containerLib.pathToFilename fileData.target; hostPath = "/var/lib/container-services/${groupName}/files/${filename}"; in '' mkdir -p "$(dirname "${hostPath}")" cat > "${hostPath}" <<'CONTENT' ${fileData.content} CONTENT chmod 0644 "${hostPath}" '' ) filesMap; 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 = mkFilesScriptFile groupName grpCfg pkgs; in lib.optionalAttrs (filesMap != { }) { "container-services-${groupName}-files" = { description = "Write custom files for container service group '${groupName}'"; before = [ "container-services-${groupName}.service" ]; # The script uses bare mkdir/cat/chmod/dirname, so coreutils must be on PATH. path = [ pkgs.coreutils ]; serviceConfig = { Type = "oneshot"; ExecStart = scriptFile; }; }; }; }