aboutsummaryrefslogtreecommitdiff
path: root/guix-module-dir/hydrilla-website.scm
blob: ce767840cc0e2a5179a1f5c18b13755b3fe2dea1 (about) (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
;; SPDX-License-Identifier: CC0-1.0

;; Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
;;
;; Available under the terms of Creative Commons Zero v1.0 Universal.

(define-module (hydrilla-website))

(use-modules
 (ice-9 rdelim)
 (ice-9 regex))

(define %source-dir
  (let* ((this-file (search-path %load-path "hydrilla-website.scm"))
         (proj-dir (dirname (dirname this-file))))
    (if (absolute-file-name? proj-dir)
        proj-dir
        (string-append (getcwd) "/" proj-dir))))

;; The PKG-INFO file is generated when running `python3 -m build -s` or similar.
;; It is also automatically included in the source release tarballs.
(define %pkg-info-path
  (string-append %source-dir "/src/hydrilla_website.egg-info/PKG-INFO"))

(define %website-version
  (if (access? %pkg-info-path R_OK)
      (call-with-input-file %pkg-info-path
        (lambda (port)
          (let ((process-line
                 (lambda (self-ref)
                   (let ((match-result
                          (string-match "^Version: (.*)" (read-line port))))
                     (if match-result (match:substring match-result 1)
                         (self-ref self-ref))))))
            (process-line process-line))))
      "unknown"))

(define source-tarball-name
  (string-append "hydrilla_website-" %website-version ".tar.gz"))

(use-modules
 (guix packages)
 (guix gexp)
 (guix build-system python)
 ((guix licenses) #:prefix license:)
 (gnu packages python-build)
 (gnu packages python-web)
 (gnu packages python-xyz)
 (gnu packages python-check))

(define-public hydrilla-website
  (package
   (name "hydrilla-website")
   (version %website-version)
   (source
    ;; setuptools_scm makes it impossible to build directly from git
    ;; checkout. We instead build from source tarball generated under ./dist/.
    (local-file (string-append %source-dir "/dist/" source-tarball-name)))
   (build-system python-build-system)
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-after 'unpack 'replace-wsgi.py
           (lambda* (#:key inputs outputs #:allow-other-keys)
             ;; In the wsgi.py file, embed the PYTHONPATH containing both the
             ;; dependencies and the python modules of this package. This will
             ;; make them available at runtime.
             (let ((pythonpath
                    (string-append (getenv "GUIX_PYTHONPATH")
                                   ":"
                                   (site-packages inputs outputs))))
               (substitute* "wsgi.py"
                 (("^from .* import .*" import-line)
                  (string-append
                   "# Make Guix-installed dependencies visible to Python.\n"
                   "import sys\n"
                   "sys.path.extend('" pythonpath "'.split(':'))\n"
                   "\n"
                   import-line))
                 (("^set_secret.*")
                  (string-append
                   "from pathlib import Path\n"
                   "path_to_secret = Path('/etc/hydrilla-website/secret.txt')\n"
                   "if path_to_secret.exists():\n"
                   "    set_secret(path_to_secret.read_text().strip())"))))))
         (add-after 'install 'install-wsgi-script
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (share-dir (string-append out "/share/hydrilla-website")))
               (mkdir-p share-dir)
               (copy-file "wsgi.py" (string-append share-dir "/wsgi.py"))))))))
   (propagated-inputs
    (list python-flask))
   (native-inputs
    (list python-setuptools-scm
          python-babel
          python-pypa-build
          python-mypy))
   (home-page "https://git.koszko.org/hydrilla-website")
   (synopsis "Website for Haketilo & Hydrilla")
   (description "This is the packaging of the website of Haketilo, a content
blocker + user script manager and Hydrilla, its script repository.")
   (license (list license:agpl3+ license:gpl3+ license:cc0))))