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

# TODO: this file needs some additional work - at least you can connect to
# firebird ..
# Example how to connect:
# isql /var/db/firebird/data/your-db.fdb -u sysdba -p <default password>

# 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 pkgs.lib;

let

  cfg = config.services.firebird;

  firebird = cfg.package;

  pidFile = "${cfg.pidDir}/firebirdd.pid";

in

{

  ###### interface

  options = {

    services.firebird = {

      enable = mkOption {
        default = false;
        description = "
          Whether to enable the firebird super server.
        ";
      };

      package = mkOption {
        default = pkgs.firebirdSuper;
        /*
          Example: <code>package = pkgs.firebirdSuper.override { icu =
            pkgs.icu; };</code> which is not recommended for compatibility
            reasons. See comments at the firebirdSuper derivation
        */

        description = "
          Which firebird derivation to use.
        ";
      };

      port = mkOption {
        default = "3050";
        description = "Port of Firebird.";
      };

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

      dataDir = mkOption {
        default = "/var/db/firebird/data"; # ubuntu is using /var/lib/firebird/2.1/data/.. ?
        description = "Location where firebird databases are stored.";
      };

      pidDir = mkOption {
        default = "/run/firebird";
        description = "Location of the file which stores the PID of the firebird server.";
      };

    };

  };


  ###### implementation

  config = mkIf config.services.firebird.enable {

    users.extraUsers.firebird.description =  "Firebird server user";

    environment.systemPackages = [firebird];

    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 =
          ''
            secureDir="${cfg.dataDir}/../system"

            mkdir -m 0700 -p \
              "${cfg.dataDir}" \
              "${cfg.pidDir}" \
              /var/log/firebird \
              "$secureDir"

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

            chown -R ${cfg.user} "${cfg.pidDir}" "${cfg.dataDir}" "$secureDir" /var/log/firebird
            chmod -R 700 "${cfg.pidDir}" "${cfg.dataDir}" "$secureDir" /var/log/firebird
          '';

        serviceConfig.PermissionsStartOnly = true; # preStart must be run as root
        serviceConfig.User = cfg.user;
        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 ${cfg.dataDir}
      DatabaseAccess = Restrict ${cfg.dataDir}
      ExternalFileAccess = Restrict ${cfg.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 more settings ..
    '';
    };

}