2014-07-27 22:00:59 +00:00
|
|
|
# Systemd services for docker.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualisation.docker;
|
2017-04-26 14:55:36 +00:00
|
|
|
proxy_env = config.networking.proxy.envVars;
|
2014-07-27 22:00:59 +00: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.
|
|
|
|
'';
|
|
|
|
};
|
2016-12-20 22:24:17 +00:00
|
|
|
|
|
|
|
listenOptions =
|
|
|
|
mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = ["/var/run/docker.sock"];
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
A list of unix and tcp docker should listen to. The format follows
|
|
|
|
ListenStream as described in systemd.socket(5).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
enableOnBoot =
|
2014-07-27 22:00:59 +00:00
|
|
|
mkOption {
|
|
|
|
type = types.bool;
|
2015-11-20 22:01:33 +00:00
|
|
|
default = true;
|
2014-07-27 22:00:59 +00:00
|
|
|
description =
|
|
|
|
''
|
2016-12-20 22:24:17 +00:00
|
|
|
When enabled dockerd is started on boot. This is required for
|
|
|
|
container, which are created with the
|
|
|
|
<literal>--restart=always</literal> flag, to work. If this option is
|
|
|
|
disabled, docker might be started on demand by socket activation.
|
2014-07-27 22:00:59 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-12-20 22:24:17 +00:00
|
|
|
|
|
|
|
liveRestore =
|
|
|
|
mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Allow dockerd to be restarted without affecting running container.
|
|
|
|
This option is incompatible with docker swarm.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-03 23:18:19 +00:00
|
|
|
storageDriver =
|
|
|
|
mkOption {
|
2016-09-10 10:55:46 +00:00
|
|
|
type = types.nullOr (types.enum ["aufs" "btrfs" "devicemapper" "overlay" "overlay2" "zfs"]);
|
|
|
|
default = null;
|
2015-09-03 23:18:19 +00:00
|
|
|
description =
|
|
|
|
''
|
2016-09-10 10:55:46 +00:00
|
|
|
This option determines which Docker storage driver to use. By default
|
|
|
|
it let's docker automatically choose preferred storage driver.
|
2015-09-03 23:18:19 +00:00
|
|
|
'';
|
|
|
|
};
|
2016-09-10 10:55:46 +00: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 22:00:59 +00:00
|
|
|
extraOptions =
|
|
|
|
mkOption {
|
2015-04-25 13:25:15 +00:00
|
|
|
type = types.separatedString " ";
|
2014-07-27 22:00:59 +00:00
|
|
|
default = "";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The extra command-line options to pass to
|
|
|
|
<command>docker</command> daemon.
|
|
|
|
'';
|
|
|
|
};
|
2017-07-19 16:20:46 +00:00
|
|
|
|
|
|
|
autoPrune = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
2017-07-21 14:53:50 +00:00
|
|
|
Whether to periodically prune Docker resources. If enabled, a
|
|
|
|
systemd timer will run <literal>docker system prune -f</literal>
|
|
|
|
as specified by the <literal>dates</literal> option.
|
2017-07-19 16:20:46 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
flags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "--all" ];
|
|
|
|
description = ''
|
|
|
|
Any additional flags passed to <command>docker system prune</command>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dates = mkOption {
|
|
|
|
default = "weekly";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Specification (in the format described by
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>) of the time at
|
|
|
|
which the prune will occur.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2017-09-04 23:02:05 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.docker;
|
|
|
|
type = types.package;
|
|
|
|
example = pkgs.docker-edge;
|
|
|
|
description = ''
|
|
|
|
Docker package to be used in the module.
|
|
|
|
'';
|
|
|
|
};
|
2014-07-27 22:00:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2016-12-24 00:44:10 +00:00
|
|
|
config = mkIf cfg.enable (mkMerge [{
|
2017-09-04 23:02:05 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
2014-09-03 19:22:20 +00:00
|
|
|
users.extraGroups.docker.gid = config.ids.gids.docker;
|
2017-09-04 23:02:05 +00:00
|
|
|
systemd.packages = [ cfg.package ];
|
2016-12-24 00:44:10 +00:00
|
|
|
|
2014-07-27 22:00:59 +00:00
|
|
|
systemd.services.docker = {
|
2016-12-20 22:24:17 +00:00
|
|
|
wantedBy = optional cfg.enableOnBoot "multi-user.target";
|
2017-04-26 14:55:36 +00:00
|
|
|
environment = proxy_env;
|
2014-07-27 22:00:59 +00:00
|
|
|
serviceConfig = {
|
2016-12-24 00:44:10 +00:00
|
|
|
ExecStart = [
|
|
|
|
""
|
|
|
|
''
|
2017-09-04 23:02:05 +00:00
|
|
|
${cfg.package}/bin/dockerd \
|
2016-12-24 00:44:10 +00:00
|
|
|
--group=docker \
|
|
|
|
--host=fd:// \
|
|
|
|
--log-driver=${cfg.logDriver} \
|
|
|
|
${optionalString (cfg.storageDriver != null) "--storage-driver=${cfg.storageDriver}"} \
|
|
|
|
${optionalString cfg.liveRestore "--live-restore" } \
|
|
|
|
${cfg.extraOptions}
|
|
|
|
''];
|
|
|
|
ExecReload=[
|
|
|
|
""
|
|
|
|
"${pkgs.procps}/bin/kill -s HUP $MAINPID"
|
|
|
|
];
|
2017-04-26 14:55:36 +00:00
|
|
|
};
|
2014-07-27 22:00:59 +00:00
|
|
|
|
2016-08-14 10:00:52 +00:00
|
|
|
path = [ pkgs.kmod ] ++ (optional (cfg.storageDriver == "zfs") pkgs.zfs);
|
2015-12-24 11:07:45 +00:00
|
|
|
};
|
2017-03-27 13:11:44 +00:00
|
|
|
|
|
|
|
systemd.sockets.docker = {
|
|
|
|
description = "Docker Socket for the API";
|
|
|
|
wantedBy = [ "sockets.target" ];
|
|
|
|
socketConfig = {
|
|
|
|
ListenStream = cfg.listenOptions;
|
|
|
|
SocketMode = "0660";
|
|
|
|
SocketUser = "root";
|
|
|
|
SocketGroup = "docker";
|
|
|
|
};
|
|
|
|
};
|
2017-07-19 16:20:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
systemd.services.docker-prune = {
|
|
|
|
description = "Prune docker resources";
|
|
|
|
|
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
|
|
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
|
|
|
script = ''
|
2017-09-04 23:02:05 +00:00
|
|
|
${cfg.package}/bin/docker system prune -f ${toString cfg.autoPrune.flags}
|
2017-07-19 16:20:46 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates;
|
|
|
|
};
|
2016-12-20 22:24:17 +00:00
|
|
|
}
|
2014-07-27 22:00:59 +00:00
|
|
|
]);
|
|
|
|
|
2017-01-01 08:01:03 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule ["virtualisation" "docker" "socketActivation"] "This option was removed in favor of starting docker at boot")
|
|
|
|
];
|
|
|
|
|
2014-07-27 22:00:59 +00:00
|
|
|
}
|