refactor: abstract docker network creation via custom options
This commit is contained in:
parent
67c7cb6ec6
commit
d4d655e735
10 changed files with 49 additions and 99 deletions
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
imports = [
|
||||
./docker.nix
|
||||
./monitoring.nix
|
||||
];
|
||||
|
||||
|
|
|
|||
38
modules/common/docker.nix
Normal file
38
modules/common/docker.nix
Normal 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;
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue