-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
218 lines (205 loc) · 6.35 KB
/
flake.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# SPDX-FileCopyrightText: 2023 Kerstin Humm <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
{
description = "How late is RE1?";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachSystem
[
"x86_64-linux"
"aarch64-linux"
]
(
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = rec {
server =
let
crates = import ./server/Cargo.nix { inherit pkgs; };
in
crates.workspaceMembers.isre1late-server.build;
client = import ./client {
inherit pkgs;
inherit icons;
};
icons = pkgs.runCommand "icons" { nativeBuildInputs = [ pkgs.imagemagick ]; } ''
${import ./client/icons.nix pkgs}/bin/generate-icons.sh ${./client/icon.svg}
mkdir -p $out
cp -R . $out
'';
default = server;
};
apps = rec {
server = flake-utils.lib.mkApp { drv = self.packages.${system}.server; };
default = server;
};
checks = {
reuse = pkgs.runCommand "reuse-check" { } ''
${pkgs.reuse}/bin/reuse --root ${self} lint && touch $out
'';
inherit (self.packages."${system}") client server;
};
devShells.default = pkgs.mkShell {
buildInputs =
with pkgs;
[
# Server
nodejs
cargo
rustc
clippy
rustfmt
diesel-cli
pkg-config
openssl
postgresql
crate2nix
reuse
(python3.withPackages (ps: with ps; [ psycopg2 ]))
websocat
]
++
# Client
(with pkgs.elmPackages; [
elm
elm-format
elm-test
elm-json
elm2nix
(python3.withPackages (ps: with ps; [ requests ]))
(import ./client/nginx.nix pkgs)
]);
RUST_LOG = "info";
DATABASE_URL = "postgres://localhost/isre1late?host=/run/postgresql";
PGDATABASE = "isre1late";
HAFAS_BASE_URL = "https://v6.vbb.transport.rest";
};
}
)
// {
nixosModules.default =
{
config,
pkgs,
lib,
...
}:
let
server = self.packages.${pkgs.stdenv.hostPlatform.system}.server;
client = self.packages.${pkgs.stdenv.hostPlatform.system}.client;
inherit (lib)
mkEnableOption
mkOption
mkIf
types
;
cfg = config.services.isre1late;
in
{
options.services.isre1late = {
enable = mkEnableOption "IsRE1late server";
port = mkOption {
type = types.int;
example = 8080;
description = "TCP port to use.";
};
websocketPort = mkOption {
type = types.int;
example = 8081;
description = "TCP port to use for the websocket server.";
};
hafasBaseUrl = mkOption {
type = types.str;
default = "https://v6.vbb.transport.rest";
description = "Hafas Base URL without a leading slash.";
};
};
config = mkIf cfg.enable {
services.postgresql = {
enable = true;
ensureDatabases = [ "isre1late" ];
ensureUsers = [
{
name = "isre1late";
ensureDBOwnership = true;
}
];
};
systemd.services.isre1late = {
description = "How late is RE1?";
after = [
"network.target"
"postgresql.service"
];
requires = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
environment = {
DATABASE_URL = "postgres://localhost/isre1late?host=/run/postgresql";
HAFAS_BASE_URL = cfg.hafasBaseUrl;
};
serviceConfig = {
Type = "simple";
ExecStart = ''
${server}/bin/isre1late-server \
--port ${builtins.toString cfg.port} \
--ws-port ${builtins.toString cfg.websocketPort}
'';
Restart = "always";
RestartSec = "30s";
User = "isre1late";
Group = "isre1late";
};
};
users.users.isre1late = {
isSystemUser = true;
group = "isre1late";
packages = [ server ];
};
users.groups.isre1late = { };
services.nginx.enable = true;
services.nginx.virtualHosts."isre1late.erictapen.name" = {
enableACME = true;
forceSSL = true;
locations = {
"/" = {
root = client;
tryFiles = "/index.html =404";
};
"/assets/" = {
alias = client + "/assets/";
};
"/api".return = "301 /api/";
"/api/" = {
proxyPass = "http://[::1]:${toString cfg.port}";
extraConfig = ''
gzip on;
gzip_types application/json;
'';
};
"/api/ws".return = "301 /api/ws/";
"/api/ws/" = {
proxyPass = "http://[::1]:${toString cfg.websocketPort}";
extraConfig = ''
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
'';
};
};
};
};
};
};
}