2014-07-27 23:00:59 +01:00
|
|
|
# Systemd services for docker.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualisation.docker;
|
2014-12-01 16:20:18 +00:00
|
|
|
pro = config.networking.proxy.default;
|
|
|
|
proxy_env = optionalAttrs (pro != null) { Environment = "\"http_proxy=${pro}\""; };
|
2014-07-27 23:00:59 +01: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 22:01:33 +00:00
|
|
|
default = true;
|
2014-07-27 23:00:59 +01:00
|
|
|
description =
|
|
|
|
''
|
|
|
|
This option enables docker with socket activation. I.e. docker will
|
|
|
|
start when first called by client.
|
|
|
|
'';
|
|
|
|
};
|
2015-09-04 00:18:19 +01:00
|
|
|
storageDriver =
|
|
|
|
mkOption {
|
2016-09-10 11:55:46 +01:00
|
|
|
type = types.nullOr (types.enum ["aufs" "btrfs" "devicemapper" "overlay" "overlay2" "zfs"]);
|
|
|
|
default = null;
|
2015-09-04 00:18:19 +01:00
|
|
|
description =
|
|
|
|
''
|
2016-09-10 11:55:46 +01:00
|
|
|
This option determines which Docker storage driver to use. By default
|
|
|
|
it let's docker automatically choose preferred storage driver.
|
2015-09-04 00:18:19 +01:00
|
|
|
'';
|
|
|
|
};
|
2016-09-10 11:55:46 +01: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-27 23:00:59 +01:00
|
|
|
extraOptions =
|
|
|
|
mkOption {
|
2015-04-25 14:25:15 +01:00
|
|
|
type = types.separatedString " ";
|
2014-07-27 23:00:59 +01:00
|
|
|
default = "";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The extra command-line options to pass to
|
|
|
|
<command>docker</command> daemon.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-07-20 14:28:49 +01:00
|
|
|
postStart =
|
|
|
|
mkOption {
|
2015-08-17 18:52:45 +01:00
|
|
|
type = types.lines;
|
2015-07-20 14:28:49 +01: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 11:07:45 +00:00
|
|
|
don't cause the socket file to be created. This option is ignored
|
|
|
|
if socket activation is used.
|
2015-07-20 14:28:49 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-07-27 23:00:59 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
{ environment.systemPackages = [ pkgs.docker ];
|
2014-09-03 20:22:20 +01:00
|
|
|
users.extraGroups.docker.gid = config.ids.gids.docker;
|
2014-07-27 23:00:59 +01:00
|
|
|
systemd.services.docker = {
|
|
|
|
description = "Docker Application Container Engine";
|
2015-12-24 11:07:45 +00:00
|
|
|
wantedBy = optional (!cfg.socketActivation) "multi-user.target";
|
|
|
|
after = [ "network.target" ] ++ (optional cfg.socketActivation "docker.socket") ;
|
|
|
|
requires = optional cfg.socketActivation "docker.socket";
|
2014-07-27 23:00:59 +01:00
|
|
|
serviceConfig = {
|
2016-09-10 11:55:46 +01: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-27 23:00:59 +01: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 11:46:36 +00:00
|
|
|
} // proxy_env;
|
2014-07-27 23:00:59 +01:00
|
|
|
|
2016-08-14 11:00:52 +01:00
|
|
|
path = [ pkgs.kmod ] ++ (optional (cfg.storageDriver == "zfs") pkgs.zfs);
|
2015-12-24 11:07:45 +00: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-27 23:00:59 +01: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";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|