summary refs log tree commit diff
path: root/nixos/modules/services/continuous-integration/woodpecker/agents.nix
blob: 3b883c72ff07fd69f0e561e18a27b4548f009505 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{ config
, lib
, pkgs
, ...
}:

let
  cfg = config.services.woodpecker-agents;

  agentModule = lib.types.submodule {
    options = {
      enable = lib.mkEnableOption (lib.mdDoc "this Woodpecker-Agent. Agents execute tasks generated by a Server, every install will need one server and at least one agent");

      package = lib.mkPackageOptionMD pkgs "woodpecker-agent" { };

      environment = lib.mkOption {
        default = { };
        type = lib.types.attrsOf lib.types.str;
        example = lib.literalExpression ''
          {
            WOODPECKER_SERVER = "localhost:9000";
            WOODPECKER_BACKEND = "docker";
            DOCKER_HOST = "unix:///run/podman/podman.sock";
          }
        '';
        description = lib.mdDoc "woodpecker-agent config environment variables, for other options read the [documentation](https://woodpecker-ci.org/docs/administration/agent-config)";
      };

      extraGroups = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
        example = [ "podman" ];
        description = lib.mdDoc ''
          Additional groups for the systemd service.
        '';
      };

      path = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [ ];
        example = [ "" ];
        description = lib.mdDoc ''
          Additional packages that should be added to the agent's `PATH`.
          Mostly useful for the `local` backend.
        '';
      };

      environmentFile = lib.mkOption {
        type = lib.types.listOf lib.types.path;
        default = [ ];
        example = [ "/var/secrets/woodpecker-agent.env" ];
        description = lib.mdDoc ''
          File to load environment variables
          from. This is helpful for specifying secrets.
          Example content of environmentFile:
          ```
          WOODPECKER_AGENT_SECRET=your-shared-secret-goes-here
          ```
        '';
      };
    };
  };

  mkAgentService = name: agentCfg: {
    name = "woodpecker-agent-${name}";
    value = {
      description = "Woodpecker-Agent Service - ${name}";
      wantedBy = [ "multi-user.target" ];
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];
      serviceConfig = {
        DynamicUser = true;
        SupplementaryGroups = agentCfg.extraGroups;
        EnvironmentFile = agentCfg.environmentFile;
        ExecStart = lib.getExe agentCfg.package;
        Restart = "on-failure";
        RestartSec = 15;
        CapabilityBoundingSet = "";
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        PrivateTmp = true;
        PrivateDevices = true;
        PrivateUsers = true;
        ProtectHostname = true;
        ProtectClock = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectKernelLogs = true;
        ProtectControlGroups = true;
        RestrictAddressFamilies = [ "AF_UNIX AF_INET AF_INET6" ];
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        PrivateMounts = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = "~@clock @privileged @cpu-emulation @debug @keyring @module @mount @obsolete @raw-io @reboot @setuid @swap";
        BindReadOnlyPaths = [
          "-/etc/resolv.conf"
          "-/etc/nsswitch.conf"
          "-/etc/ssl/certs"
          "-/etc/static/ssl/certs"
          "-/etc/hosts"
          "-/etc/localtime"
        ];
      };
      inherit (agentCfg) environment path;
    };
  };
in
{
  meta.maintainers = with lib.maintainers; [ janik ambroisie ];

  options = {
    services.woodpecker-agents = {
      agents = lib.mkOption {
        default = { };
        type = lib.types.attrsOf agentModule;
        example = lib.literalExpression ''
          {
            podman = {
              environment = {
                WOODPECKER_SERVER = "localhost:9000";
                WOODPECKER_BACKEND = "docker";
                DOCKER_HOST = "unix:///run/podman/podman.sock";
              };

              extraGroups = [ "podman" ];

              environmentFile = [ "/run/secrets/woodpecker/agent-secret.txt" ];
            };

            exec = {
              environment = {
                WOODPECKER_SERVER = "localhost:9000";
                WOODPECKER_BACKEND = "local";
              };

              environmentFile = [ "/run/secrets/woodpecker/agent-secret.txt" ];

              path = [
                # Needed to clone repos
                git
                git-lfs
                woodpecker-plugin-git
                # Used by the runner as the default shell
                bash
                # Most likely to be used in pipeline definitions
                coreutils
              ];
            };
          }
        '';
        description = lib.mdDoc "woodpecker-agents configurations";
      };
    };
  };

  config = {
    systemd.services =
      let
        mkServices = lib.mapAttrs' mkAgentService;
        enabledAgents = lib.filterAttrs (_: agent: agent.enable) cfg.agents;
      in
      mkServices enabledAgents;
  };
}