Commit 7719b92c0dbe

Vincent Demeester <vincent@sbr.pm>
2022-02-23 16:21:02
system/modules: refactor buildkit module…
… similar to upstream containerd, with settings and the buildkitd toml config. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent e371299
Changed files (2)
systems
modules
profiles
virtualisation
systems/modules/profiles/docker.nix
@@ -24,12 +24,29 @@ in
     virtualisation = {
       containerd = {
         enable = true;
-        # autostart = false;
       };
       buildkitd = {
         enable = true;
-        extraOptions = "--oci-worker=false --containerd-worker=true";
-        # autostart = false;
+        settings = {
+          worker.oci = {
+            enabled = false;
+          };
+          worker.containerd = {
+            enable = true;
+            platforms = [ "linux/amd64" "linux/arm64" ];
+            namespace = "buildkit";
+          };
+          registry = {
+            "r.svc.home:5000" = {
+              http = true;
+              insecure = true;
+            };
+            "r.svc.home" = {
+              http = true;
+              insecure = true;
+            };
+          };
+        };
       };
       docker = {
         enable = true;
systems/modules/virtualisation/buildkit.nix
@@ -1,22 +1,28 @@
 { config, lib, pkgs, ... }:
 let
   cfg = config.virtualisation.buildkitd;
-  inherit (lib) mkOption mkIf types;
+  inherit (lib) mkOption mkIf;
+  inherit (lib.types) attrsOf str nullOr path bool package listOf;
+
+  configFile =
+    if cfg.configFile == null then
+      settingsFormat.generate "buildkitd.toml" cfg.settings
+    else
+      cfg.configFile;
+
+  settingsFormat = pkgs.formats.toml { };
 in
 {
   options.virtualisation.buildkitd = {
     enable = mkOption {
-      type = types.bool;
+      type = bool;
       default = false;
-      description =
-        ''
-          This option enables buildkitd
-        '';
+      description = ''This option enables buildkitd'';
     };
 
     package = mkOption {
       default = pkgs.buildkit;
-      type = types.package;
+      type = package;
       example = pkgs.buildkit;
       description = ''
         Buildkitd package to be used in the module
@@ -24,19 +30,32 @@ in
     };
 
     packages = mkOption {
-      type = types.listOf types.package;
+      type = listOf package;
       default = [ pkgs.runc pkgs.git ];
       description = "List of packages to be added to buildkitd service path";
     };
 
-    extraOptions = mkOption {
-      type = types.separatedString " ";
-      default = "";
-      description =
-        ''
-          The extra command-line options to pass to
-          <command>buildkitd</command> daemon.
-        '';
+    configFile = lib.mkOption {
+      default = null;
+      description = ''
+        Path to containerd config file.
+        Setting this option will override any configuration applied by the settings option.
+      '';
+      type = nullOr path;
+    };
+
+    args = lib.mkOption {
+      default = { };
+      description = "extra args to append to the containerd cmdline";
+      type = attrsOf str;
+    };
+
+    settings = lib.mkOption {
+      type = settingsFormat.type;
+      default = { };
+      description = ''
+        Verbatim lines to add to containerd.toml
+      '';
     };
   };
 
@@ -44,11 +63,23 @@ in
     users.groups.buildkit.gid = 350;
     environment.systemPackages = [ cfg.package ];
     systemd.packages = [ cfg.package ];
+
+    virtualisation.buildkitd = {
+      args = {
+        addr = "unix:///run/buildkit/buildkitd.sock";
+        group = "buildkit";
+        config = toString configFile;
+      };
+      settings = {
+        debug = false;
+      };
+    };
+
     systemd.services.buildkitd = {
       after = [ "network.target" "containerd.service" ];
       wantedBy = [ "multi-user.target" ];
       serviceConfig = {
-        ExecStart = ''${cfg.package}/bin/buildkitd --addr=unix:///run/buildkit/buildkitd.sock --group=buildkit ${cfg.extraOptions}'';
+        ExecStart = ''${cfg.package}/bin/buildkitd ${lib.concatStringsSep " " (lib.cli.toGNUCommandLine {} cfg.args)}'';
         Delegate = "yes";
         KillMode = "process";
         Type = "notify";