1 { config, lib, pkgs, ... }:
4 cfg = config.virtualisation.docker.rootless;
5 proxy_env = config.networking.proxy.envVars;
6 settingsFormat = pkgs.formats.json {};
7 daemonSettingsFile = settingsFormat.generate "daemon.json" cfg.daemon.settings;
14 options.virtualisation.docker.rootless = {
15 enable = lib.mkOption {
16 type = lib.types.bool;
19 This option enables docker in a rootless mode, a daemon that manages
20 linux containers. To interact with the daemon, one needs to set
21 {command}`DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock`.
25 setSocketVariable = lib.mkOption {
26 type = lib.types.bool;
29 Point {command}`DOCKER_HOST` to rootless Docker instance for
30 normal users by default.
34 daemon.settings = lib.mkOption {
35 type = settingsFormat.type;
39 "fixed-cidr-v6" = "fd00::/80";
42 Configuration for docker daemon. The attributes are serialized to JSON used as daemon.conf.
43 See https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file
47 package = lib.mkPackageOption pkgs "docker" { };
52 config = lib.mkIf cfg.enable {
53 environment.systemPackages = [ cfg.package ];
55 environment.extraInit = lib.optionalString cfg.setSocketVariable ''
56 if [ -z "$DOCKER_HOST" -a -n "$XDG_RUNTIME_DIR" ]; then
57 export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock"
61 # Taken from https://github.com/moby/moby/blob/master/contrib/dockerd-rootless-setuptool.sh
62 systemd.user.services.docker = {
63 wantedBy = [ "default.target" ];
64 description = "Docker Application Container Engine (Rootless)";
65 # needs newuidmap from pkgs.shadow
66 path = [ "/run/wrappers" ];
67 environment = proxy_env;
69 # docker-rootless doesn't support running as root.
70 ConditionUser = "!root";
71 StartLimitInterval = "60s";
75 ExecStart = "${cfg.package}/bin/dockerd-rootless --config-file=${daemonSettingsFile}";
76 ExecReload = "${pkgs.procps}/bin/kill -s HUP $MAINPID";
81 LimitNOFILE = "infinity";
82 LimitNPROC = "infinity";
83 LimitCORE = "infinity";