1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.fider;
9 fiderCmd = lib.getExe cfg.package;
10in
11{
12 options = {
13
14 services.fider = {
15 enable = lib.mkEnableOption "the Fider server";
16 package = lib.mkPackageOption pkgs "fider" { };
17
18 dataDir = lib.mkOption {
19 type = lib.types.str;
20 default = "/var/lib/fider";
21 description = "Default data folder for Fider.";
22 example = "/mnt/fider";
23 };
24
25 database = {
26 url = lib.mkOption {
27 type = lib.types.str;
28 default = "local";
29 description = ''
30 URI to use for the main PostgreSQL database. If this needs to include
31 credentials that shouldn't be world-readable in the Nix store, set an
32 environment file on the systemd service and override the
33 `DATABASE_URL` entry. Pass the string
34 `local` to setup a database on the local server.
35 '';
36 };
37 };
38
39 environment = lib.mkOption {
40 type = lib.types.attrsOf lib.types.str;
41 default = { };
42 example = {
43 PORT = "31213";
44 BASE_URL = "https://fider.example.com";
45 EMAIL = "smtp";
46 EMAIL_NOREPLY = "fider@example.com";
47 EMAIL_SMTP_USERNAME = "fider@example.com";
48 EMAIL_SMTP_HOST = "mail.example.com";
49 EMAIL_SMTP_PORT = "587";
50 BLOB_STORAGE = "fs";
51 };
52 description = ''
53 Environment variables to set for the service. Secrets should be
54 specified using {option}`environmentFiles`.
55 Refer to <https://github.com/getfider/fider/blob/stable/.example.env>
56 and <https://github.com/getfider/fider/blob/stable/app/pkg/env/env.go>
57 for available options.
58 '';
59 };
60
61 environmentFiles = lib.mkOption {
62 type = lib.types.listOf lib.types.path;
63 default = [ ];
64 example = "/run/secrets/fider.env";
65 description = ''
66 Files to load environment variables from. Loaded variables override
67 values set in {option}`environment`.
68 '';
69 };
70 };
71 };
72
73 config = lib.mkIf cfg.enable {
74 services.postgresql = lib.mkIf (cfg.database.url == "local") {
75 enable = true;
76 ensureUsers = [
77 {
78 name = "fider";
79 ensureDBOwnership = true;
80 }
81 ];
82 ensureDatabases = [ "fider" ];
83 };
84
85 systemd.services.fider = {
86 description = "Fider server";
87 wantedBy = [ "multi-user.target" ];
88 after = [
89 "network.target"
90 ] ++ lib.optionals (cfg.database.url == "local") [ "postgresql.service" ];
91 requires = lib.optionals (cfg.database.url == "local") [ "postgresql.service" ];
92 environment =
93 let
94 localPostgresqlUrl = "postgres:///fider?host=/run/postgresql";
95 in
96 {
97 DATABASE_URL = if (cfg.database.url == "local") then localPostgresqlUrl else cfg.database.url;
98 BLOB_STORAGE_FS_PATH = "${cfg.dataDir}";
99 }
100 // cfg.environment;
101 serviceConfig = {
102 ExecStartPre = "${fiderCmd} migrate";
103 ExecStart = fiderCmd;
104 StateDirectory = "fider";
105 DynamicUser = true;
106 PrivateTmp = "yes";
107 Restart = "on-failure";
108 RuntimeDirectory = "fider";
109 RuntimeDirectoryPreserve = true;
110 CacheDirectory = "fider";
111 WorkingDirectory = "${cfg.package}";
112 EnvironmentFile = cfg.environmentFiles;
113 };
114 };
115 };
116
117 meta = {
118 maintainers = with lib.maintainers; [
119 drupol
120 niklaskorz
121 ];
122 # doc = ./fider.md;
123 };
124}