Release NixOS 23.11
[NixPkgs.git] / nixos / modules / virtualisation / docker-rootless.nix
blobf4e4bdc0963a7a00fb9c6b3b0c5b612dbce25bf7
1 { config, lib, pkgs, ... }:
3 with lib;
5 let
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;
15   ###### interface
17   options.virtualisation.docker.rootless = {
18     enable = mkOption {
19       type = types.bool;
20       default = false;
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`.
25       '';
26     };
28     setSocketVariable = mkOption {
29       type = types.bool;
30       default = false;
31       description = lib.mdDoc ''
32         Point {command}`DOCKER_HOST` to rootless Docker instance for
33         normal users by default.
34       '';
35     };
37     daemon.settings = mkOption {
38       type = settingsFormat.type;
39       default = { };
40       example = {
41         ipv6 = true;
42         "fixed-cidr-v6" = "fd00::/80";
43       };
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
47       '';
48     };
50     package = mkOption {
51       default = pkgs.docker;
52       defaultText = literalExpression "pkgs.docker";
53       type = types.package;
54       description = lib.mdDoc ''
55         Docker package to be used in the module.
56       '';
57     };
58   };
60   ###### implementation
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"
68       fi
69     '';
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;
78       unitConfig = {
79         # docker-rootless doesn't support running as root.
80         ConditionUser = "!root";
81         StartLimitInterval = "60s";
82       };
83       serviceConfig = {
84         Type = "notify";
85         ExecStart = "${cfg.package}/bin/dockerd-rootless --config-file=${daemonSettingsFile}";
86         ExecReload = "${pkgs.procps}/bin/kill -s HUP $MAINPID";
87         TimeoutSec = 0;
88         RestartSec = 2;
89         Restart = "always";
90         StartLimitBurst = 3;
91         LimitNOFILE = "infinity";
92         LimitNPROC = "infinity";
93         LimitCORE = "infinity";
94         Delegate = true;
95         NotifyAccess = "all";
96         KillMode = "mixed";
97       };
98     };
99   };