2021-12-14 23:07:58 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualisation.docker.rootless;
|
|
|
|
proxy_env = config.networking.proxy.envVars;
|
|
|
|
settingsFormat = pkgs.formats.json {};
|
|
|
|
daemonSettingsFile = settingsFormat.generate "daemon.json" cfg.daemon.settings;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.virtualisation.docker.rootless = {
|
2024-08-28 21:19:12 +02:00
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-12-14 23:07:58 +01:00
|
|
|
default = false;
|
2022-07-20 12:32:04 +02:00
|
|
|
description = ''
|
2021-12-14 23:07:58 +01:00
|
|
|
This option enables docker in a rootless mode, a daemon that manages
|
|
|
|
linux containers. To interact with the daemon, one needs to set
|
2022-07-20 12:32:04 +02:00
|
|
|
{command}`DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock`.
|
2021-12-14 23:07:58 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:12 +02:00
|
|
|
setSocketVariable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-12-14 23:07:58 +01:00
|
|
|
default = false;
|
2022-07-20 12:32:04 +02:00
|
|
|
description = ''
|
|
|
|
Point {command}`DOCKER_HOST` to rootless Docker instance for
|
2021-12-14 23:07:58 +01:00
|
|
|
normal users by default.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:12 +02:00
|
|
|
daemon.settings = lib.mkOption {
|
2021-12-14 23:07:58 +01:00
|
|
|
type = settingsFormat.type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
ipv6 = true;
|
|
|
|
"fixed-cidr-v6" = "fd00::/80";
|
|
|
|
};
|
2022-07-20 12:32:04 +02:00
|
|
|
description = ''
|
2021-12-14 23:07:58 +01:00
|
|
|
Configuration for docker daemon. The attributes are serialized to JSON used as daemon.conf.
|
|
|
|
See https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:12 +02:00
|
|
|
package = lib.mkPackageOption pkgs "docker" { };
|
2021-12-14 23:07:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-08-28 21:19:12 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
2021-12-14 23:07:58 +01:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
2024-08-28 21:19:12 +02:00
|
|
|
environment.extraInit = lib.optionalString cfg.setSocketVariable ''
|
2021-12-14 23:07:58 +01:00
|
|
|
if [ -z "$DOCKER_HOST" -a -n "$XDG_RUNTIME_DIR" ]; then
|
|
|
|
export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Taken from https://github.com/moby/moby/blob/master/contrib/dockerd-rootless-setuptool.sh
|
|
|
|
systemd.user.services.docker = {
|
|
|
|
wantedBy = [ "default.target" ];
|
|
|
|
description = "Docker Application Container Engine (Rootless)";
|
|
|
|
# needs newuidmap from pkgs.shadow
|
|
|
|
path = [ "/run/wrappers" ];
|
|
|
|
environment = proxy_env;
|
2022-02-05 12:37:11 +01:00
|
|
|
unitConfig = {
|
|
|
|
# docker-rootless doesn't support running as root.
|
|
|
|
ConditionUser = "!root";
|
|
|
|
StartLimitInterval = "60s";
|
|
|
|
};
|
2021-12-14 23:07:58 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "notify";
|
|
|
|
ExecStart = "${cfg.package}/bin/dockerd-rootless --config-file=${daemonSettingsFile}";
|
|
|
|
ExecReload = "${pkgs.procps}/bin/kill -s HUP $MAINPID";
|
|
|
|
TimeoutSec = 0;
|
|
|
|
RestartSec = 2;
|
|
|
|
Restart = "always";
|
|
|
|
StartLimitBurst = 3;
|
|
|
|
LimitNOFILE = "infinity";
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
LimitCORE = "infinity";
|
|
|
|
Delegate = true;
|
|
|
|
NotifyAccess = "all";
|
|
|
|
KillMode = "mixed";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|