summary refs log tree commit diff
path: root/nixos/modules/services/databases/firebird.nix
blob: 4e3130bea22f8cba22a939b6b9e16c03051e6a2d (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
{ config, lib, pkgs, ... }:

# TODO: This may file may need additional review, eg which configuartions to
# expose to the user.
#
# I only used it to access some simple databases.

# test:
# isql, then type the following commands:
# CREATE DATABASE '/var/db/firebird/data/test.fdb' USER 'SYSDBA' PASSWORD 'masterkey';
# CONNECT '/var/db/firebird/data/test.fdb' USER 'SYSDBA' PASSWORD 'masterkey';
# CREATE TABLE test ( text varchar(100) );
# DROP DATABASE;
#
# Be careful, virtuoso-opensource also provides a different isql command !

# There are at least two ways to run firebird. superserver has been choosen
# however there are no strong reasons to prefer this or the other one AFAIK
# Eg superserver is said to be most efficiently using resources according to
# http://www.firebirdsql.org/manual/qsg25-classic-or-super.html

with lib;

let

  cfg = config.services.firebird;

  firebird = cfg.package;

  dataDir = "${cfg.baseDir}/data";
  systemDir = "${cfg.baseDir}/system";

in

{

  ###### interface

  options = {

    services.firebird = {

      enable = mkEnableOption "the Firebird super server";

      package = mkOption {
        default = pkgs.firebird;
        defaultText = literalExpression "pkgs.firebird";
        type = types.package;
        example = literalExpression "pkgs.firebird_3";
        description = ''
          Which Firebird package to be installed: <code>pkgs.firebird_3</code>
          For SuperServer use override: <code>pkgs.firebird_3.override { superServer = true; };</code>
        '';
      };

      port = mkOption {
        default = 3050;
        type = types.port;
        description = ''
          Port Firebird uses.
        '';
      };

      user = mkOption {
        default = "firebird";
        type = types.str;
        description = ''
          User account under which firebird runs.
        '';
      };

      baseDir = mkOption {
        default = "/var/lib/firebird";
        type = types.str;
        description = ''
          Location containing data/ and system/ directories.
          data/ stores the databases, system/ stores the password database security2.fdb.
        '';
      };

    };

  };


  ###### implementation

  config = mkIf config.services.firebird.enable {

    environment.systemPackages = [cfg.package];

    systemd.tmpfiles.rules = [
      "d '${dataDir}' 0700 ${cfg.user} - - -"
      "d '${systemDir}' 0700 ${cfg.user} - - -"
    ];

    systemd.services.firebird =
      { description = "Firebird Super-Server";

        wantedBy = [ "multi-user.target" ];

        # TODO: moving security2.fdb into the data directory works, maybe there
        # is a better way
        preStart =
          ''
            if ! test -e "${systemDir}/security2.fdb"; then
                cp ${firebird}/security2.fdb "${systemDir}"
            fi

            if ! test -e "${systemDir}/security3.fdb"; then
                cp ${firebird}/security3.fdb "${systemDir}"
            fi

            if ! test -e "${systemDir}/security4.fdb"; then
                cp ${firebird}/security4.fdb "${systemDir}"
            fi

            chmod -R 700         "${dataDir}" "${systemDir}" /var/log/firebird
          '';

        serviceConfig.User = cfg.user;
        serviceConfig.LogsDirectory = "firebird";
        serviceConfig.LogsDirectoryMode = "0700";
        serviceConfig.ExecStart = "${firebird}/bin/fbserver -d";

        # TODO think about shutdown
      };

    environment.etc."firebird/firebird.msg".source = "${firebird}/firebird.msg";

    # think about this again - and eventually make it an option
    environment.etc."firebird/firebird.conf".text = ''
      # RootDirectory = Restrict ${dataDir}
      DatabaseAccess = Restrict ${dataDir}
      ExternalFileAccess = Restrict ${dataDir}
      # what is this? is None allowed?
      UdfAccess = None
      # "Native" =  traditional interbase/firebird, "mixed" is windows only
      Authentication = Native

      # defaults to -1 on non Win32
      #MaxUnflushedWrites = 100
      #MaxUnflushedWriteTime = 100

      # show trace if trouble occurs (does this require debug build?)
      # BugcheckAbort = 0
      # ConnectionTimeout = 180

      #RemoteServiceName = gds_db
      RemoteServicePort = ${cfg.port}

      # randomly choose port for server Event Notification
      #RemoteAuxPort = 0
      # rsetrict connections to a network card:
      #RemoteBindAddress =
      # there are some additional settings which should be reviewed
    '';

    users.users.firebird = {
      description = "Firebird server user";
      group = "firebird";
      uid = config.ids.uids.firebird;
    };

    users.groups.firebird.gid = config.ids.gids.firebird;

  };
}