about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-04-13 15:20:45 -0700
committerFranck Cuny <franck@fcuny.net>2022-04-13 15:20:45 -0700
commitd95497cf3a7728f4a0d43bf4f54eb4a796ba0870 (patch)
tree3df15db490f3da6f83d15e2e732196bb48cb2525
parentnginx: configure correctly the dashboard (diff)
downloadworld-d95497cf3a7728f4a0d43bf4f54eb4a796ba0870.tar.gz
drone: initial attempt at configuring it
Diffstat (limited to '')
-rw-r--r--hosts/tahoe/secrets/drone/secrets.agebin0 -> 697 bytes
-rw-r--r--hosts/tahoe/secrets/drone/shared-secrets5
-rw-r--r--hosts/tahoe/secrets/secrets.nix5
-rw-r--r--hosts/tahoe/services.nix6
-rw-r--r--modules/services/default.nix1
-rw-r--r--modules/services/drone/default.nix39
-rw-r--r--modules/services/drone/runner-docker/default.nix39
-rw-r--r--modules/services/drone/runner-exec/default.nix54
-rw-r--r--modules/services/drone/server/default.nix45
9 files changed, 194 insertions, 0 deletions
diff --git a/hosts/tahoe/secrets/drone/secrets.age b/hosts/tahoe/secrets/drone/secrets.age
new file mode 100644
index 0000000..618bbc6
--- /dev/null
+++ b/hosts/tahoe/secrets/drone/secrets.age
Binary files differdiff --git a/hosts/tahoe/secrets/drone/shared-secrets b/hosts/tahoe/secrets/drone/shared-secrets
new file mode 100644
index 0000000..47612be
--- /dev/null
+++ b/hosts/tahoe/secrets/drone/shared-secrets
@@ -0,0 +1,5 @@
+DRONE_GITEA_CLIENT_ID=21ef7412-a58a-493c-beec-2e1dc27ebe79
+DRONE_GITEA_CLIENT_SECRET=GCXGi97PXxAoMTpHveMtNJXDyzdvI8jeC0TaEtCgpPab
+DRONE_GITEA_SERVER=https://git.fcuny.net
+DRONE_GIT_ALWAYS_AUTH=1
+DRONE_RPC_SECRET=d3daa6782d0f4ed66f7f557fa384ff8f
diff --git a/hosts/tahoe/secrets/secrets.nix b/hosts/tahoe/secrets/secrets.nix
index d9ca4d9..60b8550 100644
--- a/hosts/tahoe/secrets/secrets.nix
+++ b/hosts/tahoe/secrets/secrets.nix
@@ -13,6 +13,11 @@ in {
     owner = "acme";
   };
 
+  "drone/secrets.age" = {
+    publicKeys = all;
+    owner = "drone";
+  };
+
   "unifi/unifi-poller.age".publicKeys = all;
 
   "restic/repo-systems.age".publicKeys = all;
diff --git a/hosts/tahoe/services.nix b/hosts/tahoe/services.nix
index 6ad8456..a3533da 100644
--- a/hosts/tahoe/services.nix
+++ b/hosts/tahoe/services.nix
@@ -24,6 +24,12 @@ in {
       enable = true;
       stateDir = "/var/lib/gitea";
     };
