-
Notifications
You must be signed in to change notification settings - Fork 5
/
flake.nix
243 lines (215 loc) · 7.84 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
{
inputs = {
naersk.url = "github:nmattia/naersk/master";
naersk.inputs.nixpkgs.follows = "nixpkgs";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
xess = {
url = "github:Xe/Xess";
inputs.nixpkgs.follows = "nixpkgs";
inputs.utils.follows = "utils";
};
deno2nix = {
url = "github:Xe/deno2nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "utils";
};
};
outputs = { self, nixpkgs, utils, naersk, xess, deno2nix, ... }@inputs:
utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ deno2nix.overlays.default ];
};
naersk-lib = pkgs.callPackage naersk { };
in rec {
packages = rec {
unique-monster = pkgs.stdenv.mkDerivation {
src = self.packages."${system}".waifud;
pname = "unique-monster";
version = self.packages."${system}".waifud-bin.version;
phases = "installPhase";
installPhase = ''
mkdir -p $out/bin
cp $src/bin/unique-monster $out/bin
'';
};
waifud-bin = naersk-lib.buildPackage {
pname = "waifud-bin";
src = ./.;
buildInputs = with pkgs; [
pkg-config
openssl
sqlite-interactive
libvirt
];
};
waifud-frontend = let
build = { entrypoint, name ? entrypoint, minify ? true }:
pkgs.deno2nix.mkBundled {
pname = "xesite-frontend-${name}";
inherit (waifud-bin) version;
src = ./frontend;
lockfile = ./frontend/deno.lock;
output = "${entrypoint}.js";
outPath = "static/js";
entrypoint = "./${entrypoint}.tsx";
importMap = "./import_map.json";
inherit minify;
};
instance_detail = build { entrypoint = "instance_detail"; };
instance_create = build { entrypoint = "instance_create"; };
in pkgs.symlinkJoin {
name = "waifud-frontend-${waifud-bin.version}";
paths = [ instance_detail instance_create ];
};
waifud = pkgs.symlinkJoin {
name = "waifud-${waifud-bin.version}";
paths = with self.packages."${system}"; [
waifud-bin
waifud-frontend
];
};
waifuctl = pkgs.stdenv.mkDerivation {
src = self.packages."${system}".waifud;
pname = "waifuctl";
version = self.packages."${system}".waifud-bin.version;
phases = "installPhase";
installPhase = ''
mkdir -p $out/bin
cp $src/bin/waifuctl $out/bin
mkdir -p $out/share/man/man1
HOME=. $out/bin/waifuctl utils manpage $out/share/man/man1
gzip -r $out/share/man/man1
'';
};
};
defaultPackage = self.packages."${system}".waifuctl;
apps = {
unique-monster =
utils.lib.mkApp { drv = self.packages."${system}".unique-monster; };
waifud = utils.lib.mkApp { drv = self.packages."${system}".waifud; };
waifuctl =
utils.lib.mkApp { drv = self.packages."${system}".waifuctl; };
};
defaultApp = self.apps."${system}".waifuctl;
nixosModules = {
waifuctl = { ... }: {
environment.defaultPackages =
[ self.packages."${system}".waifuctl ];
};
waifud-common = { lib, ... }: {
users.groups.waifud = lib.mkDefault { };
users.users.waifud = {
createHome = true;
description = "waifud user";
isSystemUser = true;
group = "waifud";
home = "/var/lib/waifud";
};
};
waifud-host = { lib, pkgs, config, ... }:
with lib;
let cfg = config.xeserv.waifud;
in {
imports = [
self.nixosModules."${system}".waifud-common
self.nixosModules."${system}".waifuctl
];
config = {
systemd.services = {
waifud = {
wantedBy = [ "multi-user.target" ];
environment = {
RUST_LOG = "tower_http=debug,waifud=debug,info";
};
serviceConfig = {
User = "waifud";
Group = "waifud";
Restart = "always";
WorkingDirectory = "${self.packages."${system}".waifud}";
RestartSec = "30s";
ExecStart = "${waifud}/bin/waifud --config ${cfgDhall}";
};
};
};
};
};
waifud-runner = { pkgs, lib, config, ... }:
with lib;
let cfg = config.xeserv.waifud.runner;
in {
imports = [ self.nixosModules."${system}".waifud-common ];
options.xeserv.waifud.runner = with lib; {
parentDataset = mkOption {
type = types.str;
default = "rpool/local/vms";
description =
"the parent dataset to grant the waifud group zfs management access on";
};
sshKeys = mkOption {
type = with types; listOf str;
default = [ ];
description =
"the list of SSH public keys to allow waifud to ssh in as";
};
};
config = {
environment.defaultPackages = with pkgs; [ qemu zfs wget ];
virtualisation.libvirtd.enable = lib.mkDefault true;
systemd.services.waifud-runner-setup = {
wantedBy = [ "multi-user.target" ];
serviceConfig.Type = "oneshot";
script = ''
/run/current-system/sw/bin/zfs allow -g waifud create,destroy,mount,snapshot,rollback ${cfg.parentDataset}
'';
};
security.polkit.extraConfig = ''
/* Allow users in the waifud group to manage the libvirt daemon without authentication */
polkit.addRule(function(action, subject) {
if (action.id == "org.libvirt.unix.manage" && subject.isInGroup("waifud")) {
return polkit.Result.YES;
}
});
'';
users.users.waifud.openssh.authorizedKeys.keys = cfg.sshKeys;
security.sudo.extraRules = [{
groups = [ "waifud" ];
users = [ "waifud" ];
runAs = "root:root";
commands = [{
command = "/run/current-system/sw/bin/qemu-img";
options = [ "NOPASSWD" ];
}];
}];
};
};
};
devShell = with pkgs;
mkShell {
buildInputs = [
cargo
cargo-watch
rustc
rustfmt
rust-analyzer
pre-commit
rustPackages.clippy
openssl
pkg-config
sqlite-interactive
libvirt
dhall
dhall-json
jq
jo
deno
strace
];
DATABASE_URL = "./var/waifud.db";
RUST_LOG = "tower_http=trace,debug";
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};
});
}