@@ -0,0 +1,205 @@
1+{ lib, pkgs, config, ... } :
2+
3+with lib;
4+
5+let
6+ cfg = config.services.postage;
7+
8+ confFile = pkgs.writeTextFile {
9+ name = "postage.conf";
10+ text = ''
11+ connection_file = ${postageConnectionsFile}
12+
13+ allow_custom_connections = ${builtins.toJSON cfg.allowCustomConnections}
14+
15+ postage_port = ${toString cfg.port}
16+
17+ super_only = ${builtins.toJSON cfg.superOnly}
18+
19+ ${optionalString (!isNull cfg.loginGroup) "login_group = ${cfg.loginGroup}"}
20+
21+ login_timeout = ${toString cfg.loginTimeout}
22+
23+ web_root = ${cfg.package}/etc/postage/web_root
24+
25+ data_root = ${cfg.dataRoot}
26+
27+ ${optionalString (!isNull cfg.tls) ''
28+ tls_cert = ${cfg.tls.cert}
29+ tls_key = ${cfg.tls.key}
30+ ''}
31+
32+ log_level = ${cfg.logLevel}
33+ '';
34+ };
35+
36+ postageConnectionsFile = pkgs.writeTextFile {
37+ name = "postage-connections.conf";
38+ text = concatStringsSep "\n"
39+ (mapAttrsToList (name : conn : "${name}: ${conn}") cfg.connections);
40+ };
41+
42+ postage = "postage";
43+in {
44+
45+ options.services.postage = {
46+ enable = mkEnableOption "PostgreSQL Administration for the web";
47+
48+ package = mkOption {
49+ type = types.package;
50+ default = pkgs.postage;
51+ defaultText = "pkgs.postage";
52+ description = ''
53+ The postage package to use.
54+ '';
55+ };
56+
57+ connections = mkOption {
58+ type = types.attrsOf types.str;
59+ default = {};
60+ example = {
61+ "nuc-server" = "hostaddr=192.168.0.100 port=5432 dbname=postgres";
62+ "mini-server" = "hostaddr=127.0.0.1 port=5432 dbname=postgres sslmode=require";
63+ };
64+ description = ''
65+ Postage requires at least one PostgreSQL server be defined.
66+ </para><para>
67+ Detailed information about PostgreSQL connection strings is available at:
68+ <link xlink:href="http://www.postgresql.org/docs/current/static/libpq-connect.html"/>
69+ </para><para>
70+ Note that you should not specify your user name or password. That
71+ information will be entered on the login screen. If you specify a
72+ username or password, it will be removed by Postage before attempting to
73+ connect to a database.
74+ '';
75+ };
76+
77+ allowCustomConnections = mkOption {
78+ type = types.bool;
79+ default = false;
80+ description = ''
81+ This tells Postage whether or not to allow anyone to use a custom
82+ connection from the login screen.
83+ '';
84+ };
85+
86+ port = mkOption {
87+ type = types.int;
88+ default = 8080;
89+ description = ''
90+ This tells Postage what port to listen on for browser requests.
91+ '';
92+ };
93+
94+ localOnly = mkOption {
95+ type = types.bool;
96+ default = true;
97+ description = ''
98+ This tells Postage whether or not to set the listening socket to local
99+ addresses only.
100+ '';
101+ };
102+
103+ superOnly = mkOption {
104+ type = types.bool;
105+ default = true;
106+ description = ''
107+ This tells Postage whether or not to only allow super users to
108+ login. The recommended value is true and will restrict users who are not
109+ super users from logging in to any PostgreSQL instance through
110+ Postage. Note that a connection will be made to PostgreSQL in order to
111+ test if the user is a superuser.
112+ '';
113+ };
114+
115+ loginGroup = mkOption {
116+ type = types.nullOr types.str;
117+ default = null;
118+ description = ''
119+ This tells Postage to only allow users in a certain PostgreSQL group to
120+ login to Postage. Note that a connection will be made to PostgreSQL in
121+ order to test if the user is a member of the login group.
122+ '';
123+ };
124+
125+ loginTimeout = mkOption {
126+ type = types.int;
127+ default = 3600;
128+ description = ''
129+ Number of seconds of inactivity before user is automatically logged
130+ out.
131+ '';
132+ };
133+
134+ dataRoot = mkOption {
135+ type = types.str;
136+ default = "/var/lib/postage";
137+ description = ''
138+ This tells Postage where to put the SQL file history. All tabs are saved
139+ to this location so that if you get disconnected from Postage you
140+ don't lose your work.
141+ '';
142+ };
143+
144+ tls = mkOption {
145+ type = types.nullOr (types.submodule {
146+ options = {
147+ cert = mkOption {
148+ type = types.str;
149+ description = "TLS certificate";
150+ };
151+ key = mkOption {
152+ type = types.str;
153+ description = "TLS key";
154+ };
155+ };
156+ });
157+ default = null;
158+ description = ''
159+ These options tell Postage where the TLS Certificate and Key files
160+ reside. If you use these options then you'll only be able to access
161+ Postage through a secure TLS connection. These options are only
162+ necessary if you wish to connect directly to Postage using a secure TLS
163+ connection. As an alternative, you can set up Postage in a reverse proxy
164+ configuration. This allows your web server to terminate the secure
165+ connection and pass on the request to Postage. You can find help to set
166+ up this configuration in:
167+ <link xlink:href="https://github.com/workflowproducts/postage/blob/master/INSTALL_NGINX.md"/>
168+ '';
169+ };
170+
171+ logLevel = mkOption {
172+ type = types.enum ["error" "warn" "notice" "info"];
173+ default = "error";
174+ description = ''
175+ Verbosity of logs
176+ '';
177+ };
178+ };
179+
180+ config = mkIf cfg.enable {
181+ systemd.services.postage = {
182+ description = "postage - PostgreSQL Administration for the web";
183+ wants = [ "postgresql.service" ];
184+ after = [ "postgresql.service" ];
185+ wantedBy = [ "multi-user.target" ];
186+ serviceConfig = {
187+ User = postage;
188+ Group = postage;
189+ ExecStart = "${pkgs.postage}/sbin/postage -c ${confFile}" +
190+ optionalString cfg.localOnly " --local-only=true";
191+ };
192+ };
193+ users = {
194+ users."${postage}" = {
195+ name = postage;
196+ group = postage;
197+ home = cfg.dataRoot;
198+ createHome = true;
199+ };
200+ groups."${postage}" = {
201+ name = postage;
202+ };
203+ };
204+ };
205+}