| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  | # Systemd services for docker. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cfg = config.virtualisation.docker; | 
					
						
							| 
									
										
										
										
											2017-04-26 16:55:36 +02:00
										 |  |  |   proxy_env = config.networking.proxy.envVars; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03: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 23:24:17 +01: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-28 01:00:59 +03:00
										 |  |  |       mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							| 
									
										
										
										
											2015-11-20 23:01:33 +01:00
										 |  |  |         default = true; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |         description = | 
					
						
							|  |  |  |           ''
 | 
					
						
							| 
									
										
										
										
											2016-12-20 23:24:17 +01: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-28 01:00:59 +03:00
										 |  |  |           '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2016-12-20 23:24:17 +01: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-04 00:18:19 +01: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 00:18:19 +01: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 00:18:19 +01: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 01:00:59 +03:00
										 |  |  |     extraOptions = | 
					
						
							|  |  |  |       mkOption { | 
					
						
							| 
									
										
										
										
											2015-04-25 15:25:15 +02:00
										 |  |  |         type = types.separatedString " "; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |         default = ""; | 
					
						
							|  |  |  |         description = | 
					
						
							|  |  |  |           ''
 | 
					
						
							|  |  |  |             The extra command-line options to pass to | 
					
						
							|  |  |  |             <command>docker</command> daemon. | 
					
						
							|  |  |  |           '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2017-07-19 18:20:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     autoPrune = { | 
					
						
							|  |  |  |       enable = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = false; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2017-07-21 16:53:50 +02: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 18:20:46 +02: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. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ###### implementation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-24 01:44:10 +01:00
										 |  |  |   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; | 
					
						
							| 
									
										
										
										
											2016-12-24 01:44:10 +01:00
										 |  |  |       systemd.packages = [ pkgs.docker ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |       systemd.services.docker = { | 
					
						
							| 
									
										
										
										
											2016-12-20 23:24:17 +01:00
										 |  |  |         wantedBy = optional cfg.enableOnBoot "multi-user.target"; | 
					
						
							| 
									
										
										
										
											2017-04-26 16:55:36 +02:00
										 |  |  |         environment = proxy_env; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |         serviceConfig = { | 
					
						
							| 
									
										
										
										
											2016-12-24 01:44:10 +01:00
										 |  |  |           ExecStart = [ | 
					
						
							|  |  |  |             "" | 
					
						
							|  |  |  |             ''
 | 
					
						
							|  |  |  |               ${pkgs.docker}/bin/dockerd \ | 
					
						
							|  |  |  |                 --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 16:55:36 +02:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 13:00:52 +03:00
										 |  |  |         path = [ pkgs.kmod ] ++ (optional (cfg.storageDriver == "zfs") pkgs.zfs); | 
					
						
							| 
									
										
										
										
											2015-12-24 12:07:45 +01:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2017-03-27 16:11:44 +03: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 18:20:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       systemd.services.docker-prune = { | 
					
						
							|  |  |  |         description = "Prune docker resources"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         restartIfChanged = false; | 
					
						
							|  |  |  |         unitConfig.X-StopOnRemoval = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         serviceConfig.Type = "oneshot"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         script = ''
 | 
					
						
							|  |  |  |           ${pkgs.docker}/bin/docker system prune -f ${toString cfg.autoPrune.flags} | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates; | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2016-12-20 23:24:17 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  |   ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-01 09:01:03 +01:00
										 |  |  |   imports = [ | 
					
						
							|  |  |  |     (mkRemovedOptionModule ["virtualisation" "docker" "socketActivation"] "This option was removed in favor of starting docker at boot") | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-28 01:00:59 +03:00
										 |  |  | } |