2023-07-24 00:23:46 +00:00
|
|
|
(define-module (kulupu services gotosocial)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (guix gexp)
|
2023-07-24 00:23:46 +00:00
|
|
|
#:use-module ((guix licenses) #:prefix license:)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (guix records)
|
2023-07-23 09:27:04 +00:00
|
|
|
#:use-module (guix packages)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (gnu services)
|
2023-07-23 06:47:18 +00:00
|
|
|
#:use-module (gnu services certbot)
|
|
|
|
#:use-module (gnu services configuration)
|
|
|
|
#:use-module (gnu services databases)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (gnu services base)
|
|
|
|
#:use-module (gnu services shepherd)
|
2023-07-23 06:47:18 +00:00
|
|
|
#:use-module (gnu services web)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (gnu system shadow)
|
|
|
|
#:use-module (gnu packages admin)
|
2023-07-23 10:27:20 +00:00
|
|
|
#:use-module (gnu packages bash)
|
2023-07-24 00:23:46 +00:00
|
|
|
#:use-module (gnu packages base)
|
2023-07-24 05:46:16 +00:00
|
|
|
#:use-module (kulupu packages gotosocial)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:export (gotosocial-service-type
|
|
|
|
gotosocial-configuration))
|
2023-05-03 01:37:42 +00:00
|
|
|
|
2023-07-23 06:47:18 +00:00
|
|
|
(define-maybe string)
|
2023-05-03 01:37:42 +00:00
|
|
|
|
2023-07-23 06:47:18 +00:00
|
|
|
(define-configuration gotosocial-configuration
|
|
|
|
(host
|
|
|
|
(string "localhost")
|
|
|
|
"Name to use as gotosocial host")
|
|
|
|
(gotosocial
|
|
|
|
(package gotosocial)
|
|
|
|
"The gotosocial package to use")
|
2023-07-23 09:33:45 +00:00
|
|
|
(frontend
|
2023-07-23 06:47:18 +00:00
|
|
|
(package gotosocial-frontend)
|
|
|
|
"The gotosocial web asset package to use")
|
|
|
|
(config-file
|
|
|
|
(maybe-string)
|
|
|
|
"Path to configuration file, defaults to no configuration file")
|
|
|
|
(port
|
|
|
|
(integer 8080)
|
|
|
|
"Port to listen on, default 8080")
|
|
|
|
(work-dir
|
|
|
|
(string "/var/lib/gotosocial")
|
|
|
|
"GTS work directory")
|
|
|
|
(run-dir
|
|
|
|
(string "/var/run/gotosocial")
|
|
|
|
"GTS runtime directory")
|
2023-07-24 00:23:46 +00:00
|
|
|
(postgres?
|
|
|
|
(boolean #t)
|
|
|
|
"Set up postgres DB, use sqlite if false")
|
2023-07-24 06:38:00 +00:00
|
|
|
(nginx?
|
|
|
|
(boolean #t)
|
|
|
|
"Set up reverse proxy via nginx")
|
|
|
|
(https?
|
|
|
|
(boolean #t)
|
|
|
|
"Set up HTTPS via certbot")
|
2023-07-23 06:47:18 +00:00
|
|
|
(database-address
|
|
|
|
(string "/var/run/gotosocial/gts.db")
|
2023-07-23 09:27:04 +00:00
|
|
|
"Address of the database, default /var/run/gotosocial/gts.db")
|
|
|
|
(no-serialization))
|
2023-07-23 06:47:18 +00:00
|
|
|
|
|
|
|
(define (gotosocial-shepherd-service config)
|
2023-07-24 05:46:16 +00:00
|
|
|
(match-record config <gotosocial-configuration>
|
|
|
|
(gotosocial frontend config-file port work-dir run-dir host postgres? database-address)
|
|
|
|
(let* ((gts (file-append gotosocial "/bin/gotosocial")))
|
|
|
|
(list (shepherd-service
|
|
|
|
(documentation "Run GoToSocial")
|
2023-07-24 07:06:28 +00:00
|
|
|
(requirement `(networking ,@(if postgres? '(postgres) '())))
|
2023-07-24 05:46:16 +00:00
|
|
|
(provision '(gotosocial))
|
|
|
|
(start #~(make-forkexec-constructor
|
|
|
|
(list
|
|
|
|
(#$(file-append gotosocial "/bin/gotosocial")
|
|
|
|
"server" "start" "--syslog-enabled"
|
|
|
|
"--port=" (number->string #$port)
|
|
|
|
$#(when (not postgres?) "--db-type=sqlite")
|
|
|
|
"--db-address=" #$database-address
|
|
|
|
"--letsencrypt-cert-dir=" #$work-dir "/storage/certs"
|
|
|
|
"--storage-local-base-path=" #$work-dir "/storage"
|
|
|
|
"--web-asset-base-dir=" #$(file-append frontend "/gotosocial/web/assets")
|
|
|
|
"--web-template-base-dir=" #$(file-append frontend "/gotosocial/web/template")
|
|
|
|
"--host=" host))
|
|
|
|
#:user "gotosocial"
|
|
|
|
#:group "gotosocial"
|
|
|
|
#:directory #$work-dir))
|
|
|
|
(stop #~(make-kill-destructor)))))))
|
2023-07-23 06:47:18 +00:00
|
|
|
|
2023-07-24 05:46:16 +00:00
|
|
|
(define (gotosocial-postgresql-roles config)
|
2023-07-24 00:23:46 +00:00
|
|
|
(match-record config <gotosocial-configuration>
|
2023-07-24 05:46:16 +00:00
|
|
|
(postgres?)
|
2023-07-24 00:23:46 +00:00
|
|
|
(if postgres? (list (postgresql-role
|
|
|
|
(name "gotosocial")
|
|
|
|
(create-database? #t)))
|
|
|
|
'())))
|
2023-05-03 01:37:42 +00:00
|
|
|
|
2023-07-24 06:38:00 +00:00
|
|
|
(define (gotosocial-certbot config)
|
|
|
|
(match-record config <gotosocial-configuration>
|
|
|
|
(https? host nginx?)
|
|
|
|
(if (not https?) '()
|
|
|
|
(list (certificate-configuration
|
|
|
|
(domains (list host))
|
|
|
|
(deploy-hook (if nginx? %nginx-cert-deploy-hook %gotosocial-cert-deploy-hook)))))))
|
|
|
|
|
|
|
|
(define %nginx-cert-deploy-hook
|
|
|
|
(program-file
|
|
|
|
"nginx-cert-deploy-hook"
|
|
|
|
#~(let ((pid (call-with-input-file "/var/run/nginx/pid" read)))
|
|
|
|
(kill pid SIGHUP))))
|
|
|
|
|
|
|
|
(define %gotosocial-cert-deploy-hook
|
|
|
|
(program-file
|
|
|
|
"gotosocial-cert-deploy-hook"
|
|
|
|
(with-imported-modules '((guix build utils))
|
|
|
|
#~(begin (use-module (guix build utils))
|
|
|
|
(invoke "herd" "restart" "gotosocial")))))
|
|
|
|
|
|
|
|
(define (gotosocial-nginx config)
|
|
|
|
(match-record config <gotosocial-configuration>
|
|
|
|
(nginx? https? host run-dir)
|
|
|
|
(if (not nginx?) '()
|
|
|
|
(list (nginx-server-configuration
|
|
|
|
(listen (if https? '("443 ssl") '("80")))
|
|
|
|
(server-name (list host))
|
|
|
|
(ssl-certificate (if https? (string-append "/etc/letsencrypt/live/" host "/fullchain.pem") #f))
|
|
|
|
(ssl-certificate-key (if https? (string-append "/etc/letsencrypt/live/" host "/privkey.pem") #f))
|
|
|
|
(locations (list
|
|
|
|
(nginx-location-configuration
|
|
|
|
(uri "/")
|
|
|
|
(body `(,(string-append "proxy_pass http://unix:" run-dir "/gotosocial.socket;")
|
|
|
|
"proxy_set_header Host $host;"
|
|
|
|
"proxy_set_header X-Real_IP $remote_addr;"
|
|
|
|
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;"
|
|
|
|
"proxy_set_header X-Forwarded-Proto $scheme;"))))))))))
|
|
|
|
|
2023-07-24 06:53:59 +00:00
|
|
|
(define (gotosocial-activation config)
|
|
|
|
(match-record config <gotosocial-configuration>
|
|
|
|
(work-dir run-dir)
|
|
|
|
#~(begin
|
|
|
|
(use-modules (guix build utils)
|
|
|
|
(ice-9 string-fun))
|
|
|
|
(let* ((user (getpw "gotosocial"))
|
|
|
|
(user-id (passwd:uid user))
|
|
|
|
(group-id (passwd:gid user)))
|
|
|
|
(mkdir-p #$work-dir)
|
|
|
|
(mkdir-p #$run-dir)
|
|
|
|
(chown #$work-dir user-id group-id)
|
|
|
|
(chown #$run-dir user-id group-id)))))
|
|
|
|
|
2023-07-23 06:47:18 +00:00
|
|
|
(define (gotosocial-accounts config)
|
|
|
|
(match-record config <gotosocial-configuration>
|
|
|
|
(work-dir)
|
|
|
|
(list (user-group
|
2023-07-24 05:46:16 +00:00
|
|
|
(name "gotosocial")
|
|
|
|
(system? #t))
|
|
|
|
(user-account
|
|
|
|
(name "gotosocial")
|
|
|
|
(system? #t)
|
|
|
|
(group "gotosocial")
|
|
|
|
(comment "GoToSocial server user")
|
|
|
|
(home-directory work-dir)
|
|
|
|
(shell (file-append bash-minimal "/bin/bash"))))))
|
2023-05-03 01:37:42 +00:00
|
|
|
|
2023-07-24 05:46:16 +00:00
|
|
|
(define-public gotosocial-service-type
|
|
|
|
(service-type
|
|
|
|
(name 'gotosocial)
|
|
|
|
(extensions
|
|
|
|
(list (service-extension shepherd-root-service-type gotosocial-shepherd-service)
|
|
|
|
(service-extension account-service-type gotosocial-accounts)
|
2023-07-24 06:53:59 +00:00
|
|
|
(service-extension activation-service-type gotosocial-activation)
|
2023-07-24 06:38:00 +00:00
|
|
|
(service-extension nginx-service-type gotosocial-nginx)
|
|
|
|
(service-extension certbot-service-type gotosocial-certbot)
|
2023-07-24 05:46:16 +00:00
|
|
|
(service-extension postgresql-role-service-type gotosocial-postgresql-roles)))
|
|
|
|
(description "Runs GoToSocial")
|
|
|
|
(default-value (gotosocial-configuration))))
|