1

Modules/Docker: Fix invalid systemd services generation for docker networks

This commit is contained in:
2025-07-10 02:11:26 +02:00
parent e4ab000f30
commit 5d61740724
5 changed files with 46 additions and 35 deletions

View File

@ -1,6 +1,3 @@
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running nixos-help).
{
inputs,
hostname,

View File

@ -70,11 +70,11 @@ in {
then "${config.virtualisation.podman.package}/bin/podman"
else "${config.virtualisation.docker.package}/bin/docker";
mkDockerNetwork = name: options:
mkDockerNetwork = options:
builtins.concatStringsSep "\n" [
# Make sure to return true on fail to not crash
''
check=$(${cli} network inspect ${name} || true)
check=$(${cli} network inspect ${options.name} || true)
if [ -z "$check" ]; then
''
@ -82,55 +82,57 @@ in {
"${cli} network create"
# Disable masquerading
(lib.mkIf
(lib.optionalString
options.disable_masquerade
''-o "com.docker.network.bridge.enable_ip_masquerade"="false"'')
# Enable ipv6
(lib.mkIf
(lib.optionalString
options.ipv6.enable
"--ipv6")
(lib.mkIf
(builtins.hasAttr "gateway" options.ipv6)
(lib.optionalString
(!(builtins.isNull options.ipv6.gateway))
''--gateway="${options.ipv6.gateway}"'')
(lib.mkIf
(builtins.hasAttrs "subnet" options.ipv6)
(lib.optionalString
(!(builtins.isNull options.ipv6.subnet))
''--subnet="${options.ipv6.subnet}"'')
"${name}"
"${options.name}"
])
''
else
echo "${name} already exists!"
echo "Network ${options.name} already exists!"
fi
''
];
mkPodmanNetwork = name: options:
mkPodmanNetwork = options:
builtins.concatStringsSep "\n" [
''
ehco "Can't create Podman networks (yet)!"
''
];
mkSystemdNetworkService = name: options: let
mkSystemdNetworkService = options: let
toolName =
if docker.podman
then "Podman"
else "Docker";
then "podman"
else "docker";
in {
description = "Creates the ${toolName} network \"${name}\"";
after = ["network.target"];
wantedBy = ["multi-user.target"];
"${toolName}-create-${options.name}-network" = {
description = "Creates the ${toolName} network \"${options.name}\"";
after = ["network.target"];
wantedBy = ["multi-user.target"];
serviceConfig.Type = "oneshot";
script =
if docker.podman
then (mkPodmanNetwork name options)
else (mkDockerNetwork name options);
serviceConfig.Type = "oneshot";
script =
if docker.podman
then (mkPodmanNetwork options)
else (mkDockerNetwork options);
};
};
in
lib.mkMerge (builtins.mapAttrs mkSystemdNetworkService docker.networks);
lib.mkMerge (builtins.map mkSystemdNetworkService docker.networks);
};
}

View File

@ -9,12 +9,18 @@
docker.rootless = lib.mkEnableOption "Use rootless docker (no effect if podman is used)";
networks = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule ({
type = lib.types.listOf (lib.types.submodule ({
lib,
mylib,
...
}: {
options = {
name = lib.mkOption {
type = lib.types.str;
description = "The name of the docker/podman network";
example = "behind-nginx";
};
disable_masquerade = lib.mkEnableOption "Disable IP masquerading for this network";
ipv6 = {

View File

@ -36,10 +36,13 @@
];
modules = {
docker.networks."behind-nginx" = {
disable_masquerade = false;
ipv6.enable = false;
};
docker.networks = [
{
name = "behind-nginx";
disable_masquerade = false;
ipv6.enable = false;
}
];
network = {
useNetworkManager = false;

View File

@ -25,10 +25,13 @@
];
modules = {
docker.networks."behind-nginx" = {
disable_masquerade = false;
ipv6.enable = false;
};
docker.networks = [
{
name = "behind-nginx";
disable_masquerade = false;
ipv6.enable = false;
}
];
network = {
useNetworkManager = false;