summary refs log tree commit diff
path: root/nixos/modules/virtualisation/docker-rootless.nix
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2021-12-15 01:07:58 +0300
committerNikolay Amiantov <ab@fmap.me>2021-12-22 14:23:23 +0300
commitab64310a5e5534a8c96d6523ba5a765d27beceaf (patch)
treedf239bf068804458e7a61b8d8df862c5f09f304a /nixos/modules/virtualisation/docker-rootless.nix
parent721fde93ffcc941b7486e3ec77121d9a8ddbd64f (diff)
downloadnixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar.gz
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar.bz2
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar.lz
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar.xz
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.tar.zst
nixpkgs-ab64310a5e5534a8c96d6523ba5a765d27beceaf.zip
docker-rootless service: init
Diffstat (limited to 'nixos/modules/virtualisation/docker-rootless.nix')
-rw-r--r--nixos/modules/virtualisation/docker-rootless.nix98
1 files changed, 98 insertions, 0 deletions
diff --git a/nixos/modules/virtualisation/docker-rootless.nix b/nixos/modules/virtualisation/docker-rootless.nix
new file mode 100644
index 00000000000..0e7f0503142
--- /dev/null
+++ b/nixos/modules/virtualisation/docker-rootless.nix
@@ -0,0 +1,98 @@
+{ config, lib, pkgs, ... }:
+
+with lib;
+
+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 = {
+    enable = mkOption {
+      type = types.bool;
+      default = false;
+      description = ''
+        This option enables docker in a rootless mode, a daemon that manages
+        linux containers. To interact with the daemon, one needs to set
+        <command>DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock</command>.
+      '';
+    };
+
+    setSocketVariable = mkOption {
+      type = types.bool;
+      default = false;
+      description = ''
+        Point <command>DOCKER_HOST</command> to rootless Docker instance for
+        normal users by default.
+      '';
+    };
+
+    daemon.settings = mkOption {
+      type = settingsFormat.type;
+      default = { };
+      example = {
+        ipv6 = true;
+        "fixed-cidr-v6" = "fd00::/80";
+      };
+      description = ''
+        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
+      '';
+    };
+
+    package = mkOption {
+      default = pkgs.docker;
+      defaultText = literalExpression "pkgs.docker";
+      type = types.package;
+      example = literalExpression "pkgs.docker-edge";
+      description = ''
+        Docker package to be used in the module.
+      '';
+    };
+  };
+
+  ###### implementation
+
+  config = mkIf cfg.enable {
+    environment.systemPackages = [ cfg.package ];
+
+    environment.extraInit = optionalString cfg.setSocketVariable ''
+      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;
+      unitConfig.StartLimitInterval = "60s";
+      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";
+      };
+    };
+  };
+
+}