blob: f6eb2afc2dfade88107eede0eb29c09ca9ac09b8 (
plain)
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
|
{ config, pkgs, lib, ... }:
let
inherit (builtins) toString;
inherit (lib)
filterAttrs hasPrefix mapAttrsToList
mkEnableOption concatStrings mkIf mkOption types ;
inherit (types)
enum int nullOr attrsOf path str submodule ;
explicit = filterAttrs (n: v: n != "_module" && v != null);
cfg = config.nixsap.apps.sproxy;
oauth2Options = concatStrings (mapAttrsToList (n: c:
if n == "google" then ''
client_id : ${c.client_id}
client_secret : ${c.client_secret_file}
'' else ''
${n}_client_id : ${c.client_id}
${n}_client_secret : ${c.client_secret_file}
''
) (explicit cfg.oauth2));
configFile = pkgs.writeText "sproxy.conf" ''
${oauth2Options}
user : ${cfg.user}
cookie_domain : ${cfg.cookieDomain}
cookie_name : ${cfg.cookieName}
database : "${cfg.database}"
listen : 443
log_level : ${cfg.logLevel}
log_target : stderr
ssl_certs : ${cfg.sslCert}
ssl_key : ${cfg.sslKey}
session_shelf_life : ${toString cfg.sessionShelfLife}
${if cfg.backendSocket != null then ''
backend_socket : ${cfg.backendSocket}
'' else ''
backend_address : ${cfg.backendAddress}
backend_port : ${toString cfg.backendPort}
''}
'';
keys = [ cfg.sslKey ]
++ mapAttrsToList (_: c: c.client_secret_file) (explicit cfg.oauth2)
;
oauth2 = mkOption {
type = attrsOf (submodule {
options = {
client_id = mkOption {
type = str;
description = "OAuth2 client id";
};
client_secret_file = mkOption {
type = path;
description = "File with OAuth2 client secret";
};
};
});
example = {
google.client_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
google.client_secret_file = "/run/keys/google_oauth2_secret";
};
};
in {
options.nixsap.apps.sproxy = {
enable = mkEnableOption "SProxy";
inherit oauth2;
user = mkOption {
description = "User to run as";
default = "sproxy";
type = str;
};
cookieDomain = mkOption {
description = "Cookie domain";
type = str;
example = "example.com";
};
cookieName = mkOption {
description = "Cookie name";
type = str;
example = "sproxy";
};
logLevel = mkOption {
description = "Log level";
default = "info";
type = enum [ "info" "warn" "debug" ];
};
sslCert = mkOption {
description = "SSL certificate (in PEM format)";
type = path;
};
sslKey = mkOption {
description = "SSL key (in PEM format) - secret";
type = path;
};
backendAddress = mkOption {
description = "Backend TCP address";
type = str;
default = "127.0.0.1";
};
backendPort = mkOption {
description = "Backend TCP port";
type = int;
example = 8080;
};
backendSocket = mkOption {
description = "Backend UNIX socket. If set, other backend options are ignored";
type = nullOr path;
default = null;
};
database = mkOption {
description = "PostgreSQL connection string";
type = str;
example = "user=sproxy dbname=sproxy port=6001";
};
sessionShelfLife = mkOption {
description = "Session shelf life in seconds";
type = int;
default = 3600 * 24 * 14; # two weeks
};
};
config = mkIf cfg.enable {
nixsap.system.users.daemons = [ cfg.user ];
nixsap.deployment.keyrings.${cfg.user} = keys;
systemd.services.sproxy = {
description = "Sproxy secure proxy";
wantedBy = [ "multi-user.target" ];
wants = [ "keys.target" ];
after = [ "keys.target" "network.target" "local-fs.target" ];
serviceConfig = {
ExecStart = "${pkgs.sproxy}/bin/sproxy --config=${configFile}";
Restart = "on-failure";
};
};
};
}
|