2014-07-28 00:00:59 +02:00
|
|
|
# Systemd services for docker.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualisation.docker;
|
2014-12-01 17:20:18 +01:00
|
|
|
pro = config.networking.proxy.default;
|
|
|
|
proxy_env = optionalAttrs (pro != null) { Environment = "\"http_proxy=${pro}\""; };
|
2014-07-28 00:00:59 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.virtualisation.docker = {
|
|
|
|
enable =
|
|
|
|
mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
This option enables docker, a daemon that manages
|
|
|
|
linux containers. Users in the "docker" group can interact with
|
|
|
|
the daemon (e.g. to start or stop containers) using the
|
|
|
|
<command>docker</command> command line tool.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
socketActivation =
|
|
|
|
mkOption {
|
|
|
|
type = types.bool;
|
2015-11-20 23:01:33 +01:00
|
|
|
default = true;
|
2014-07-28 00:00:59 +02:00
|
|
|
description =
|
|
|
|
''
|
|
|
|
This option enables docker with socket activation. I.e. docker will
|
|
|
|
start when first called by client.
|
|
|
|
'';
|
|
|
|
};
|
2015-09-04 01:18:19 +02:00
|
|
|
storageDriver =
|
|
|
|
mkOption {
|
2016-09-10 12:55:46 +02:00
|
|
|
type = types.nullOr (types.enum ["aufs" "btrfs" "devicemapper" "overlay" "overlay2" "zfs"]);
|
|
|
|
default = null;
|
2015-09-04 01:18:19 +02:00
|
|
|
description =
|
|
|
|
''
|
2016-09-10 12:55:46 +02:00
|
|
|
This option determines which Docker storage driver to use. By default
|
|
|
|
it let's docker automatically choose preferred storage driver.
|
2015-09-04 01:18:19 +02:00
|
|
|
'';
|
|
|
|
};
|
2016-09-10 12:55:46 +02:00
|
|
|
|
|
|
|
logDriver =
|
|
|
|
mkOption {
|
|
|
|
type = types.enum ["none" "json-file" "syslog" "journald" "gelf" "fluentd" "awslogs" "splunk" "etwlogs" "gcplogs"];
|
|
|
|
default = "journald";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
This option determines which Docker log driver to use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-07-28 00:00:59 +02:00
|
|
|
extraOptions =
|
|
|
|
mkOption {
|
2015-04-25 15:25:15 +02:00
|
|
|
type = types.separatedString " ";
|
2014-07-28 00:00:59 +02:00
|
|
|
default = "";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The extra command-line options to pass to
|
|
|
|
<command>docker</command> daemon.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-07-20 15:28:49 +02:00
|
|
|
postStart =
|
|
|
|
mkOption {
|
2015-08-17 19:52:45 +02:00
|
|
|
type = types.lines;
|
2015-07-20 15:28:49 +02:00
|
|
|
default = ''
|
|
|
|
while ! [ -e /var/run/docker.sock ]; do
|
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
The postStart phase of the systemd service. You may need to
|
|
|
|
override this if you are passing in flags to docker which
|
2015-12-24 12:07:45 +01:00
|
|
|
don't cause the socket file to be created. This option is ignored
|
|
|
|
if socket activation is used.
|
2015-07-20 15:28:49 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-07-28 00:00:59 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{ environment.systemPackages = [ pkgs.docker ];
|
2014-09-03 21:22:20 +02:00
|
|
|
users.extraGroups.docker.gid = config.ids.gids.docker;
|
2014-07-28 00:00:59 +02:00
|
|
|
systemd.services.docker = {
|
|
|
|
description = "Docker Application Container Engine";
|
2015-12-24 12:07:45 +01:00
|
|
|
wantedBy = optional (!cfg.socketActivation) "multi-user.target";
|
|
|
|
after = [ "network.target" ] ++ (optional cfg.socketActivation "docker.socket") ;
|
|
|
|
requires = optional cfg.socketActivation "docker.socket";
|
2014-07-28 00:00:59 +02:00
|
|
|
serviceConfig = {
|
2016-09-10 12:55:46 +02:00
|
|
|
ExecStart = ''${pkgs.docker}/bin/dockerd \
|
|
|
|
--group=docker --log-driver=${cfg.logDriver} \
|
|
|
|
${optionalString (cfg.storageDriver != null) "--storage-driver=${cfg.storageDriver}"} \
|
|
|
|
${optionalString cfg.socketActivation "--host=fd://"} \
|
|
|
|
${cfg.extraOptions}
|
|
|
|
'';
|
2014-07-28 00:00:59 +02:00
|
|
|
# I'm not sure if that limits aren't too high, but it's what
|
|
|
|
# goes in config bundled with docker itself
|
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
LimitNPROC = 1048576;
|
2014-11-07 12:46:36 +01:00
|
|
|
} // proxy_env;
|
2014-07-28 00:00:59 +02:00
|
|
|
|
2016-08-14 12:00:52 +02:00
|
|
|
path = [ pkgs.kmod ] ++ (optional (cfg.storageDriver == "zfs") pkgs.zfs);
|
2015-12-24 12:07:45 +01:00
|
|
|
|
|
|
|
postStart = if cfg.socketActivation then "" else cfg.postStart;
|
|
|
|
|
|
|
|
# Presumably some containers are running we don't want to interrupt
|
|
|
|
restartIfChanged = false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
(mkIf cfg.socketActivation {
|
2014-07-28 00:00:59 +02:00
|
|
|
systemd.sockets.docker = {
|
|
|
|
description = "Docker Socket for the API";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = "/var/run/docker.sock";
|
|
|
|
SocketMode = "0660";
|
|
|
|
SocketUser = "root";
|
|
|
|
SocketGroup = "docker";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|