Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "docker.nix: Add options for named volumes and networks" #67

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 0 additions & 162 deletions nixos/modules/virtualisation/docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,6 @@ let
proxy_env = config.networking.proxy.envVars;
settingsFormat = pkgs.formats.json {};
daemonSettingsFile = settingsFormat.generate "daemon.json" cfg.daemon.settings;

inherit (builtins) attrNames;

mkUncreateMaybe = networks: volumes: ''
set -euo pipefail

nexisting=$(${pkgs.coreutils}/bin/mktemp)
nwanted=$(${pkgs.coreutils}/bin/mktemp)
vexisting=$(${pkgs.coreutils}/bin/mktemp)
vwanted=$(${pkgs.coreutils}/bin/mktemp)

cleanup() {
rm -f "$nexisting" "$nwanted" "$vexisting" "$vwanted"
}
trap cleanup EXIT

${pkgs.docker}/bin/docker network ls --format '{{.Name}}' > "$nexisting"
echo -e "bridge\nhost\nnone\n${concatStringsSep "\n" networks}" > "$nwanted"

${pkgs.docker}/bin/docker volume ls --format '{{.Name}}' > "$vexisting"
echo -e "${concatStringsSep "\n" volumes}" > "$vwanted"

nsuperfluous="$(${pkgs.gnugrep}/bin/grep -vxF -f $nwanted $nexisting || true)"
vsuperfluous="$(${pkgs.gnugrep}/bin/grep -vxF -f $vwanted $vexisting || true)"

while read -r net; do
if [[ ! -z "$net" ]]; then
echo -n "Removed superfluous Docker network: "
${pkgs.docker}/bin/docker network rm "$net" || true
fi
done <<< "$nsuperfluous"

while read -r vol; do
if [[ ! -z "$vol" ]]; then
echo -n "Removed superfluous Docker volume: "
${pkgs.docker}/bin/docker volume rm "$vol" || true
fi
done <<< "$vsuperfluous"
'';

mkNetworkOpts = opts: concatStringsSep " "
([ "--driver=${opts.driver}" ]
++ optional (cfg ? subnet && cfg.subnet != null) "--subnet=${opts.subnet}"
++ optional (cfg ? ip-range && cfg.ip-range != null) "--ip-range=${opts.ip-range}"
++ optional (cfg ? gateway && cfg.gateway != null) "--gateway=${opts.gateway}"
++ optional (cfg ? ipv6 && cfg.ipv6) "--ipv6"
++ optional (cfg ? internal && cfg.internal) "--internal");


mkNetwork = name: opts: ''
if [[ $(${pkgs.docker}/bin/docker network ls --quiet --filter name=${name} | wc -c) -eq 0 ]]; then
echo "*** docker network create ${mkNetworkOpts opts} ${name}"
${pkgs.docker}/bin/docker network create ${mkNetworkOpts opts} ${name}
fi
'';

mkVolume = name: ''
if [[ $(${pkgs.docker}/bin/docker volume ls --quiet --filter name=${name} | wc -c) -eq 0 ]]; then
echo "*** docker volume create ${name}"
${pkgs.docker}/bin/docker volume create ${name}
fi
'';
in

{
Expand Down Expand Up @@ -170,16 +108,6 @@ in
'';
};

logLevel =
mkOption {
type = types.enum ["debug" "info" "warn" "error" "fatal"];
default = "info";
description =
''
This option determines the log level for the Docker daemon.
'';
};

extraOptions =
mkOption {
type = types.separatedString " ";
Expand Down Expand Up @@ -232,89 +160,6 @@ in
Extra packages to add to PATH for the docker daemon process.
'';
};

volumes = mkOption {
default = [];
type = types.listOf types.str;
example = [ "volume_1" "volume_2" ];
description = ''
A list of named volumes that should be created.
'';
};

networks = mkOption {
default = {};
type = types.attrsOf (types.submodule {
options = {
driver = mkOption {
default = "bridge";
type = types.str;
example = "overlay";
description = ''
Driver to manage the network. One of bridge, or overlay.
'';
};

subnet = mkOption {
default = null;
type = types.nullOr types.str;
example = "172.28.0.0/16";
description = ''
Subnet in CIDR format that represents a network segment.
'';
};

ip-range = mkOption {
default = null;
type = types.nullOr types.str;
example = "172.28.5.0/24";
description = ''
Allocate container ip from a sub-range.
'';
};

gateway = mkOption {
default = null;
type = types.nullOr types.str;
example = "172.28.5.254";
description = ''
IPv4 or IPv6 Gateway for the master subnet.
'';
};

ipv6 = mkOption {
default = false;
type = types.bool;
example = true;
description = ''
Enable IPv6 networking.
'';
};

internal = mkOption {
default = false;
type = types.bool;
example = true;
description = ''
Restrict external access to the network.
'';
};
};
});

example = {
my-network = {
driver = "bridge";
subnet = "172.28.0.0/16";
ip-range = "172.28.5.0/24";
gateway = "172.28.5.254";
};
};

description = ''
A list of named networks to be created.
'';
};
};

###### implementation
Expand All @@ -335,22 +180,15 @@ in
after = [ "network.target" "docker.socket" ];
requires = [ "docker.socket" ];
environment = proxy_env;

postStart = mkUncreateMaybe (attrNames cfg.networks) cfg.volumes
+ concatStrings (mapAttrsToList mkNetwork cfg.networks)
+ concatStrings (map mkVolume cfg.volumes);

serviceConfig = {
Type = "notify";
ExecStart = [
""
''
${cfg.package}/bin/dockerd \
--config-file=${daemonSettingsFile} \
--log-level=${cfg.logLevel} \
${cfg.extraOptions}
''];

ExecReload=[
""
"${pkgs.procps}/bin/kill -s HUP $MAINPID"
Expand Down
25 changes: 3 additions & 22 deletions nixos/tests/docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ import ./make-test-python.nix ({ pkgs, ...} : {
docker =
{ pkgs, ... }:
{
virtualisation.docker = {
enable = true;
package = pkgs.docker;
volumes = [ "thevolume" ];
networks.thenetwork = {
driver = "bridge";
subnet = "172.28.0.0/16";
ip-range = "172.28.5.0/24";
gateway = "172.28.5.254";
};

logLevel = "warn";
};
virtualisation.docker.enable = true;
virtualisation.docker.autoPrune.enable = true;
virtualisation.docker.package = pkgs.docker;

users.users = {
noprivs = {
Expand Down Expand Up @@ -54,15 +44,6 @@ import ./make-test-python.nix ({ pkgs, ...} : {
docker.fail("sudo -u noprivs docker ps")
docker.succeed("docker stop sleeping")

$docker->succeed("docker volume ls | grep thevolume");
$docker->succeed("docker network ls | grep thenetwork");

$docker->succeed("docker volume create superfluousvolume");
$docker->succeed("docker network create superfluousnetwork");
$docker->systemctl("restart docker");
$docker->waitForUnit("docker.service");
$docker->fail("docker volume ls | grep superfluous");

# Must match version 4 times to ensure client and server git commits and versions are correct
docker.succeed('[ $(docker version | grep ${pkgs.docker.version} | wc -l) = "4" ]')
docker.succeed("systemctl restart systemd-sysctl")
Expand Down