1
1
{ inputs , pkgs , config , ... } :
2
2
3
3
let
4
- port = "8000" ;
5
- startScript = pkgs . writeShellScript "catcolab.sh" ''
6
- rm -f instrument.mjs
7
- cp ${ config . age . secrets . "instrument.mjs" . path } .
8
- ${ pkgs . nodejs } /bin/node dist/index.js
4
+ automergePort = "8010" ;
5
+ backendPort = "8000" ;
6
+
7
+ automergeScript = pkgs . writeShellScript "automerge.sh" ''
8
+ ln -sf ${ config . age . secrets . "instrument.mjs" . path } /var/lib/catcolab/packages/automerge-doc-server/
9
+ ${ pkgs . nodejs } /bin/node dist/automerge-doc-server/src/main.js
10
+ '' ;
11
+
12
+ backendScript = pkgs . writeShellScript "backend.sh" ''
13
+ ln -sf ${ config . age . secrets . ".env" . path } /var/lib/catcolab/packages/backend/
14
+ ../../target/debug/backend
15
+ '' ;
16
+
17
+ initScript = pkgs . writeShellScriptBin "catcolab-init" ''
18
+ echo -e "\n\n##### catcolab-init: cloning catcolab repo...\n\n"
19
+ cd /var/lib
20
+ if [ -z "$1" ]; then branch="main"; else branch="$1"; fi
21
+ git clone -b $branch https://github.com/ToposInstitute/CatColab.git
22
+ mv CatColab catcolab
23
+ chown -R catcolab:catcolab catcolab
24
+
25
+ echo -e "\n\n##### catcolab-init: linking secrets...\n\n"
26
+ ln -sf ${ config . age . secrets . "instrument.mjs" . path } /var/lib/catcolab/packages/automerge-doc-server/
27
+ ln -sf ${ config . age . secrets . ".env" . path } /var/lib/catcolab/packages/backend/
28
+
29
+ echo -e "\n\n##### catcolab-init: installing nodejs dependencies...\n\n"
30
+ su -l catcolab -c "cd /var/lib/catcolab/packages/backend; pnpm install"
31
+
32
+ echo -e "\n\n##### catcolab-init: installing rust and cargo...\n\n"
33
+ su -l catcolab -c "rustup default stable"
34
+
35
+ echo -e "\n\n##### catcolab-init: installing sqlx-cli for migrations...\n\n"
36
+ su -l catcolab -c "cargo install sqlx-cli"
37
+
38
+ echo -e "\n\n##### catcolab-init: setting up postgres user, database, permissions...\n\n"
39
+ su -l postgres -- /var/lib/catcolab/infrastructure/scripts/initdb.sh $(cat ${ config . age . secrets . ".env" . path } )
40
+
41
+ echo -e "\n\n##### catcolab-init: stopping automerge, build services...\n\n"
42
+ /var/lib/catcolab/infrastructure/scripts/stop.sh
43
+
44
+ echo -e "\n\n##### catcolab-init: migrating database...\n\n"
45
+ su -l catcolab -- /var/lib/catcolab/infrastructure/scripts/migrate.sh
46
+
47
+ echo -e "\n\n##### catcolab-init: building binaries...\n\n"
48
+ su -l catcolab -- /var/lib/catcolab/infrastructure/scripts/build.sh
49
+
50
+ echo -e "\n\n##### catcolab-init: start automerge, build services...\n\n"
51
+ /var/lib/catcolab/infrastructure/scripts/start.sh
52
+ '' ;
53
+
54
+ stopScript = pkgs . writeShellScriptBin "catcolab-stop" ''
55
+ /var/lib/catcolab/infrastructure/scripts/stop.sh
56
+ '' ;
57
+
58
+ startScript = pkgs . writeShellScriptBin "catcolab-start" ''
59
+ /var/lib/catcolab/infrastructure/scripts/start.sh
60
+ '' ;
61
+
62
+ restartScript = pkgs . writeShellScriptBin "catcolab-restart" ''
63
+ /var/lib/catcolab/infrastructure/scripts/restart.sh
64
+ '' ;
65
+
66
+ statusScript = pkgs . writeShellScriptBin "catcolab-status" ''
67
+ /var/lib/catcolab/infrastructure/scripts/status.sh
68
+ '' ;
69
+
70
+ migrateScript = pkgs . writeShellScriptBin "catcolab-migrate" ''
71
+ /var/lib/catcolab/infrastructure/scripts/migrate.sh
9
72
'' ;
73
+
74
+ buildScript = pkgs . writeShellScriptBin "catcolab-build" ''
75
+ /var/lib/catcolab/infrastructure/scripts/build.sh
76
+ '' ;
77
+
78
+ packages = with pkgs ; [
79
+ rustup
80
+ nodejs
81
+ nodejs . pkgs . pnpm
82
+ git
83
+ stdenv . cc
84
+ openssl . dev
85
+ pkg-config
86
+ ] ;
87
+
88
+ scripts = [
89
+ initScript
90
+ stopScript
91
+ startScript
92
+ restartScript
93
+ statusScript
94
+ migrateScript
95
+ buildScript
96
+ ] ;
97
+
10
98
in {
11
- age . secrets . DATABASE_URL = {
12
- file = "${ inputs . self } /secrets/DATABASE_URL .age" ;
99
+ age . secrets . ".env" = {
100
+ file = "${ inputs . self } /secrets/.env .age" ;
13
101
mode = "400" ;
14
102
owner = "catcolab" ;
15
103
} ;
@@ -23,61 +111,99 @@ in {
23
111
services . postgresql . enable = true ;
24
112
25
113
services . nginx . enable = true ;
26
- services . nginx . virtualHosts . "backend.catcolab.org" = {
114
+
115
+ services . nginx . virtualHosts . "automerge.catcolab.org" = {
27
116
forceSSL = true ;
28
117
enableACME = true ;
29
118
locations . "/" = {
30
119
extraConfig = ''
31
- if ($request_method = OPTIONS) {
32
- return 204;
33
- }
34
- proxy_hide_header 'Access-Control-Allow-Origin';
35
- add_header 'Access-Control-Allow-Origin' '*' always;
36
- add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS' always;
37
- add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
38
- proxy_pass http://localhost:${ port } ;
39
- error_log syslog:server=unix:/dev/log;
40
- access_log syslog:server=unix:/dev/log;
41
- proxy_http_version 1.1;
42
- proxy_set_header Upgrade $http_upgrade;
43
- proxy_set_header Connection "upgrade";
120
+ if ($request_method = OPTIONS) {
121
+ return 204;
122
+ }
123
+ proxy_hide_header 'Access-Control-Allow-Origin';
124
+ add_header 'Access-Control-Allow-Origin' '*' always;
125
+ add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS' always;
126
+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
127
+ proxy_pass http://localhost:${ automergePort } ;
128
+ error_log syslog:server=unix:/dev/log;
129
+ access_log syslog:server=unix:/dev/log;
130
+ proxy_http_version 1.1;
131
+ proxy_set_header Upgrade $http_upgrade;
132
+ proxy_set_header Connection "upgrade";
44
133
'' ;
45
134
} ;
135
+ } ;
136
+
137
+ services . nginx . virtualHosts . "backend.catcolab.org" = {
138
+ forceSSL = true ;
139
+ enableACME = true ;
140
+ locations . "/" = {
141
+ extraConfig = ''
142
+ if ($request_method = OPTIONS) {
143
+ return 204;
144
+ }
145
+ proxy_hide_header 'Access-Control-Allow-Origin';
146
+ add_header 'Access-Control-Allow-Origin' '*' always;
147
+ add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS' always;
148
+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
149
+ proxy_pass http://localhost:${ backendPort } ;
150
+ error_log syslog:server=unix:/dev/log;
151
+ access_log syslog:server=unix:/dev/log;
152
+ proxy_http_version 1.1;
153
+ proxy_set_header Upgrade $http_upgrade;
154
+ proxy_set_header Connection "upgrade";
155
+ '' ;
156
+ } ;
157
+ } ;
158
+
159
+ systemd . services . automerge = {
160
+ enable = true ;
161
+ wantedBy = [ "multi-user.target" ] ;
162
+
163
+ environment = {
164
+ PORT = automergePort ;
165
+ # NODE_OPTIONS = "--import ./instrument.mjs"; # sentry disabled - need Owen to fix
166
+ } ;
167
+
168
+ serviceConfig = {
169
+ User = "catcolab" ;
170
+ ExecStart = automergeScript ;
171
+ Type = "simple" ;
172
+ WorkingDirectory = "/var/lib/catcolab/packages/automerge-doc-server/" ;
173
+ Restart = "on-failure" ;
174
+ } ;
46
175
} ;
47
176
48
- systemd . services . catcolab = {
177
+ systemd . services . backend = {
49
178
enable = true ;
50
179
wantedBy = [ "multi-user.target" ] ;
51
180
52
181
environment = {
53
- PORT = port ;
54
- DATABASE_URL_PATH = config . age . secrets . DATABASE_URL . path ;
55
- NODE_OPTIONS = "--import ./instrument.mjs" ;
182
+ PORT = backendPort ;
56
183
} ;
57
184
58
185
serviceConfig = {
59
186
User = "catcolab" ;
60
- ExecStart = startScript ;
187
+ ExecStart = backendScript ;
61
188
Type = "simple" ;
62
189
WorkingDirectory = "/var/lib/catcolab/packages/backend/" ;
63
190
Restart = "on-failure" ;
64
191
} ;
65
192
} ;
66
193
67
- users . users . catcolab = {
68
- isNormalUser = true ;
69
- group = "catcolab" ;
70
- } ;
71
-
72
- environment . systemPackages = with pkgs ; [
73
- rustup
74
- nodejs
75
- nodejs . pkgs . pnpm
76
- git
77
- stdenv . cc
78
- ] ;
194
+ security . sudo . extraRules = [ {
195
+ users = [ "catcolab" ] ;
196
+ commands = [
197
+ { command = "/run/current-system/sw/bin/systemctl start automerge" ; options = [ "NOPASSWD" ] ; }
198
+ { command = "/run/current-system/sw/bin/systemctl stop automerge" ; options = [ "NOPASSWD" ] ; }
199
+ { command = "/run/current-system/sw/bin/systemctl restart automerge" ; options = [ "NOPASSWD" ] ; }
200
+ { command = "/run/current-system/sw/bin/systemctl start backend" ; options = [ "NOPASSWD" ] ; }
201
+ { command = "/run/current-system/sw/bin/systemctl stop backend" ; options = [ "NOPASSWD" ] ; }
202
+ { command = "/run/current-system/sw/bin/systemctl restart backend" ; options = [ "NOPASSWD" ] ; }
203
+ ] ;
204
+ } ] ;
79
205
80
- environment . variables . DATABASE_URL_PATH = config . age . secrets . DATABASE_URL . path ;
206
+ environment . systemPackages = packages ++ scripts ;
81
207
82
- users . groups . catcolab = { } ;
208
+ environment . variables . PKG_CONFIG_PATH = "/run/current-system/sw/lib/pkgconfig" ;
83
209
}
0 commit comments