Files
2026-09-16 22:02:04 -04:00

99 lines
3.1 KiB
Nix

{ lib }:
rec {
# Build one compose service entry from a service config.
# Includes both user volumes and file-mounted volumes.
mkComposeService = groupName: _svcName: svcCfg: fileVolumes:
{
image = svcCfg.image;
restart = svcCfg.restart;
}
// lib.optionalAttrs (svcCfg.network_mode == null) {
networks =
if svcCfg.networks == [ ]
then [ groupName ]
else svcCfg.networks;
}
// lib.optionalAttrs (svcCfg.command != [ ]) {
command = svcCfg.command;
}
// lib.optionalAttrs ((svcCfg.ports) != [ ]) {
ports = svcCfg.ports;
}
// lib.optionalAttrs ((svcCfg.volumes ++ fileVolumes) != [ ]) {
volumes = svcCfg.volumes ++ fileVolumes;
}
// lib.optionalAttrs (svcCfg.environment != { }) {
environment = svcCfg.environment;
}
// lib.optionalAttrs (svcCfg.environmentFiles != [ ]) {
env_file = map builtins.toString svcCfg.environmentFiles;
}
// 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;
}
// lib.optionalAttrs (svcCfg.extra_hosts != [ ]) {
extra_hosts = svcCfg.extra_hosts;
}
// lib.optionalAttrs (svcCfg.shm_size != null) {
shm_size = svcCfg.shm_size;
}
// lib.optionalAttrs (svcCfg.network_mode != null) {
network_mode = svcCfg.network_mode;
}
// svcCfg.extraConfig;
# Build the full compose document attrset for a group.
mkComposeAttrs = groupName: grpCfg: fileVolumesByService:
let
networks =
if grpCfg.networks == { }
then { "${groupName}" = { driver = "bridge"; }; }
else grpCfg.networks;
in
{
services = lib.mapAttrs
(svcName: svcCfg:
mkComposeService groupName svcName svcCfg (fileVolumesByService."${svcName}" or [ ])
)
grpCfg.services;
networks = networks;
}
// lib.optionalAttrs (grpCfg.volumes != { }) {
volumes = grpCfg.volumes;
}
// grpCfg.extraConfig;
# Render compose.yml to the Nix store.
mkComposeFile = pkgs: groupName: grpCfg: fileVolumesByService:
(pkgs.formats.yaml { }).generate "compose.yml"
(mkComposeAttrs groupName grpCfg fileVolumesByService);
# Build the overrides attrset (networks/volumes/extraConfig only) used
# alongside an external composeFile. Passed as an earlier -f file so the
# composeFile's own definitions win on any overlapping keys.
mkOverridesAttrs = grpCfg:
lib.optionalAttrs (grpCfg.networks != { })
{
networks = grpCfg.networks;
}
// lib.optionalAttrs (grpCfg.volumes != { }) {
volumes = grpCfg.volumes;
}
// grpCfg.extraConfig;
# Render the overrides file to the Nix store.
mkOverridesFile = pkgs: groupName: grpCfg:
(pkgs.formats.yaml { }).generate "compose-overrides-${groupName}.yml"
(mkOverridesAttrs grpCfg);
}