at 24.11-pre 2.7 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 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; 11 12in 13 14{ 15 ###### interface 16 17 options.virtualisation.docker.rootless = { 18 enable = mkOption { 19 type = types.bool; 20 default = false; 21 description = '' 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`. 25 ''; 26 }; 27 28 setSocketVariable = mkOption { 29 type = types.bool; 30 default = false; 31 description = '' 32 Point {command}`DOCKER_HOST` to rootless Docker instance for 33 normal users by default. 34 ''; 35 }; 36 37 daemon.settings = mkOption { 38 type = settingsFormat.type; 39 default = { }; 40 example = { 41 ipv6 = true; 42 "fixed-cidr-v6" = "fd00::/80"; 43 }; 44 description = '' 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 47 ''; 48 }; 49 50 package = mkPackageOption pkgs "docker" { }; 51 }; 52 53 ###### implementation 54 55 config = mkIf cfg.enable { 56 environment.systemPackages = [ cfg.package ]; 57 58 environment.extraInit = optionalString cfg.setSocketVariable '' 59 if [ -z "$DOCKER_HOST" -a -n "$XDG_RUNTIME_DIR" ]; then 60 export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/docker.sock" 61 fi 62 ''; 63 64 # Taken from https://github.com/moby/moby/blob/master/contrib/dockerd-rootless-setuptool.sh 65 systemd.user.services.docker = { 66 wantedBy = [ "default.target" ]; 67 description = "Docker Application Container Engine (Rootless)"; 68 # needs newuidmap from pkgs.shadow 69 path = [ "/run/wrappers" ]; 70 environment = proxy_env; 71 unitConfig = { 72 # docker-rootless doesn't support running as root. 73 ConditionUser = "!root"; 74 StartLimitInterval = "60s"; 75 }; 76 serviceConfig = { 77 Type = "notify"; 78 ExecStart = "${cfg.package}/bin/dockerd-rootless --config-file=${daemonSettingsFile}"; 79 ExecReload = "${pkgs.procps}/bin/kill -s HUP $MAINPID"; 80 TimeoutSec = 0; 81 RestartSec = 2; 82 Restart = "always"; 83 StartLimitBurst = 3; 84 LimitNOFILE = "infinity"; 85 LimitNPROC = "infinity"; 86 LimitCORE = "infinity"; 87 Delegate = true; 88 NotifyAccess = "all"; 89 KillMode = "mixed"; 90 }; 91 }; 92 }; 93 94}