summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/web-apps/windmill.nix
blob: c94b706d2ed1c578f07605b5c9f4145acafdc813 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.services.windmill;
in
{
  options.services.windmill = {
    enable = lib.mkEnableOption "windmill service";

    package = lib.mkPackageOption pkgs "windmill" { };

    serverPort = lib.mkOption {
      type = lib.types.port;
      default = 8001;
      description = "Port the windmill server listens on.";
    };

    lspPort = lib.mkOption {
      type = lib.types.port;
      default = 3001;
      description = "Port the windmill lsp listens on.";
    };

    database = {
      name = lib.mkOption {
        type = lib.types.str;
        # the simplest database setup is to have the database named like the user.
        default = "windmill";
        description = "Database name.";
      };

      user = lib.mkOption {
        type = lib.types.str;
        # the simplest database setup is to have the database user like the name.
        default = "windmill";
        description = "Database user.";
      };

      url = lib.mkOption {
        type = lib.types.str;
        default = "postgres://${config.services.windmill.database.name}?host=/var/run/postgresql";
        defaultText = lib.literalExpression ''
          "postgres://\$\{config.services.windmill.database.name}?host=/var/run/postgresql";
        '';
        description = "Database url. Note that any secret here would be world-readable. Use `services.windmill.database.urlPath` unstead to include secrets in the url.";
      };

      urlPath = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        description = ''
          Path to the file containing the database url windmill should connect to. This is not deducted from database user and name as it might contain a secret
        '';
        default = null;
        example = "config.age.secrets.DATABASE_URL_FILE.path";
      };

      createLocally = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = "Whether to create a local database automatically.";
      };
    };

    baseUrl = lib.mkOption {
      type = lib.types.str;
      default = "https://localhost:${toString config.services.windmill.serverPort}";
      defaultText = lib.literalExpression ''
        "https://localhost:\$\{toString config.services.windmill.serverPort}";
      '';
      description = ''
        The base url that windmill will be served on.
      '';
      example = "https://windmill.example.com";
    };

    logLevel = lib.mkOption {
      type = lib.types.enum [
        "error"
        "warn"
        "info"
        "debug"
        "trace"
      ];
      default = "info";
      description = "Log level";
    };
  };

  config = lib.mkIf cfg.enable {

    assertions = [
      {
        assertion = cfg.database.createLocally -> cfg.database.name == cfg.database.user;
        message = ''
          Automatically provisioning the windmill database requires both database name and database user to be equal. '${cfg.database.name}' != '${cfg.database.user}'
          To fix this problem, assign the same value to both options services.windmill.database.{name,user}.
        '';
      }
    ];

    services.postgresql = lib.optionalAttrs (cfg.database.createLocally) {
      enable = lib.mkDefault true;

      ensureDatabases = [ cfg.database.name ];
      ensureUsers = [
        {
          name = cfg.database.user;
          ensureDBOwnership = true;
        }
      ];
    };

    systemd.targets.windmill = {
      description = "Windmill";
      wantedBy = [ "multi-user.target" ];
      requires =
        [ ]
        ++ (lib.optionals config.systemd.services.windmill-server.enable [ "windmill-server.service" ])
        ++ (lib.optionals config.systemd.services.windmill-worker.enable [ "windmill-worker.service" ])
        ++ (lib.optionals config.systemd.services.windmill-worker-native.enable [
          "windmill-worker-native.service"
        ]);
    };

    systemd.services =
      let
        useUrlPath = (cfg.database.urlPath != null);
        serviceConfig = {
          DynamicUser = true;
          # using the same user to simplify db connection
          User = cfg.database.user;
          ExecStart = lib.getExe cfg.package;
          Restart = "always";
        }
        // lib.optionalAttrs useUrlPath {
          LoadCredential = [
            "DATABASE_URL_FILE:${cfg.database.urlPath}"
          ];
        };
        db_url_envs =
          lib.optionalAttrs useUrlPath {
            DATABASE_URL_FILE = "%d/DATABASE_URL_FILE";
          }
          // lib.optionalAttrs (!useUrlPath) {
            DATABASE_URL = cfg.database.url;
          };
      in
      {
        windmill-initdb = lib.mkIf cfg.database.createLocally {
          description = "Windmill database setup";
          requires = [ "postgresql.target" ];
          after = [ "postgresql.target" ];
          requiredBy =
            [ ]
            ++ (lib.optionals config.systemd.services.windmill-server.enable [ "windmill-server.service" ])
            ++ (lib.optionals config.systemd.services.windmill-worker.enable [ "windmill-worker.service" ])
            ++ (lib.optionals config.systemd.services.windmill-worker-native.enable [
              "windmill-worker-native.service"
            ]);
          before =
            [ ]
            ++ (lib.optionals config.systemd.services.windmill-server.enable [ "windmill-server.service" ])
            ++ (lib.optionals config.systemd.services.windmill-worker.enable [ "windmill-worker.service" ])
            ++ (lib.optionals config.systemd.services.windmill-worker-native.enable [
              "windmill-worker-native.service"
            ]);

          path = [ config.services.postgresql.package ];
          # coming from https://github.com/windmill-labs/windmill/blob/main/init-db-as-superuser.sql
          # modified to not grant privileges on all tables
          # create role windmill_user and windmill_admin only if they don't exist
          script = ''
            psql -tA <<"EOF"
              DO $$
              BEGIN
                  IF NOT EXISTS (
                      SELECT FROM pg_catalog.pg_roles
                      WHERE rolname = 'windmill_user'
                  ) THEN
                      CREATE ROLE windmill_user;
                      GRANT ALL PRIVILEGES ON DATABASE ${cfg.database.name} TO windmill_user;
                  ELSE
                    RAISE NOTICE 'Role "windmill_user" already exists. Skipping.';
                  END IF;
                  IF NOT EXISTS (
                      SELECT FROM pg_catalog.pg_roles
                      WHERE rolname = 'windmill_admin'
                  ) THEN
                    CREATE ROLE windmill_admin WITH BYPASSRLS;
                    GRANT windmill_user TO windmill_admin;
                  ELSE
                    RAISE NOTICE 'Role "windmill_admin" already exists. Skipping.';
                  END IF;
                  GRANT windmill_admin TO ${cfg.database.user};
              END
              $$;
            EOF
          '';

          serviceConfig = {
            Type = "oneshot";
            RemainAfterExit = true;
            # Superuser because of required permission CREATE ROLE
            User = "postgres";

            ProtectSystem = "strict";
            ProtectHome = "read-only";
          };
        };

        windmill-server = {
          description = "Windmill server";
          after = [ "network.target" ];
          partOf = [ "windmill.target" ];

          serviceConfig = serviceConfig // {
            StateDirectory = "windmill";
          };

          environment = {
            PORT = toString cfg.serverPort;
            WM_BASE_URL = cfg.baseUrl;
            RUST_LOG = cfg.logLevel;
            MODE = "server";
          }
          // db_url_envs;
        };

        windmill-worker = {
          description = "Windmill worker";
          after = [ "network.target" ];
          partOf = [ "windmill.target" ];

          serviceConfig = serviceConfig // {
            StateDirectory = "windmill-worker";
          };

          environment = {
            WM_BASE_URL = cfg.baseUrl;
            RUST_LOG = cfg.logLevel;
            MODE = "worker";
            WORKER_GROUP = "default";
            KEEP_JOB_DIR = "false";
          }
          // db_url_envs;
        };

        windmill-worker-native = {
          description = "Windmill worker native";
          after = [ "network.target" ];
          partOf = [ "windmill.target" ];

          serviceConfig = serviceConfig // {
            StateDirectory = "windmill-worker-native";
          };

          environment = {
            WM_BASE_URL = cfg.baseUrl;
            RUST_LOG = cfg.logLevel;
            MODE = "worker";
            WORKER_GROUP = "native";
          }
          // db_url_envs;
        };
      };
  };
}