summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/frab.nix
blob: fb95e024817cfea8cca8b60c65f03db0f51f4a29 (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
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.frab;

  package = pkgs.frab;

  databaseConfig = builtins.toJSON { production = cfg.database; };

  frabEnv = {
    RAILS_ENV = "production";
    RACK_ENV = "production";
    SECRET_KEY_BASE = cfg.secretKeyBase;
    FRAB_HOST = cfg.host;
    FRAB_PROTOCOL = cfg.protocol;
    FROM_EMAIL = cfg.fromEmail;
    RAILS_SERVE_STATIC_FILES = "1";
  } // cfg.extraEnvironment;

  frab-rake = pkgs.stdenv.mkDerivation rec {
    name = "frab-rake";
    buildInputs = [ package.env pkgs.makeWrapper ];
    phases = "installPhase fixupPhase";
    installPhase = ''
      mkdir -p $out/bin
      makeWrapper ${package.env}/bin/bundle $out/bin/frab-bundle \
          ${concatStrings (mapAttrsToList (name: value: "--set ${name} '${value}' ") frabEnv)} \
          --set PATH '${lib.makeBinPath (with pkgs; [ nodejs file imagemagick ])}:$PATH' \
          --set RAKEOPT '-f ${package}/share/frab/Rakefile' \
          --run 'cd ${package}/share/frab'
      makeWrapper $out/bin/frab-bundle $out/bin/frab-rake \
          --add-flags "exec rake"
     '';
  };

in

{
  options = {
    services.frab = {
      enable = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Enable the frab service.
        '';
      };

      host = mkOption {
        type = types.str;
        example = "frab.example.com";
        description = ''
          Hostname under which this frab instance can be reached.
        '';
      };

      protocol = mkOption {
        type = types.str;
        default = "https";
        example = "http";
        description = ''
          Either http or https, depending on how your Frab instance
          will be exposed to the public.
        '';
      };

      fromEmail = mkOption {
        type = types.str;
        default = "frab@localhost";
        description = ''
          Email address used by frab.
        '';
      };

      listenAddress = mkOption {
        type = types.str;
        default = "localhost";
        description = ''
          Address or hostname frab should listen on.
        '';
      };

      listenPort = mkOption {
        type = types.int;
        default = 3000;
        description = ''
          Port frab should listen on.
        '';
      };

      statePath = mkOption {
        type = types.str;
        default = "/var/lib/frab";
        description = ''
          Directory where frab keeps its state.
        '';
      };

      user = mkOption {
        type = types.str;
        default = "frab";
        description = ''
          User to run frab.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "frab";
        description = ''
          Group to run frab.
        '';
      };

      secretKeyBase = mkOption {
        type = types.str;
        description = ''
          Your secret key is used for verifying the integrity of signed cookies.
          If you change this key, all old signed cookies will become invalid!

          Make sure the secret is at least 30 characters and all random,
          no regular words or you'll be exposed to dictionary attacks.
        '';
      };

      database = mkOption {
        type = types.attrs;
        default = {
          adapter = "sqlite3";
          database = "/var/lib/frab/db.sqlite3";
          pool = 5;
          timeout = 5000;
        };
        example = {
          adapter = "postgresql";
          database = "frab";
          host = "localhost";
          username = "frabuser";
          password = "supersecret";
          encoding = "utf8";
          pool = 5;
        };
        description = ''
          Rails database configuration for Frab as Nix attribute set.
        '';
      };

      extraEnvironment = mkOption {
        type = types.attrs;
        default = {};
        example = {
          FRAB_CURRENCY_UNIT = "€";
          FRAB_CURRENCY_FORMAT = "%n%u";
          EXCEPTION_EMAIL = "frab-owner@example.com";
          SMTP_ADDRESS = "localhost";
          SMTP_PORT = "587";
          SMTP_DOMAIN = "localdomain";
          SMTP_USER_NAME = "root";
          SMTP_PASSWORD = "toor";
          SMTP_AUTHENTICATION = "1";
          SMTP_NOTLS = "1";
        };
        description = ''
          Additional environment variables to set for frab for further
          configuration. See the frab documentation for more information.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    environment.systemPackages = [ frab-rake ];

    users.users = [
      { name = cfg.user;
        group = cfg.group;
        home = "${cfg.statePath}";
      }
    ];

    users.groups = [ { name = cfg.group; } ];

    systemd.services.frab = {
      after = [ "network.target" "gitlab.service" ];
      wantedBy = [ "multi-user.target" ];
      environment = frabEnv;

      preStart = ''
        mkdir -p ${cfg.statePath}/system/attachments
        chown ${cfg.user}:${cfg.group} -R ${cfg.statePath}

        mkdir /run/frab -p
        ln -sf ${pkgs.writeText "frab-database.yml" databaseConfig} /run/frab/database.yml
        ln -sf ${cfg.statePath}/system /run/frab/system

        if ! test -e "${cfg.statePath}/db-setup-done"; then
          ${frab-rake}/bin/frab-rake db:setup
          touch ${cfg.statePath}/db-setup-done
        else
          ${frab-rake}/bin/frab-rake db:migrate
        fi
      '';

      serviceConfig = {
        PermissionsStartOnly = true;
        PrivateTmp = true;
        PrivateDevices = true;
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        TimeoutSec = "300s";
        Restart = "on-failure";
        RestartSec = "10s";
        WorkingDirectory = "${package}/share/frab";
        ExecStart = "${frab-rake}/bin/frab-bundle exec rails server " +
          "--binding=${cfg.listenAddress} --port=${toString cfg.listenPort}";
      };
    };

  };
}