forked from nixcloud/nixcloud-webservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
129 lines (112 loc) · 3.31 KB
/
default.nix
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
{ config, lib, ... }:
let
inherit (lib) mkOption types;
inherit (config) defaultDatabaseType;
dbSpec = {
postgresql = rec {
socketPath = config.runtimeDir;
phpHostname = socketPath;
};
mysql = rec {
socketPath = "${config.runtimeDir}/.mysql.sock";
phpHostname = ":${socketPath}";
};
};
dbTypes = lib.attrNames dbSpec;
dbSubmodule = { name, config, ... }: {
options.type = mkOption {
type = types.enum dbTypes;
default = defaultDatabaseType;
example = "mysql";
description = ''
The DBMS type to use for this database.
<note><para>
The default is dependant on the <option>defaultDatabaseType</option>.
</para></note>
'';
};
options.socketPath = mkOption {
type = types.str;
readOnly = true;
default = dbSpec.${config.type}.socketPath;
description = ''
The path to the database socket file.
'';
};
options.phpHostname = mkOption {
type = types.str;
readOnly = true;
default = dbSpec.${config.type}.phpHostname;
description = ''
The hostname argument suitable for PHP.
<note><para>This maps to the socket file rather than a real host
name.</para></note>
'';
};
options.name = mkOption {
type = types.str;
default = name;
description = ''
The name of the database or schema to create.
'';
};
options.user = mkOption {
type = types.str;
default = name;
description = ''
The user name which should be the owner of the database.
'';
};
options.postCreate = mkOption {
type = types.lines;
default = "";
description = ''
The commands to run shortly after the database has been created.
<note><para>
All of these commands are run in a context, where a special command
called <command>sqlsh</command> is available, which in turn reads SQL
commands from standard input and executes them on the current database.
</para></note>
'';
};
};
in {
imports = map (dbtype: ./. + "/${dbtype}.nix") dbTypes;
options.database = mkOption {
type = types.attrsOf (types.submodule dbSubmodule);
default = {};
description = ''
Map database names to their corresponding options.
'';
};
options.defaultDatabaseType = mkOption {
type = types.enum dbTypes;
default = "postgresql";
example = "mysql";
description = ''
The default type to use for all the databases where no explicit
<option>type</option> has been defined.
'';
};
config = lib.mkIf config.enable {
systemd.targets = {
db-server = {
description = "Database Management Systems For ${config.uniqueName}";
after = [ "network.target" ];
instance.requiredBy = [ "database.target" ];
};
database = {
description = "Databases For ${config.uniqueName}";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
};
} // lib.mapAttrs' (lib.const (dbcfg: {
name = "database-${dbcfg.name}";
value = {
description = "Create Database ${dbcfg.name}";
instance.requiredBy = [ "database.target" ];
instance.after = [ "db-server.target" ];
};
})) config.database;
};
}