From 760b07c48133e1576d20175f2652b3094d1e04e5 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 4 Dec 2023 18:50:26 -0700 Subject: [PATCH] Added gitea service/package --- metznet/packages/gitea.scm | 77 ++++++++++++++++++++++++++++++++ metznet/services/gitea.scm | 91 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 metznet/packages/gitea.scm create mode 100644 metznet/services/gitea.scm diff --git a/metznet/packages/gitea.scm b/metznet/packages/gitea.scm new file mode 100644 index 0000000..975a627 --- /dev/null +++ b/metznet/packages/gitea.scm @@ -0,0 +1,77 @@ +(define-module (metznet packages gitea) + #:use-module ((guix licenses) #:prefix license:) + #:use-module (guix packages) + #:use-module (guix download) + #:use-module (guix gexp) + #:use-module (guix build-system go) + #:use-module (gnu packages bash) + #:use-module (gnu packages node) + #:use-module (gnu packages version-control)) + +(define-public gitea + (package + (name "gitea") + (version "1.16.9") + (source (origin + (method url-fetch) + (uri (string-append "https://github.com/go-gitea/gitea/releases" + "/download/v" version + "/gitea-src-" version ".tar.gz")) + (sha256 + (base32 "1q9hbg6fwi9gq8dwa9hi1giqz10h9y0xi0h03gjz0jci5i8xh4rg")))) + (build-system go-build-system) + (arguments + `(#:install-source? #f + #:phases + (modify-phases %standard-phases + (add-after 'patch-source-shebangs 'unpatch-example-shebangs + ;; If we don't do this then git repos created with this version of + ;; gitea will use the build environment's bash for the different + ;; git repo hooks. + (lambda _ + (substitute* + (find-files "src/integrations/gitea-repositories-meta" + "(\\.sample|gitea|(post|pre)-receive|update)") + (("#!/gnu/store/.*/bin/bash") "#!/bin/bash") + (("#!/gnu/store/.*/bin/sh") "#!/bin/sh")))) + (add-before 'build 'prepare-build + (lambda _ + (setenv "TAGS" "bindata sqlite sqlite_unlock_notify") + (setenv "GITEA_WORK_DIR" "/var/lib/gitea"))) + (replace 'build + (lambda _ + (with-directory-excursion "src" + (invoke "make" "build") + (invoke "make" "generate-manpage")))) + (replace 'check + (lambda* (#:key tests? #:allow-other-keys) + (when tests? + (unsetenv "GITEA_WORK_DIR") + (with-directory-excursion "src" + (invoke "make" "test-backend") + (invoke "make" "test-sqlite-migration"))))) + (replace 'install + (lambda* (#:key outputs #:allow-other-keys) + (let ((out (assoc-ref outputs "out"))) + (with-directory-excursion "src" + (invoke "make" "install") + (install-file "man/man1/gitea.1.gz" + (string-append out "/share/man/man1")))))) + (add-after 'install 'wrap-program + (lambda* (#:key outputs inputs #:allow-other-keys) + (let* ((out (assoc-ref outputs "out")) + (bin (string-append out "/bin/gitea"))) + (wrap-program bin + `("PATH" ":" prefix + (,(dirname (search-input-file inputs "/bin/git"))))))))))) + (inputs + (list bash-minimal + git)) + (home-page "https://gitea.io/") + (synopsis "Self-hosted git service") + (description "Gitea is an open-source forge software package for hosting +software development version control using Git as well as other collaborative +features like bug tracking, wikis and code review.") + (properties + '((release-monitoring-url . "https://github.com/go-gitea/gitea/releases"))) + (license license:expat))) diff --git a/metznet/services/gitea.scm b/metznet/services/gitea.scm new file mode 100644 index 0000000..1b138de --- /dev/null +++ b/metznet/services/gitea.scm @@ -0,0 +1,91 @@ +(define-module (metznet services gitea) + #:use-module (gnu services) + #:use-module (gnu services base) + #:use-module (gnu services admin) + #:use-module (gnu system shadow) + #:use-module (gnu packages admin) + #:use-module (gnu services shepherd) + #:use-module (gnu services configuration) + #:use-module (metznet packages gitea) + #:use-module (guix gexp) + #:use-module (gnu packages shells) + #:export (gitea-configuration gitea-configuation? gitea-service-type)) + +(define-maybe string) + +(define-configuration/no-serialization gitea-configuration + (gitea (file-like gitea) + "gitea package to use") + (work-path (string "/var/lib/gitea") + "Gitea's work path.") + (custom-path maybe-string + "Gitea's custom folder path.") + (config maybe-string + "Gitea configuration file path.") + (port (integer 3000) "gitea web port")) + +(define (gitea-activation configuration) + (let ((work-path (gitea-configuration-work-path configuration)) + (custom-path (gitea-configuration-custom-path configuration))) + #~(begin + (use-modules (guix build utils)) + (let ((user (getpwnam "gitea"))) + (mkdir-p #$work-path) + (chown #$work-path + (passwd:uid user) + (passwd:gid user)) + (when #$(maybe-value-set? custom-path) + (mkdir-p #$custom-path)))))) + +(define (gitea-accounts configuration) + (list (user-group + (name "gitea") + (system? #t)) + (user-account + (name "gitea") + (group "gitea") + (system? #t) + (comment "gitea server user") + (home-directory (gitea-configuration-work-path configuration)) + (shell (file-append shadow "/sbin/nologin"))))) + +(define (gitea-shepherd-service configuration) + (let* ((work-path (gitea-configuration-work-path configuration)) + (config-custom-path (gitea-configuration-custom-path configuration)) + (custom-path (if (maybe-value-set? config-custom-path) config-custom-path (string-append work-path "/custom"))) + (config-config-path (gitea-configuration-config configuration)) + (config-path (if (maybe-value-set? config-config-path) config-config-path (string-append custom-path "/conf/app.ini"))) + (port (gitea-configuration-port configuration))) + (list (shepherd-service (provision '(gitea)) + (documentation "Run `gitea web` as a daemon") + (requirement '(networking user-processes)) + (start #~(make-forkexec-constructor (list (string-append #$gitea + "/bin/gitea") + "--port" + #$(number->string + port) + "--work-path" + #$work-path + "--custom-path" + #$custom-path + "--config" + #$config-path) + #:environment-variables (list (string-append + "HOME=" + #$(gitea-configuration-work-path + configuration))) + #:user "gitea" + #:log-file "/var/log/gitea.log")) + (stop #~(make-kill-destructor)))))) + +(define-public gitea-service-type + (service-type (name 'gitea) + (extensions (list (service-extension + shepherd-root-service-type + gitea-shepherd-service) + (service-extension activation-service-type + gitea-activation) + (service-extension account-service-type + gitea-accounts))) + (default-value (gitea-configuration)) + (description "Run the Gitea Web server.")))