aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/piet.scm
blob: a09c05d9905f90fd408171690874d74499a45746 (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
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
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Jesse Gibbons <jgibbons2357+guix@gmail.com>
;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages piet)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix build-system gnu)
  #:use-module (gnu packages gd)
  #:use-module (gnu packages groff)
  #:use-module (gnu packages image)
  #:use-module (gnu packages netpbm)
  #:use-module (gnu packages perl)
  #:use-module (gnu packages tcl))

(define-public npiet
  (package
    (name "npiet")
    (version "1.3f")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://www.bertnase.de/npiet/npiet-"
                                  version ".tar.gz"))
              (sha256
               (base32
                "0nl59fhdqqr7nslxdirdn8nvlq5wws67c7jyx2ckbmxbc9h8bv9d"))))
    (build-system gnu-build-system)
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (add-after 'install 'wrap-binaries
           (lambda* (#:key outputs #:allow-other-keys)
             (let ((out (assoc-ref outputs "out")))
               (wrap-program (string-append out "/bin/npietedit")
                 `("PATH" ":" prefix (,(dirname (which "wish")))))
               #t))))))
    (inputs
     `(("gd" ,gd)
       ("giflib" ,giflib)
       ("libpng" ,libpng)
       ("tk" ,tk)))
    (native-inputs `(("groff" ,groff)))
    (synopsis "Piet interpreter")
    (description
     "Npiet is an interpreter for the Piet programming language.  Instead of
text, Piet programs are pictures.  Commands are determined based on changes in
color.

This package includes:
@enumerate
@item @command{npiet}, a Piet interpreter with debugging capabilities
@item @command{npiet-foogol}, a program that builds a Piet program from Foogol,
an Algol-like language
@item @command{npietedit}, an editor for Piet programs.
@end enumerate\n")
    (home-page "https://www.bertnase.de/npiet/")
    (license license:gpl2+)))

(define-public piet-toolchain
  (let ((commit "f002ff6a924a6bbace5eef94f3be06f425e7f590")
        (revision "0"))
    (package
      (name "piet-toolchain")
      (version (git-version "0.0.0" revision commit))
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "https://github.com/sl236/Piet")
               (commit commit)))
         (file-name (git-file-name name version))
         (sha256
          (base32 "0xwbhwizfbn080fmrgavaz3b939brycmlar3m5px9avl2b68c816"))
         (modules '((guix build utils)))
         (snippet
          '(begin
             ;; Remove a bundled fork of Marc Majcher's Piet interpreter.
             (delete-file-recursively "interpreter")
             #t))))
      (build-system gnu-build-system)
      (arguments
       `(#:modules ((guix build gnu-build-system)
                    (guix build utils)
                    (srfi srfi-26))
         #:phases
         (modify-phases %standard-phases
           (delete 'configure)          ; no configure script
           (delete 'build)              ; nothing to build
           (delete 'check)              ; run our own tests below
           (replace 'install
             (lambda* (#:key outputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (bin (string-append out "/bin"))
                      (doc (string-append out "/share/doc/"
                                          ,name "-" ,version)))
                 (for-each (lambda (script)
                             (install-file script bin)
                             (wrap-program (string-append bin "/" script)
                               `("PERL5LIB" ":" = (,(getenv "PERL5LIB")))))
                           (list "piet-assembler"
                                 "piet-compiler"))

                 ;; Fix an odd mode.
                 (chmod "compiler-samples/test-binary-ops.script" #o644)
                 (for-each (lambda (file)    ; INSTALL-FILE is not recursive
                             (copy-recursively file
                                               (string-append doc "/" file)))
                           (list "assembler-samples"
                                 "compiler-samples"
                                 "README.md")) ; includes the licence grant
                 #t)))
           (add-after 'install 'check
             (lambda* (#:key outputs tests? #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (bin (string-append out "/bin")))
                 (when tests?
                   (unsetenv "PERL5LIB") ; test the wrapping
                   ;; Compile all scripts assemble all Piets.
                   (for-each (lambda (file)
                               (system (string-append bin "/piet-compiler "
                                                      file ">"
                                                      file ".piet")))
                             (find-files "." "\\.script$"))
                   (for-each (lambda (file)
                               (system (string-append bin "/piet-assembler "
                                                      file "|pnmtopng>"
                                                      file ".png")))
                             (find-files "." "\\.piet$"))

                   ;; Don't run the interactive one.
                   (delete-file "assembler-samples/quest.piet.png")
                   (for-each (cut invoke "npiet" <>)
                             (find-files "." "\\.png$"))
                   #t)))))))
      (native-inputs
       ;; For our tests.
       `(("netpbm" ,netpbm)
         ("npiet" ,npiet)))
      (inputs
       `(("perl" ,perl)
         ("perl-parse-recdescent" ,perl-parse-recdescent)))
      (home-page "https://www.toothycat.net/wiki/wiki.pl?MoonShadow/Piet")
      (synopsis "Piet compiler and assembler")
      (description
       "This package provides a compiler and assembler that target the Piet
graphical programming language.

@command{piet-assembler} converts Piet assembler instructions (e.g.,
@code{push}, @code{add}, @code{switch}, @code{outn}) and directives into an
executable @code{netpbm} image of the corresponding Piet program.

@command{piet-compiler} compiles a C-like high-level language into assembly
source understood by @command{piet-assembler}.  It supports common arithmetic
and boolean logic operators (though not bitwise manipulation), flow control
(@code{if}, @code{for}, @code{while}), recursive functions, in-line assembler,
and input/output intrinsics.  The only supported data type is the integer.

The language is documented only by the compiler's Perl source code and the
included samples.")
      (license license:cc-by-sa4.0))))
ql-configuration-postgresql)))) (description "Run the PostgreSQL database server."))) (define-deprecated (postgresql-service #:key (postgresql postgresql-10) (port 5432) (locale "en_US.utf8") (config-file (postgresql-config-file)) (data-directory "/var/lib/postgresql/data") (extension-packages '())) postgresql-service-type "Return a service that runs @var{postgresql}, the PostgreSQL database server. The PostgreSQL daemon loads its runtime configuration from @var{config-file} and stores the database cluster in @var{data-directory}." (service postgresql-service-type (postgresql-configuration (postgresql postgresql) (port port) (locale locale) (config-file config-file) (data-directory data-directory) (extension-packages extension-packages)))) (define-record-type* <postgresql-role> postgresql-role make-postgresql-role postgresql-role? (name postgresql-role-name) ;string (permissions postgresql-role-permissions (default '(createdb login))) ;list (create-database? postgresql-role-create-database? ;boolean (default #f)) (encoding postgresql-role-encoding ;string (default "UTF8")) (collation postgresql-role-collation ;string (default "en_US.utf8")) (ctype postgresql-role-ctype ;string (default "en_US.utf8")) (template postgresql-role-template ;string (default "template1"))) (define-record-type* <postgresql-role-configuration> postgresql-role-configuration make-postgresql-role-configuration postgresql-role-configuration? (host postgresql-role-configuration-host ;string (default "/var/run/postgresql")) (log postgresql-role-configuration-log ;string (default "/var/log/postgresql_roles.log")) (roles postgresql-role-configuration-roles (default '()))) ;list (define (postgresql-create-roles config) ;; See: https://www.postgresql.org/docs/current/sql-createrole.html for the ;; complete permissions list. (define (format-permissions permissions) (let ((dict '(bypassrls createdb createrole login replication superuser))) (string-join (filter-map (lambda (permission) (and (member permission dict) (string-upcase (symbol->string permission)))) permissions) " "))) (define (roles->queries roles) (apply mixed-text-file "queries" (append-map (lambda (role) (match-record role <postgresql-role> (name permissions create-database? encoding collation ctype template) `("SELECT NOT(EXISTS(SELECT 1 FROM pg_catalog.pg_roles WHERE \ rolname = '" ,name "')) as not_exists;\n" "\\gset\n" "\\if :not_exists\n" "CREATE ROLE \"" ,name "\"" " WITH " ,(format-permissions permissions) ";\n" ,@(if create-database? `("CREATE DATABASE \"" ,name "\"" " OWNER \"" ,name "\"\n" " ENCODING '" ,encoding "'\n" " LC_COLLATE '" ,collation "'\n" " LC_CTYPE '" ,ctype "'\n" " TEMPLATE " ,template ";") '()) "\\endif\n"))) roles))) (let ((host (postgresql-role-configuration-host config)) (roles (postgresql-role-configuration-roles config))) #~(let ((psql #$(file-append postgresql "/bin/psql"))) (list psql "-a" "-h" #$host "-f" #$(roles->queries roles))))) (define (postgresql-role-shepherd-service config) (match-record config <postgresql-role-configuration> (log) (list (shepherd-service (requirement '(postgres)) (provision '(postgres-roles)) (one-shot? #t) (start #~(lambda args (let ((pid (fork+exec-command #$(postgresql-create-roles config) #:user "postgres" #:group "postgres" #:log-file #$log))) (zero? (cdr (waitpid pid)))))) (documentation "Create PostgreSQL roles."))))) (define postgresql-role-service-type (service-type (name 'postgresql-role) (extensions (list (service-extension shepherd-root-service-type postgresql-role-shepherd-service))) (compose concatenate) (extend (lambda (config extended-roles) (match-record config <postgresql-role-configuration> (host roles) (postgresql-role-configuration (host host) (roles (append roles extended-roles)))))) (default-value (postgresql-role-configuration)) (description "Ensure the specified PostgreSQL roles are created after the PostgreSQL database is started."))) ;;; ;;; Memcached ;;; (define-record-type* <memcached-configuration> memcached-configuration make-memcached-configuration memcached-configuration? (memcached memcached-configuration-memcached ;file-like (default memcached)) (interfaces memcached-configuration-interfaces (default '("0.0.0.0"))) (tcp-port memcached-configuration-tcp-port (default 11211)) (udp-port memcached-configuration-udp-port (default 11211)) (additional-options memcached-configuration-additional-options (default '()))) (define %memcached-accounts (list (user-group (name "memcached") (system? #t)) (user-account (name "memcached") (group "memcached") (system? #t) (comment "Memcached server user") (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin"))))) (define memcached-activation #~(begin (use-modules (guix build utils)) (let ((user (getpwnam "memcached"))) (mkdir-p "/var/run/memcached") (chown "/var/run/memcached" (passwd:uid user) (passwd:gid user))))) (define memcached-shepherd-service (match-lambda (($ <memcached-configuration> memcached interfaces tcp-port udp-port additional-options) (list (shepherd-service (provision '(memcached)) (documentation "Run the Memcached daemon.") (requirement '(user-processes loopback)) (start #~(make-forkexec-constructor `(#$(file-append memcached "/bin/memcached") "-l" #$(string-join interfaces ",") "-p" #$(number->string tcp-port) "-U" #$(number->string udp-port) "--daemon" ;; Memcached changes to the memcached user prior to ;; writing the pid file, so write it to a directory ;; that memcached owns. "-P" "/var/run/memcached/pid" "-u" "memcached" ,#$@additional-options) #:log-file "/var/log/memcached" #:pid-file "/var/run/memcached/pid")) (stop #~(make-kill-destructor))))))) (define memcached-service-type (service-type (name 'memcached) (extensions (list (service-extension shepherd-root-service-type memcached-shepherd-service) (service-extension activation-service-type (const memcached-activation)) (service-extension account-service-type (const %memcached-accounts)))) (default-value (memcached-configuration)) (description "Run @command{memcached}, a daemon that provides an in-memory caching service, intended for use by dynamic web applications."))) ;;; ;;; MySQL. ;;; (define-record-type* <mysql-configuration> mysql-configuration make-mysql-configuration mysql-configuration? (mysql mysql-configuration-mysql (default mariadb)) (bind-address mysql-configuration-bind-address (default "127.0.0.1")) (port mysql-configuration-port (default 3306)) (socket mysql-configuration-socket (default "/run/mysqld/mysqld.sock")) (datadir mysql-configuration-datadir (default "/var/lib/mysql")) (extra-content mysql-configuration-extra-content (default "")) (extra-environment mysql-configuration-extra-environment (default #~'())) (auto-upgrade? mysql-configuration-auto-upgrade? (default #t))) (define %mysql-accounts (list (user-group (name "mysql") (system? #t)) (user-account (name "mysql") (group "mysql") (system? #t) (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin"))))) (define mysql-configuration-file (match-lambda (($ <mysql-configuration> mysql bind-address port socket datadir extra-content) (mixed-text-file "my.cnf" "[mysqld] datadir=" datadir " socket=" socket " bind-address=" bind-address " port=" (number->string port) " " extra-content " ")))) (define (mysqld-wrapper config) "Start mysqld, and initialize the system tables if necessary." (program-file "mysqld-wrapper" (with-imported-modules (source-module-closure '((guix build utils))) (let ((mysql (mysql-configuration-mysql config)) (datadir (mysql-configuration-datadir config)) (my.cnf (mysql-configuration-file config))) #~(begin (use-modules (guix build utils)) (let* ((mysqld (string-append #$mysql "/bin/mysqld")) (user (getpwnam "mysql")) (uid (passwd:uid user)) (gid (passwd:gid user)) (rundir "/run/mysqld")) (mkdir-p #$datadir) (chown #$datadir uid gid) (mkdir-p rundir) (chown rundir uid gid) (unless (file-exists? (string-append #$datadir "/mysql")) (let ((init (system* #$(mysql-install config)))) (unless (= 0 (status:exit-val init)) (throw 'system-error "MySQL initialization failed.")))) ;; Drop privileges and start the server. (setgid gid) (setuid uid) (execl mysqld mysqld (string-append "--defaults-file=" #$my.cnf)))))))) (define (mysql-shepherd-service config) (list (shepherd-service (provision '(mysql)) (requirement '(user-processes)) (documentation "Run the MySQL server.") (actions (list (shepherd-configuration-action (mysql-configuration-file config)))) (start (let ((mysql (mysql-configuration-mysql config)) (extra-env (mysql-configuration-extra-environment config)) (my.cnf (mysql-configuration-file config))) #~(make-forkexec-constructor (list #$(mysqld-wrapper config)) #:log-file "/var/log/mysqld.log" #:environment-variables #$extra-env))) (stop #~(make-kill-destructor))))) (define (mysql-install config) "Install MySQL system database and secure the installation." (let ((mysql (mysql-configuration-mysql config)) (my.cnf (mysql-configuration-file config))) (program-file "mysql-install" (with-imported-modules (source-module-closure '((guix build utils))) #~(begin (use-modules (guix build utils)) ;; Make sed, mkdir, uname, etc available for mariadb-install-db. (set-path-environment-variable "PATH" '("bin") (list #$sed #$coreutils)) (if (string=? "mariadb" #$(package-name mysql)) ;; For MariaDB. (system* #$(file-append mysql "/bin/mariadb-install-db") (string-append "--defaults-file=" #$my.cnf) "--skip-test-db" "--user=mysql") ;; For MySQL. (system* #$(file-append mysql "/bin/mysqld") (string-append "--defaults-file=" #$my.cnf) "--initialize" "--user=mysql"))))))) (define (mysql-upgrade-wrapper config) ;; The MySQL socket and PID file may appear before the server is ready to ;; accept connections. Ensure the socket is responsive before attempting ;; to run the upgrade script. (let ((mysql (mysql-configuration-mysql config)) (socket-file (mysql-configuration-socket config)) (config-file (mysql-configuration-file config))) (program-file "mysql-upgrade-wrapper" #~(begin (let ((mysql-upgrade #$(file-append mysql "/bin/mysql_upgrade")) (timeout 20)) (begin (let loop ((i 0)) (catch 'system-error (lambda () (let ((sock (socket PF_UNIX SOCK_STREAM 0))) (connect sock AF_UNIX #$socket-file) (close-port sock) ;; The socket is ready! (execl mysql-upgrade mysql-upgrade (string-append "--defaults-file=" #$config-file) "--user=mysql"))) (lambda args (if (< i timeout) (begin (sleep 1) (loop (+ 1 i))) ;; No luck, give up. (throw 'timeout-error "MySQL server did not appear in time!"))))))))))) (define (mysql-upgrade-shepherd-service config) (list (shepherd-service (provision '(mysql-upgrade)) (requirement '(mysql)) (one-shot? #t) (documentation "Upgrade MySQL database schemas.") (start #~(make-forkexec-constructor (list #$(mysql-upgrade-wrapper config)) #:user "mysql" #:group "mysql" #:log-file "/var/log/mysql_upgrade.log"))))) (define (mysql-shepherd-services config) (let ((mysql-services (mysql-shepherd-service config))) (if (mysql-configuration-auto-upgrade? config) (append mysql-services (mysql-upgrade-shepherd-service config)) mysql-services))) (define mysql-service-type (service-type (name 'mysql) (extensions (list (service-extension account-service-type (const %mysql-accounts)) (service-extension shepherd-root-service-type mysql-shepherd-services))) (default-value (mysql-configuration)) (description "Run the MySQL or MariaDB database server, @command{mysqld}."))) (define-deprecated (mysql-service #:key (config (mysql-configuration))) mysql-service-type (service mysql-service-type config)) ;;; ;;; Redis ;;; (define-record-type* <redis-configuration> redis-configuration make-redis-configuration redis-configuration? (redis redis-configuration-redis ;file-like (default redis)) (bind redis-configuration-bind (default "127.0.0.1")) (port redis-configuration-port (default 6379)) (working-directory redis-configuration-working-directory (default "/var/lib/redis")) (config-file redis-configuration-config-file (default #f))) (define (default-redis.conf bind port working-directory) (mixed-text-file "redis.conf" "bind " bind "\n" "port " (number->string port) "\n" "dir " working-directory "\n" "daemonize no\n")) (define %redis-accounts (list (user-group (name "redis") (system? #t)) (user-account (name "redis") (group "redis") (system? #t) (comment "Redis server user") (home-directory "/var/empty") (shell (file-append shadow "/sbin/nologin"))))) (define redis-activation (match-lambda (($ <redis-configuration> redis bind port working-directory config-file) #~(begin (use-modules (guix build utils) (ice-9 match)) (let ((user (getpwnam "redis"))) (mkdir-p #$working-directory) (chown #$working-directory (passwd:uid user) (passwd:gid user))))))) (define redis-shepherd-service (match-lambda (($ <redis-configuration> redis bind port working-directory config-file) (let ((config-file (or config-file (default-redis.conf bind port working-directory)))) (list (shepherd-service (provision '(redis)) (documentation "Run the Redis daemon.") (requirement '(user-processes syslogd)) (actions (list (shepherd-configuration-action config-file))) (start #~(make-forkexec-constructor '(#$(file-append redis "/bin/redis-server") #$config-file) #:user "redis" #:group "redis")) (stop #~(make-kill-destructor)))))))) (define redis-service-type (service-type (name 'redis) (extensions (list (service-extension shepherd-root-service-type redis-shepherd-service) (service-extension activation-service-type redis-activation) (service-extension account-service-type (const %redis-accounts)))) (default-value (redis-configuration)) (description "Run Redis, a caching key/value store.")))