This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
410 lines (402 loc) · 13.8 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
purs-nix.url = "github:purs-nix/purs-nix";
utils.url = "github:ursi/flake-utils";
ps-tools.follows = "purs-nix/ps-tools";
npmlock2nix.url = "github:nix-community/npmlock2nix";
npmlock2nix.flake = false;
simple-csv.url = "github:smartermaths/purescript-simple-csv";
simple-csv.flake = false;
generators.url = "github:nix-community/nixos-generators";
generators.inputs.nixpkgs.follows = "nixpkgs";
agenix.url = "github:ryantm/agenix";
agenix.inputs.nixpkgs.follows = "nixpkgs";
everyday.url = "github:klarkc/nixos-everyday";
};
outputs = { self, utils, ... }@inputs:
let
linux = "linux";
x64 = "x86_64";
linux-x64 = "${x64}-${linux}";
# TODO add missing arm to match standard systems
# right now purs-nix is only compatible with x86_64-linux
platform = x64;
os = linux;
systems = [ "${platform}-${os}" ];
make-pkgs = system: import inputs.nixpkgs {
inherit system;
# required by npmlock2nix
config.permittedInsecurePackages = [
"nodejs-16.20.1"
];
};
nixosModules =
let
inherit (inputs.nixpkgs.lib) types mkDefault mkOption;
inherit (inputs.generators.nixosModules) all-formats;
env = { config, ... }:
{
imports = [ inputs.agenix.nixosModules.default ];
options.env = {
service = mkOption {
type = types.str;
};
file = mkOption {
type = types.path;
};
};
config =
let cfg = config.env; in
{
age = {
secrets.env.file = cfg.file;
};
systemd.services.${cfg.service}.serviceConfig.EnvironmentFile =
config.age.secrets.env.path;
};
};
host-keys = { config, ... }:
{
options.host-keys = {
dir = mkOption {
type = types.str;
default = "/var/keys";
};
source = mkOption {
type = types.str;
};
};
config =
let cfg = config.host-keys; in
{
environment.etc =
{
"ssh/ssh_host_ed25519_key" = {
mode = "0600";
source = "${cfg.dir}/id_ed25519";
};
"ssh/ssh_host_ed25519_key.pub" = {
mode = "0644";
source = "${cfg.dir}/id_ed25519.pub";
};
};
virtualisation.sharedDirectories.keys = {
source = cfg.source;
target = cfg.dir;
};
};
};
temporal = { lib, pkgs, config, ... }:
let
inherit (lib) mkIf mkEnableOption;
cfg = config.services.temporal;
user = "temporal";
group = "temporal";
in
{
options.services.temporal = {
enable = mkEnableOption "temporal";
};
config = {
environment.defaultPackages = with pkgs; [
temporalite
];
users = {
users.${user} = {
inherit group;
isSystemUser = true;
description = "Temporalite Daemon";
home = "/var/temporal";
createHome = true;
};
groups.${group} = { };
};
systemd.services.temporal = {
description = "Temporalite server";
wantedBy = [ "multi-user.target" ];
path = with pkgs; [ temporalite ];
script = ''
temporalite start \
--namespace default \
--ip 0.0.0.0 \
--ui-ip 0.0.0.0
'';
startLimitIntervalSec = 60;
startLimitBurst = 3;
serviceConfig = {
User = user;
Group = group;
WorkingDirectory = "~";
};
};
};
};
evo-siigo = { config, lib, pkgs, ... }: {
options.services.evo-siigo.enable =
lib.mkEnableOption "evo-siigo";
config.systemd.services.evo-siigo = {
description = "Evo-siigo evo-siigo-srv";
wantedBy = [ "multi-user.target" ];
requires = [ "temporal.service" ];
script = self.apps.${linux-x64}.default.program;
startLimitIntervalSec = 60;
startLimitBurst = 3;
serviceConfig.DynamicUser = "yes";
};
};
evo-siigo-srv = { config, ... }:
let home = "/home/klarkc"; in
rec
{
imports = [
all-formats
temporal
evo-siigo
];
nix.settings.experimental-features = [ "nix-command" "flakes" ];
system.stateVersion = config.system.nixos.version;
fileSystems."/".device = "none";
boot.loader.grub.device = "nodev";
boot.readOnlyNixStore = false;
security.sudo.wheelNeedsPassword = false;
programs.vim.defaultEditor = true;
users = {
users.klarkc = {
isNormalUser = true;
inherit home;
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
(builtins.readFile ./secrets/klarkc.pub)
];
};
mutableUsers = false;
};
services.openssh.enable = true;
networking.firewall.enable = false;
formatConfigs.vm-nogui = { config, ... }:
{
imports =
let
inherit (inputs.everyday.nixosModules) logger;
in
imports ++ [ host-keys env logger ];
host-keys.source = "${home}/.ssh";
env = {
service = "evo-siigo";
file = ./secrets/env.age;
};
# TODO remove workaround ryantm/agenix#45
age.identityPaths = [ "/var/keys/id_ed25519" ];
virtualisation.forwardPorts = [
{ from = "host"; host.port = 2222; guest.port = 22; }
{ from = "host"; host.port = 8233; guest.port = 8233; }
{ from = "host"; host.port = 8080; guest.port = 8080; }
];
};
};
in
{ inherit evo-siigo temporal evo-siigo-srv; };
nixosConfigurations =
let
inherit (inputs.nixpkgs.lib) nixosSystem;
inherit (self.nixosModules) evo-siigo-srv;
in
{
evo-siigo-srv0 = nixosSystem {
system = linux-x64;
modules = [
evo-siigo-srv
({
networking.hostName = "evo-siigo-srv0";
})
];
};
};
in
{ inherit nixosModules nixosConfigurations; } // utils.apply-systems
{ inherit inputs systems make-pkgs; }
({ system, pkgs, ps-tools, agenix, ... }:
let
inherit (ps-tools.for-0_15) purescript purs-tidy purescript-language-server;
nodejs = pkgs.runCommand
"nodejs-flags"
{
inherit (pkgs.nodejs) meta version src;
nativeBuildInputs = [ pkgs.makeWrapper ];
NODE_OPTIONS = "--experimental-import-meta-resolve";
}
''
cp -r --no-preserve=ownership --reflink=auto ${pkgs.nodejs} $out
chmod -R +w $out
wrapProgram $out/bin/node --set NODE_OPTIONS $NODE_OPTIONS
'';
npmlock2nix = import inputs.npmlock2nix { inherit pkgs; };
purs-nix = inputs.purs-nix { inherit system; };
node_modules = npmlock2nix.v2.node_modules { src = ./.; inherit nodejs; } + /node_modules;
ulid_ = pkgs.lib.recursiveUpdate purs-nix.ps-pkgs.ulid {
purs-nix-info.foreign.Ulid = { inherit node_modules; };
};
simple-csv_ = purs-nix.build {
name = "simple-csv";
src.path = inputs.simple-csv;
info.dependencies = with purs-nix.ps-pkgs; [
arrays
"assert"
control
effect
either
maybe
prelude
string-parsers
strings
];
};
ps = purs-nix.purs
{
# Project dir (src, test)
dir = ./.;
# Dependencies
dependencies =
with purs-nix.ps-pkgs;
[
debug
prelude
stringutils
console
effect
aff
js-promise
js-promise-aff
unlift
ulid_
foreign
foreign-object
parallel
node-path
httpurple
httpurple-argonaut
argonaut
fetch
fetch-argonaut
newtype
node-process
node-buffer
dotenv
monad-logger
record
free
formatters
simple-csv_
];
# FFI dependencies
foreign."Temporal.Client" = { inherit node_modules; };
foreign."Temporal.Client.Connection" = { inherit node_modules; };
foreign."Temporal.Node.Worker" = { inherit node_modules; };
foreign."Temporal.Workflow" = { inherit node_modules; };
foreign."Workflows" = { inherit node_modules; };
# compiler
inherit purescript nodejs;
};
ps-command = ps.command { };
purs-watch = pkgs.writeShellApplication {
name = "purs-watch";
runtimeInputs = with pkgs; [ entr ps-command ];
text = ''find src | entr -s "purs-nix $*"'';
};
concurrent = pkgs.writeShellApplication {
name = "concurrent";
runtimeInputs = with pkgs; [
concurrently
];
text = ''
concurrently\
--color "auto"\
--prefix "[{command}]"\
--handle-input\
--restart-tries 10\
"$@"
'';
};
devRuntimeInputs = with pkgs; [
purs-watch
concurrent
temporalite
];
dev = pkgs.writeShellApplication {
name = "dev";
runtimeInputs = devRuntimeInputs;
text = ''concurrent \
"purs-watch run"\
"temporalite start --namespace default"
'';
};
dev-debug = pkgs.writeShellApplication {
name = "dev-debug";
runtimeInputs = devRuntimeInputs ++ [ ps-command ];
text = ''concurrent \
"TEMPORAL_DEBUG=1 NODE_OPTIONS=--inspect-brk purs-nix run"\
"temporalite start --namespace default"
'';
};
vm = name: pkgs.writeShellApplication {
inherit name;
text = ''
export USE_TMPDIR=0
${self.nixosConfigurations.${name}.config.formats.vm-nogui}
'';
};
in
{
apps.default =
{
type = "app";
program = "${self.packages.${system}.default}";
};
packages =
with ps;
{
default = pkgs.writeScript "evo-siigo" ''
#!${nodejs}/bin/node
import("${self.packages.${system}.output}/Main/index.js").then(m=>m.main())
'';
output = output { };
};
devShells.default = pkgs.mkShell {
packages =
devRuntimeInputs
++ [
ps-command
dev
dev-debug
(vm "evo-siigo-srv0")
purescript
purs-tidy
purescript-language-server
nodejs
agenix
];
shellHook = ''
alias log_='printf "\033[1;32m%s\033[0m\n" "$@"'
alias info_='printf "\033[1;34m[INFO] %s\033[0m\n" "$@"'
log_ "Welcome to evo-siigo shell."
info_ "Available commands: dev, dev-debug, evo-siigo-srv0, agenix."
'';
};
});
# --- Flake Local Nix Configuration ----------------------------
nixConfig = {
extra-experimental-features = "nix-command flakes";
# This sets the flake to use nix cache.
# Nix should ask for permission before using it,
# but remove it here if you do not want it to.
extra-substituters = [
"https://klarkc.cachix.org?priority=99"
"https://cache.nixos.org"
];
extra-trusted-public-keys = [
"klarkc.cachix.org-1:R+z+m4Cq0hMgfZ7AQ42WRpGuHJumLLx3k0XhwpNFq9U="
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
];
};
}