2023-09-14 15:54:48 +02:00
{ config , pkgs , . . . }:
{
# https://nixos.wiki/wiki/Docker#Installation
# TODO: rootless Docker. https://nixos.wiki/wiki/Docker#Rootless_docker
# TODO: run as systemd services. https://nixos.wiki/wiki/Docker#docker_containers_as_systemd_services
virtualisation = {
docker = {
enable = true ;
# As we use btrfs, we enable the according storageDriver option.
storageDriver = " b t r f s " ;
2023-10-09 12:06:15 +02:00
# Run `docker system prune -f ${autoPrune.flags}` every week.
2023-09-14 15:54:48 +02:00
autoPrune . enable = true ;
autoPrune . dates = " w e e k l y " ;
# https://docs.docker.com/engine/reference/commandline/system_prune/#options
autoPrune . flags = [
" - - a l l "
# https://docs.docker.com/engine/reference/commandline/system_prune/#filter
# https://pkg.go.dev/maze.io/x/duration#ParseDuration
2023-10-09 12:06:15 +02:00
# `7d` could not be parsed, thus we use `168h`
" - - f i l t e r u n t i l = 1 6 8 h "
# `--volumes` can't be used together with `--filter`.
# Thus we added our own service below.
#--volumes
2023-09-14 15:54:48 +02:00
] ;
} ;
} ;
2023-10-08 20:03:00 +02:00
2023-10-09 12:06:15 +02:00
# Prune docker volumes.
# This is a slightly modified copy of https://github.com/NixOS/nixpkgs/blob/5a237aecb57296f67276ac9ab296a41c23981f56/nixos/modules/virtualisation/docker.nix#L211C5-L226
systemd . services . " d o c k e r - p r u n e - v o l u m e s " = {
description = " P r u n e d o c k e r v o l u m e s " ;
restartIfChanged = false ;
unitConfig . X-StopOnRemoval = false ;
serviceConfig . Type = " o n e s h o t " ;
script = ''
$ { pkgs . docker } /bin/docker system prune - f - - all - - volumes
'' ;
startAt = " w e e k l y " ;
after = [ " d o c k e r . s e r v i c e " ] ;
requires = [ " d o c k e r . s e r v i c e " ] ;
} ;
2023-10-08 20:03:00 +02:00
# Monitor unhealthy Docker containers.
systemd . timers . " d o c k e r - h e a l t h " = {
wantedBy = [ " t i m e r s . t a r g e t " ] ;
partOf = [ " d o c k e r - h e a l t h . s e r v i c e " ] ;
timerConfig = {
OnBootSec = " 0 m " ;
OnUnitInactiveSec = " 3 m " ;
AccuracySec = " 1 5 s " ;
RandomizedDelaySec = " 1 5 s " ;
} ;
} ;
systemd . services . " d o c k e r - h e a l t h " = {
serviceConfig = {
Type = " o n e s h o t " ;
PrivateTmp = true ;
# `docker` requires root access.
User = " r o o t " ;
Nice = 19 ;
IOSchedulingClass = " i d l e " ;
} ;
path = with pkgs ; [
docker
] ;
# If there are no unhealthy Docker containers, the output of `docker ps -f health=unhealthy` is just one line:
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# We filter this line with `grep -v`.
# As a result, grep returns exit code 1 if there are no unhealthy containers (as not a single line is printed).
# Thus, we prefix the whole command with `!`.
# Lastly, we redirect stdout to stderr with `1>&2` so that unhealthy containers are written to stderr.
script = ''
set - eu - o pipefail
! sudo docker ps - f health = unhealthy | grep - v ' CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES' 1 > & 2
'' ;
} ;
2023-09-14 15:54:48 +02:00
}