1 { config, lib, pkgs, ... }:
7 cfg = config.virtualisation.docker.rootless;
8 proxy_env = config.networking.proxy.envVars;
9 settingsFormat = pkgs.formats.json {};
10 daemonSettingsFile = settingsFormat.generate "daemon.json" cfg.daemon.settings;
17 options.virtualisation.docker.rootless = {
21 description = lib.mdDoc ''
22 This option enables docker in a rootless mode, a daemon that manages
23 linux containers. To interact with the daemon, one needs to set
24 {command}`DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock`.
28 setSocketVariable = mkOption {
31 description = lib.mdDoc ''
32 Point {command}`DOCKER_HOST` to rootless Docker instance for
33 normal users by default.
37 daemon.settings = mkOption {
38 type = settingsFormat.type;
42 "fixed-cidr-v6" = "fd00::/80";
44 description = lib.mdDoc ''
45 Configuration for docker daemon. The attributes are serialized to JSON used as daemon.conf.
46 See https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file
51 default = pkgs.docker;
52 defaultText = literalExpression "pkgs.docker";
54 description = lib.mdDoc ''
55 Docker package to be used in the module.
62 config = mkIf cfg.enable {
63 environment.systemPackages = [ cfg.package ];
65 environment.extraInit = optionalString cfg.setSocketVariable ''
66 if [ -z "$DOCKER_HOST" -a -n "$XDG_RUNTIME_DIR" ]; then
67 export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock"
71 # Taken from https://github.com/moby/moby/blob/master/contrib/dockerd-rootless-setuptool.sh
72 systemd.user.services.docker = {
73 wantedBy = [ "default.target" ];
74 description = "Docker Application Container Engine (Rootless)";
75 # needs newuidmap from pkgs.shadow
76 path = [ "/run/wrappers" ];
77 environment = proxy_env;
79 # docker-rootless doesn't support running as root.
80 ConditionUser = "!root";
81 StartLimitInterval = "60s";
85 ExecStart = "${cfg.package}/bin/dockerd-rootless --config-file=${daemonSettingsFile}";
86 ExecReload = "${pkgs.procps}/bin/kill -s HUP $MAINPID";
91 LimitNOFILE = "infinity";
92 LimitNPROC = "infinity";
93 LimitCORE = "infinity";