implement basis docker network
This commit is contained in:
47
module.nix
47
module.nix
@@ -1,18 +1,47 @@
|
|||||||
{ config, lib, ... }:
|
{ config, lib, pkgs, ... }:
|
||||||
|
|
||||||
with lib;
|
with lib;
|
||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.foo.bar;
|
cfg = config.docker-networks;
|
||||||
|
|
||||||
in
|
dockerNetwork = { ... }: {
|
||||||
|
options = {
|
||||||
{
|
usedBy = mkOption {
|
||||||
options.foo.bar = {
|
type = with types; listOf str;
|
||||||
enable = mkEnableOption "foobar";
|
default = [];
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
mkService = name: network: let
|
||||||
# ...
|
mkBefore = map (x: "docker-${x}.service") network.usedBy;
|
||||||
|
in rec {
|
||||||
|
wantedBy = [ "multi-user.target" ];
|
||||||
|
after = [ "docker.service" "docker.socket" ];
|
||||||
|
before = mkBefore;
|
||||||
|
requires = after;
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
ExecStart = ''
|
||||||
|
${pkgs.docker}/bin/docker network create ${name}
|
||||||
|
'';
|
||||||
|
ExecStop = ''
|
||||||
|
${pkgs.docker}/bin/docker network rm ${name}
|
||||||
|
'';
|
||||||
|
RemainAfterExit="true";
|
||||||
|
Type="oneshot";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options.docker-networks = mkOption {
|
||||||
|
default = {};
|
||||||
|
type = types.attrsOf (types.submodule dockerNetwork);
|
||||||
|
description = "docker networks";
|
||||||
|
};
|
||||||
|
|
||||||
|
config = mkIf (cfg != {}) {
|
||||||
|
systemd.services = mapAttrs' (n: v: nameValuePair "docker-network-${n}" (mkService n v)) cfg;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user