refactor: abstract docker network creation via custom options

This commit is contained in:
Tibo De Peuter 2026-07-17 23:14:45 +02:00
parent 67c7cb6ec6
commit d4d655e735
Signed by: tdpeuter
SSH key fingerprint: SHA256:u/h/LVoqKF1Iz02uOyxe6hcjmoZASCGV2HM0TG9ZMoU
10 changed files with 49 additions and 99 deletions

View file

@ -1,5 +1,6 @@
{
imports = [
./docker.nix
./monitoring.nix
];

38
modules/common/docker.nix Normal file
View file

@ -0,0 +1,38 @@
{ config, lib, pkgs, ... }:
{
options.homelab.dockerNetworks = lib.mkOption {
description = "Declarative Docker networks to create before containers start.";
default = {};
type = lib.types.attrsOf (lib.types.submodule {
options = {
requiredBy = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "List of systemd services that require this network (e.g., docker-containerName.service).";
};
};
});
};
config = {
systemd.services = lib.mapAttrs' (networkName: cfg:
lib.nameValuePair "docker-${networkName}-create-network" {
description = "Create Docker network for ${networkName}";
requiredBy = cfg.requiredBy;
after = [ "network.target" "docker.service" ];
requires = [ "docker.service" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "create-${networkName}-docker-network" ''
if ! ${pkgs.docker}/bin/docker network ls | grep -q ${networkName}; then
${pkgs.docker}/bin/docker network create ${networkName}
fi
'';
};
}
) config.homelab.dockerNetworks;
};
}