+    drone = {
+      enable = true;
+      vhostName = "drone.fcuny.xyz";
+      runners = [ "docker" "exec" ];
+      sharedSecretFile = secrets."drone/secrets".path;
+    };
     rclone = { enable = true; };
     nginx = { enable = true; };
     transmission = {
diff --git a/modules/services/default.nix b/modules/services/default.nix
index 85eed9e..693872f 100644
--- a/modules/services/default.nix
+++ b/modules/services/default.nix
@@ -4,6 +4,7 @@
   imports = [
     ./avahi
     ./backup
+    ./drone
     ./fwupd
     ./gitea
     ./gnome
diff --git a/modules/services/drone/default.nix b/modules/services/drone/default.nix
new file mode 100644
index 0000000..352ee3d
--- /dev/null
+++ b/modules/services/drone/default.nix
@@ -0,0 +1,39 @@
+# A docker-based CI/CD system
+#
+# Inspired by [1]
+# [1]: https://github.com/Mic92/dotfiles/blob/master/nixos/eve/modules/drone.nix
+{ lib, ... }: {
+  imports = [ ./runner-docker ./runner-exec ./server ];
+
+  options.my.services.drone = with lib; {
+    enable = mkEnableOption "Drone CI";
+    vhostName = mkOption {
+      type = types.str;
+      example = "drone.fcuny.net";
+      description = "Name for the virtual host";
+    };
+    runners = mkOption {
+      type = with types; listOf (enum [ "exec" "docker" ]);
+      default = [ ];
+      example = [ "exec" "docker" ];
+      description = "Types of runners to enable";
+    };
+    admin = mkOption {
+      type = types.str;
+      default = "ambroisie";
+      example = "admin";
+      description = "Name of the admin user";
+    };
+    port = mkOption {
+      type = types.port;
+      default = 3030;
+      example = 8080;
+      description = "Internal port of the Drone UI";
+    };
+    sharedSecretFile = mkOption {
+      type = types.str;
+      example = "/run/secrets/drone-rpc.env";
+      description = "Shared RPC secret to inject into server and runners";
+    };
+  };
+}
diff --git a/modules/services/drone/runner-docker/default.nix b/modules/services/drone/runner-docker/default.nix
new file mode 100644
index 0000000..d701c18
--- /dev/null
+++ b/modules/services/drone/runner-docker/default.nix
@@ -0,0 +1,39 @@
+{ config, lib, pkgs, ... }:
+let
+  cfg = config.my.services.drone;
+  hasRunner = (name: builtins.elem name cfg.runners);
+  dockerPkg = pkgs.drone-runner-docker;
+in {
+  config = lib.mkIf (cfg.enable && hasRunner "docker") {
+    systemd.services.drone-runner-docker = {
+      wantedBy = [ "multi-user.target" ];
+      after = [ "docker.socket" ]; # Needs the socket to be available
+      # might break deployment
+      restartIfChanged = false;
+      confinement.enable = true;
+      serviceConfig = {
+        Environment = [
+          "DRONE_SERVER_HOST=${cfg.vhostName}"
+          "DRONE_SERVER_PROTO=https"
+          "DRONE_RUNNER_CAPACITY=10"
+          "CLIENT_DRONE_RPC_HOST=127.0.0.1:${toString cfg.port}"
+        ];
+        BindPaths = [ "/var/run/docker.sock" ];
+        EnvironmentFile = [ cfg.sharedSecretFile ];
+        ExecStart = "${dockerPkg}/bin/drone-runner-docker";
+        User = "drone-runner-docker";
+        Group = "drone-runner-docker";
+      };
+    };
+
+    # Make sure it is activated in that case
+    virtualisation.docker.enable = true;
+
+    users.users.drone-runner-docker = {
+      isSystemUser = true;
+      group = "drone-runner-docker";
+      extraGroups = [ "docker" ]; # Give access to the daemon
+    };
+    users.groups.drone-runner-docker = { };
+  };
+}
diff --git a/modules/services/drone/runner-exec/default.nix b/modules/services/drone/runner-exec/default.nix
new file mode 100644
index 0000000..f2b2a0f
--- /dev/null
+++ b/modules/services/drone/runner-exec/default.nix
@@ -0,0 +1,54 @@
+{ config, lib, pkgs, ... }:
+let
+  cfg = config.my.services.drone;
+  hasRunner = (name: builtins.elem name cfg.runners);
+  execPkg = pkgs.drone-runner-exec;
+in {
+  config = lib.mkIf (cfg.enable && hasRunner "exec") {
+    systemd.services.drone-runner-exec = {
+      wantedBy = [ "multi-user.target" ];
+      # might break deployment
+      restartIfChanged = false;
+      confinement.enable = true;
+      confinement.packages = with pkgs; [ git gnutar bash nixUnstable gzip ];
+      path = with pkgs; [ git gnutar bash nixUnstable gzip ];
+      serviceConfig = {
+        Environment = [
+          "DRONE_SERVER_HOST=${cfg.vhostName}"
+          "DRONE_SERVER_PROTO=https"
+          "DRONE_RUNNER_CAPACITY=10"
+          "CLIENT_DRONE_RPC_HOST=127.0.0.1:${toString cfg.port}"
+          "NIX_REMOTE=daemon"
+          "PAGER=cat"
+        ];
+        BindPaths = [ "/nix/var/nix/daemon-socket/socket" "/run/nscd/socket" ];
+        BindReadOnlyPaths = [
+          "/etc/resolv.conf:/etc/resolv.conf"
+          "/etc/resolvconf.conf:/etc/resolvconf.conf"
+          "/etc/passwd:/etc/passwd"
+          "/etc/group:/etc/group"
+          "/nix/var/nix/profiles/system/etc/nix:/etc/nix"
+          "${
+            config.environment.etc."ssl/certs/ca-certificates.crt".source
+          }:/etc/ssl/certs/ca-certificates.crt"
+          "${
+            config.environment.etc."ssh/ssh_known_hosts".source
+          }:/etc/ssh/ssh_known_hosts"
+          "/etc/machine-id"
+          # channels are dynamic paths in the nix store, therefore we need to bind mount the whole thing
+          "/nix/"
+        ];
+        EnvironmentFile = [ cfg.sharedSecretFile ];
+        ExecStart = "${execPkg}/bin/drone-runner-exec";
+        User = "drone-runner-exec";
+        Group = "drone-runner-exec";
+      };
+    };
+
+    users.users.drone-runner-exec = {
+      isSystemUser = true;
+      group = "drone-runner-exec";
+    };
+    users.groups.drone-runner-exec = { };
+  };
+}
diff --git a/modules/services/drone/server/default.nix b/modules/services/drone/server/default.nix
new file mode 100644
index 0000000..199aa47
--- /dev/null
+++ b/modules/services/drone/server/default.nix
@@ -0,0 +1,45 @@
+{ config, lib, pkgs, ... }:
+let cfg = config.my.services.drone;
+in {
+  config = lib.mkIf cfg.enable {
+    systemd.services.drone-server = {
+      wantedBy = [ "multi-user.target" ];
+      serviceConfig = {
+        EnvironmentFile = [ cfg.sharedSecretFile ];
+        Environment = [
+          "DRONE_SERVER_HOST=${cfg.vhostName}"
+          "DRONE_SERVER_PROTO=https"
+          "DRONE_SERVER_PORT=:${toString cfg.port}"
+          "DRONE_USER_CREATE=username:${cfg.admin},admin:true"
+          "DRONE_JSONNET_ENABLED=true"
+          "DRONE_STARLARK_ENABLED=true"
+        ];
+        ExecStart = "${pkgs.drone}/bin/drone-server";
+        User = "drone";
+        Group = "drone";
+      };
+    };
+
+    users.users.drone = {
+      isSystemUser = true;
+      createHome = true;
+      group = "drone";
+    };
+
+    users.groups.drone = { };
+
+    services.nginx.virtualHosts."${cfg.vhostName}" = {
+      forceSSL = true;
+      useACMEHost = cfg.vhostName;
+      locations."/" = {
+        proxyPass = "http://127.0.0.1:${toString cfg.port}";
+        proxyWebsockets = true;
+      };
+    };
+
+    security.acme.certs."${cfg.vhostName}" = {
+      dnsProvider = "gcloud";
+      credentialsFile = secrets."acme/credentials".path;
+    };
+  };
+}