-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
114 lines (106 loc) · 4.31 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
{
description = "Tinyquiz – an open source online quiz platform";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.nix-filter.url = "github:numtide/nix-filter/master";
outputs = { self, nixpkgs, nix-filter }:
let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
buildCmd = name:
(pkgs.buildGoPackage {
pname = "tinyquiz-${name}";
version = "dev";
src = with nix-filter.lib; filter { root = ./.; include = [ (inDirectory "cmd") (inDirectory "pkg") (inDirectory "ui") "go.mod" "go.sum" ]; };
goDeps = ./deps.nix;
preBuild = "go generate vkane.cz/tinyquiz/...";
checkPhase = "go test -race vkane.cz/tinyquiz/...";
doCheck = true;
goPackagePath = "vkane.cz/tinyquiz";
subPackages = [ "cmd/${name}" ];
});
in
{
defaultPackage.x86_64-linux = self.packages.x86_64-linux.tinyquiz-web;
packages.x86_64-linux.tinyquiz-web = buildCmd "web";
packages.x86_64-linux.dev = pkgs.writeShellScriptBin "dev" ''
echo "This dev script must be run from the project root, otherwise unexpected behavior might occur."
read -p "Are you in the right directory and shall I continue? (y/n): " ack
if [ "$ack" != y ]; then exit 1; fi
unset GOROOT # Use the one bundled into the binary. I don't currently know who sets this to the wrong one
'${pkgs.findutils}/bin/find' cmd pkg ui | '${pkgs.entr}/bin/entr' -dr '${pkgs.go}/bin/go' run ./cmd/web
'';
packages.x86_64-linux.devDb = pkgs.writeShellScriptBin "devDb" ''
'${pkgs.postgresql}/bin/postgres' -D .pg-data -k "$PWD/.pg-sockets" -c log_statement=all #-c listen_addresses="" # Goland does not support connecting over socket
'';
nixosModules.web = { config, lib, pkgs, ... }:
let
cfg = config.services.tinyquiz;
in
{
options = {
services.tinyquiz = {
enable = lib.mkEnableOption "tinyquiz";
postgresql.enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether the set up the postgresql database for tinyquiz automatically.
'';
};
config = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
};
};
};
config = lib.mkIf cfg.enable {
systemd.services.tinyquiz = {
description = "Tinyquiz service";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" (lib.mkIf cfg.postgresql.enable "postgresql.service") ];
serviceConfig = {
ExecStart = "${self.packages.x86_64-linux.tinyquiz-web}/bin/web";
User = "tinyquiz";
DynamicUser = true;
};
environment = cfg.config;
};
services.tinyquiz.config = if cfg.postgresql.enable then {
TINYQUIZ_PG_HOST = "/run/postgresql";
} else {};
services.postgresql = lib.mkIf cfg.postgresql.enable {
enable = true;
ensureDatabases = [ "tinyquiz" ];
ensureUsers = [
{
name = "tinyquiz";
ensurePermissions = {
"DATABASE \"tinyquiz\"" = "ALL PRIVILEGES";
};
}
];
};
};
};
checks.x86_64-linux.module = (import "${nixpkgs}/nixos/tests/make-test-python.nix" ({ pkgs, ... }: {
system = "x86_64-linux";
machine = { config, pkgs, ... }:
{
require = [
self.nixosModules.web
];
services.tinyquiz = {
enable = true;
config = {
TINYQUIZ_LISTEN = ":8080";
};
};
systemd.services.postgresql.serviceConfig.TimeoutSec = pkgs.lib.mkForce "3600"; # wait for postgresql even on very slow machines. Not using "infinity" is just a safeguard
};
testScript = ''
machine.start()
machine.wait_for_unit("default.target")
machine.wait_for_open_port(8080)
machine.succeed("curl --fail http://localhost:8080")
'';
})) { system = "x86_64-linux"; inherit pkgs; };
};
}