summaryrefslogtreecommitdiff
path: root/epistemia/systems/demiurge.scm
blob: 77e681e7c6881e00e95314057c092f7f166575e2 (plain)
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
(define-module (epistemia systems demiurge)
  #:use-module (gnu)
  #:use-module (gnu packages bash)
  #:use-module (gnu packages emacs)
  #:use-module (gnu packages golang-crypto)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages music)
  #:use-module (gnu packages version-control)
  #:use-module (gnu packages vpn)
  #:use-module (gnu services dbus) ; temp
  #:use-module (gnu services desktop)
  #:use-module (gnu services dns)
  #:use-module (gnu services containers) ; temp
  #:use-module (gnu services linux)
  #:use-module (gnu services networking)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services sound)
  #:use-module (gnu services ssh)
  #:use-module (gnu services sysctl)
  #:use-module (gnu services vpn)
  #:use-module (gnu services web)
  #:use-module (gnu system accounts)
  #:use-module (guix)
  #:use-module (guix gexp)
  #:use-module (guix transformations)
  #:use-module (guix utils)
  #:use-module (nongnu packages linux)
  #:use-module (nongnu system linux-initrd)
  #:use-module (epistemia bootloader)
  #:use-module (epistemia systems base-system)
  #:use-module (epistemia systems linux-initrd)
  #:use-module (epistemia services age-secret)
  #:use-module (epistemia services machine-learning)
  #:use-module (epistemia services zfs)
  #:use-module (epistemia packages machine-learning)
  #:use-module (epistemia packages zfs))

(define (nginx-reverse-proxy domain proxy-port)
  (nginx-server-configuration
   (server-name (list domain))
   (listen '("10.0.13.2:443" "[fd00:b0ba:cafe:babe::2]:443" "10.0.13.2:80" "[fd00:b0ba:cafe:babe::2]:80"))
   (ssl-certificate "/var/demiurge.epistemia.crt")
   (ssl-certificate-key "/var/demiurge.epistemia.key")
   (locations
    (list
     (nginx-location-configuration
      (uri "/")
      (body (list (string-append "proxy_pass http://127.0.0.1:" (number->string proxy-port) ";")
		  "proxy_set_header Upgrade $http_upgrade;"
		  "proxy_set_header Connection \"upgrade\";")))))))

(operating-system
 (inherit base-system)
 (host-name "demiurge.epistemia")

 (users (cons (user-account
               (name "vin")
               (comment "Vineet")
	       (shell (file-append bash "/bin/bash"))
               (group "users")
               (supplementary-groups '("wheel" "audio" "video" "input" "seat" "tty" "cgroup" "kvm"))) ;; TODO remove docker
	      %base-user-accounts))

 (kernel linux-6.18)
 (kernel-arguments (list "net.ifnames=0"
			 "modprobe.blacklist=snd_hda_intel"
			 "amdgpu.gfxoff=0"))
 (kernel-loadable-modules (list (list zfs-linux "module")))

 (initrd (lambda (file-systems . args)
           (apply microcode-initrd file-systems
                  #:initrd epistemia-zfs-initrd
                  #:zfs-package zfs-linux
                  args)))
 (firmware (cons* amdgpu-firmware %base-firmware))

 (bootloader (bootloader-configuration
	      (bootloader grub-zfs)
	      (targets '("/boot/efi"))))

 (file-systems (append
                (list (file-system
		       (device "zroot/root")
		       (mount-point "/")
		       (type "zfs")
		       (check? #f)
		       (needed-for-boot? #t))
                      (file-system
		       (device "zroot/gnu")
		       (mount-point "/gnu")
		       (type "zfs")
		       (check? #f)
		       (needed-for-boot? #t))
                      (file-system
		       (device "zroot/var")
		       (mount-point "/var")
		       (type "zfs")
		       (check? #f)
		       (needed-for-boot? #t))
                      (file-system
		       (device "zroot/home")
		       (mount-point "/home")
		       (type "zfs")
		       (check? #f)
		       (needed-for-boot? #t))
                      (file-system
		       (device (uuid "F618-FA9F" 'fat))
		       (mount-point "/boot/efi")
		       (type "vfat")))
                %base-file-systems))

 (packages (append (list emacs-no-x
			 git
			 zfs-linux)
                   %base-packages))

 (services (modify-services (append (list (service age-secret-service-type
						   (age-secret-configuration
						    (secrets (list
							      (age-secret
							       (name "wg0_demiurge")
							       (file (local-file "../secrets/wg0_demiurge.age")))
							      (age-secret
							       (name "wg1_demiurge")
							       (file (local-file "../secrets/wg1_demiurge.age")))))))
					  (simple-service 'zfs-loader
							  kernel-module-loader-service-type
							  '("zfs"))
					  (simple-service 'zfs-shepherd-services
							  shepherd-root-service-type
							  zfs-shepherd-services)
					  (simple-service 'zfs-shepherd-services-user-processes
							  user-processes-service-type
							  '(zfs-automount))
					  (simple-service 'gpu-power-cap
							  shepherd-root-service-type
							  (list (shepherd-service
								  (provision '(gpu-power-cap))
								  (requirement '(udev))
								  (one-shot? #t)
								  (documentation "Set GPU power cap on boot")
								  (start #~(make-forkexec-constructor
									    (list #$(file-append bash "/bin/bash") "-c"
										  (string-append
										   "echo 294000000 > /sys/class/drm/card0/device/hwmon/hwmon*/power1_cap; "
										   "echo 294000000 > /sys/class/drm/card1/device/hwmon/hwmon*/power1_cap"))))
								  (stop #~(make-kill-destructor)))))
					  (service seatd-service-type)
					  (service iptables-service-type)
					  (service static-networking-service-type
						   (list (static-networking
							  (addresses
							   (list (network-address
								  (device "eth0")
								  (value "192.168.1.2/24"))))
							  (routes
							   (list (network-route
								  (destination "default")
								  (gateway "192.168.1.1")))))))
					  (simple-service 'resolv-configuration
							  etc-service-type
							  `(("resolv.conf"
							     ,(plain-file "epistemia_resolv.conf"
									  (string-append
									   "nameserver ::1\n"
									   "search epistemia\n")))))
					  (service openssh-service-type
						   (openssh-configuration
						    (permit-root-login #f)
						    (authorized-keys
						     `(("vin" ,(local-file "/data/ssh/id_ed25519.pub"))
						       ("root" ,(local-file "/data/ssh/id_ed25519.pub"))))))
					  (service unbound-service-type
						   (unbound-configuration
						    (server
						     (unbound-server
						      (interface '("::1"))
						      (hide-version #t)
						      (hide-identity #t)
						      (extra-options '((ip-freebind . "yes" )
								       (aggressive-nsec . "no")))))
						    (forward-zone
						     (list
						      (unbound-zone
						       (name ".")
						       (forward-addr '("10.0.13.1")))))))
					  (service wireguard-service-type
						   (wireguard-configuration
						    (interface "wg0")
						    (addresses '("172.26.55.213/32" "fd00:0000:1337:cafe:1111:1111:4975:4c1d/128"))
						    (port 51821)
						    (bootstrap-private-key? #f)
						    (private-key "/run/secrets/wg0_demiurge")
						    (pre-up '("while [ ! -f /run/secrets/wg0_demiurge ]; do sleep 1; done"
							      "ip route replace 185.157.162.7 via 192.168.1.1 dev eth0"))
						    (post-up
						     (list "ip rule add fwmark 0x1 table 100 priority 100"
							   "ip rule add fwmark 51820 table 100 priority 100"
							   "ip route replace default via 192.168.1.1 dev eth0 table 100"
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -A PREROUTING -i eth0 -d 192.168.1.2 -m conntrack --ctstate NEW -j CONNMARK --set-mark 0x1")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -A PREROUTING -i eth0 -j CONNMARK --restore-mark")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark")))
						    (post-down
						     (list "ip route del 185.157.162.7 via 192.168.1.1 dev eth0"
							   "ip rule del fwmark 0x1 table 100"
							   "ip rule del fwmark 51820 table 100"
							   "ip route flush table 100"
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -D PREROUTING -i eth0 -d 192.168.1.2 -m conntrack --ctstate NEW -j CONNMARK --set-mark 0x1")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -D PREROUTING -i eth0 -j CONNMARK --restore-mark")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -t mangle -D OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j CONNMARK --restore-mark")))
						    (peers
						     (list
						      (wireguard-peer
						       (name "ovpn-ams-29")
						       (public-key "wTlz3u1egzUrx4Ho02cL7Ht1BBa/C8bULzXStn5f+zk=")
						       (allowed-ips '("0.0.0.0/0" "::0/0"))
						       (endpoint "185.157.162.7:9929"))))))
					  (service wireguard-service-type
						   (wireguard-configuration
						    (interface "wg1")
						    (addresses '("10.0.13.2/24" "fd00:b0ba:cafe:babe::2/64 "))
						    (port 51820)
						    (bootstrap-private-key? #f)
						    (private-key "/run/secrets/wg1_demiurge")
						    (table #f)
						    (shepherd-requirement '(age-secret))
						    (pre-up '("while [ ! -f /run/secrets/wg1_demiurge ]; do sleep 1; done"))
						    (post-up
						     ;; IPv4 and IPv6 NAT
						     (list #~(string-append #$(file-append iptables "/sbin/iptables") " -t nat -I POSTROUTING 1 -s 10.0.13.4/32 -o wg0 -j MASQUERADE")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -t nat -I POSTROUTING 1 -s fd00:b0ba:cafe:babe::4/128 -o wg0 -j MASQUERADE")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -N WG1-FWD")

							   ;; IPv4 Forwarding Chain
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -I FORWARD 1 -j WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -A WG1-FWD -i wg1 -o wg1 -j ACCEPT")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -A WG1-FWD -i wg1 -o wg0 -j ACCEPT")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -A WG1-FWD -i wg0 -o wg1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT")

							   ;; IPv6 Forwarding Chain
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -N WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -I FORWARD 1 -j WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -A WG1-FWD -i wg1 -o wg1 -j ACCEPT")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -A WG1-FWD -i wg1 -o wg0 -j ACCEPT")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -A WG1-FWD -i wg0 -o wg1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT")))
						    (pre-down
						     ;; IPv4 and IPv6 NAT
						     (list #~(string-append #$(file-append iptables "/sbin/iptables") " -t nat -D POSTROUTING -s 10.0.13.4/32 -o wg0 -j MASQUERADE")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -t nat -D POSTROUTING -s fd00:b0ba:cafe:babe::4/128 -o wg0 -j MASQUERADE")

							   ;; IPv4 Forwarding Chain
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -D FORWARD -j WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -F WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/iptables") " -X WG1-FWD")

							   ;; IPv6 Forwarding Chain
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -D FORWARD -j WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -F WG1-FWD")
							   #~(string-append #$(file-append iptables "/sbin/ip6tables") " -X WG1-FWD")))
						    (peers
						     (list
						      (wireguard-peer
						       (name "saklas.epistemia")
						       (public-key "XvRailvccuc7LJIF4aaYM/MLkU4upiprwFlCfBllhl0=")
						       (allowed-ips '("10.0.13.1/32" "fd00:b0ba:cafe:babe::1/128"))
						       (endpoint "141.95.52.111:51820")
						       (keep-alive 25))
						      (wireguard-peer
						       (name "hastur.epistemia")
						       (public-key "1ketYziRhoUmpbrj/60O5DYbcPacvmEoFQqa/NntSnc=")
						       (allowed-ips '("10.0.13.3/32" "fd00:b0ba:cafe:babe::3/128"))
						       (keep-alive 25))
						      (wireguard-peer
						       (name "iphonebob.epistemia")
						       (public-key "Xn0EmeRZdpMejBgzr98mYtb/2f5O58GAzQLNZV/SS30=")
						       (allowed-ips '("10.0.13.4/32" "fd00:b0ba:cafe:babe::4/128"))
						       (keep-alive 25))
						      (wireguard-peer
						       (name "lab.epistemia")
						       (public-key "iMDEwvXjPAlQH8ZCmP63FM5ICYIFIX5XIyGxjnXoNVE=")
						       (allowed-ips '("10.0.13.5/32" "fd00:b0ba:cafe:babe::5/128"))
						       (keep-alive 25))))))
					  (simple-service 'wireguard-route-timer
							  shepherd-root-service-type
							  (list (shepherd-timer
								 '(wg1-route-updater)
								 "* * * * *"
								 #~("/data_hdd/wireguard-endpoint-route")
								 #:requirement '(wireguard-wg1)
								 #:documentation "Update Wireguard routes every minute.")))
					  (service nginx-service-type
						   (nginx-configuration
						    (server-blocks
						     (list
						      (nginx-reverse-proxy "llm.demiurge.epistemia" 8080)
						      (nginx-reverse-proxy "searx.demiurge.epistemia" 8081)
						      (nginx-reverse-proxy "redlib.demiurge.epistemia" 8085)
						      (nginx-reverse-proxy "navidrome.demiurge.epistemia" 4533)
						      (nginx-reverse-proxy "sdui.demiurge.epistemia" 5000)
						      (nginx-reverse-proxy "home.demiurge.epistemia" 8123)))))
					  (service pam-limits-service-type
						   (list (pam-limits-entry "@audio" 'both 'rtprio 99)
							 (pam-limits-entry "@audio" 'both 'memlock 'unlimited)))
					  (simple-service 'llm-router
							  shepherd-root-service-type
							  (list (shepherd-service
								  (provision '(llm-router))
								  (requirement '(user-processes networking guix-daemon))
								  (documentation "llm-router service")
								  (start #~(make-forkexec-constructor
									    (list "/home/vin/.config/guix/current/bin/guix" "shell"
										  "-m" "/data/src/public/llm-router/manifest.scm"
										  "--" "python3" "app.py")
									    #:user "vin"
									    #:directory "/data/src/public/llm-router"
									    #:log-file "/var/log/llm-router.log"))
								  (stop #~(make-kill-destructor)))))
					  (simple-service 'sdui
							  shepherd-root-service-type
							  (list (shepherd-service
								  (provision '(sdui))
								  (requirement '(user-processes networking guix-daemon))
								  (documentation "sdui service")
								  (start #~(make-forkexec-constructor
									    (list "/home/vin/.config/guix/current/bin/guix" "shell"
										  "-m" "/data/src/public/sdui/manifest.scm"
										  "--" "python3" "app.py")
									    #:user "vin"
									    #:directory "/data/src/public/sdui"
									    #:log-file "/var/log/sdui.log"))
								  (stop #~(make-kill-destructor)))))
					  ;; netx three temporary
					  (service dbus-root-service-type)
					  (service rootless-podman-service-type
						   (rootless-podman-configuration
						    (subgids
						     (list (subid-range (name "vin"))))
						    (subuids
						     (list (subid-range (name "vin"))))))
					  (service oci-service-type
						   (oci-configuration
						    (runtime 'podman)
						    (containers
						     (list
						      (oci-container-configuration
						       (image "deluan/navidrome:latest")
						       (user "vin")
						       (ports '(("4533" . "4533")))
						       (environment '("ND_ENABLEINSIGHTCOLLECTOR=false"))
						       (volumes '("/data_hdd/music_navidrome:/data"
								  "/data_hdd/music:/music:ro"))))))))
				    %base-services)
			    (sysctl-service-type config =>
						 (sysctl-configuration
						  (settings
						   (append
						    '(("net.ipv4.ip_forward" . "1")
						      ("net.ipv6.conf.all.forwarding" . "1"))
						    %default-sysctl-settings)))))))