about summary refs log tree commit diff
path: root/pkgs/development/lisp-modules-new-obsolete/import/nix.lisp
blob: c6de5a4c9932de2a2c1c192a57d32b196b8bdcc7 (plain) (blame)
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
(defpackage org.lispbuilds.nix/nix
  (:documentation "Utilities for generating Nix code")
  (:use :cl)
  (:import-from :str)
  (:import-from :ppcre)
  (:import-from :arrow-macros :->>)
  (:import-from :org.lispbuilds.nix/util :replace-regexes)
  (:export
   :nix-eval
   :system-master
   :nixify-symbol
   :make-pname
   :*nix-attrs-depth*))

(in-package org.lispbuilds.nix/nix)

;; Path names are alphanumeric and can include the symbols +-._?= and
;; must not begin with a period.
(defun make-pname (string)
  (replace-regexes '("^[.]" "[^a-zA-Z0-9+-._?=]")
                   '("_" "_")
                   string))

(defun system-master (system)
  (first (str:split "/" system)))

;;;; Nix generation

(defun nix-eval (exp)
  (assert (consp exp))
  (ecase (car exp)
    (:string (nix-string (cadr exp)))
    (:list (apply #'nix-list (rest exp)))
    (:funcall (apply #'nix-funcall (rest exp)))
    (:attrs (nix-attrs (cdr exp)))
    (:merge (apply #'nix-merge (cdr exp)))
    (:symbol (nix-symbol (cadr exp)))))

(defun nix-string (object)
  (format nil "\"~a\"" object))

(defun nixify-symbol (string)
  (flet ((fix-special-chars (str)
           (replace-regexes '("[+]$" "[+][/]" "[+]" "[.]" "[/]")
                            '("_plus" "_plus/" "_plus_" "_dot_" "_slash_")
                            str)))
    (if (ppcre:scan "^[0-9]" string)
        (str:concat "_" (fix-special-chars string))
        (fix-special-chars string))))


(defun nix-symbol (object)
  (nixify-symbol (format nil "~a" object)))

(defun nix-list (&rest things)
  (format nil "[ ~{~A~^ ~} ]" (mapcar 'nix-eval things)))
(defvar *nix-attrs-depth* 0)

(defun nix-attrs (keyvals)
  (let ((*nix-attrs-depth* (1+ *nix-attrs-depth*)))
    (format
     nil
     (->> "{~%*depth*~{~{~A = ~A;~}~^~%*depth*~}~%*depth-1*}"
          (str:replace-all "*depth*" (str:repeat *nix-attrs-depth* "  "))
          (str:replace-all "*depth-1*" (str:repeat (1- *nix-attrs-depth*) "  ")))
     (mapcar (lambda (keyval)
               (let ((key (car keyval))
                     (val (cadr keyval)))
                 (list (nix-symbol key)
                       (nix-eval val))))
             keyvals))))

(defun nix-funcall (fun &rest args)
  (format nil "(~a ~{~a~^ ~})"
          (nixify-symbol fun)
          (mapcar 'nix-eval args)))

(defun nix-merge (a b)
  (format nil "(~a // ~b)"
          (nix-eval a)
          (nix-eval b)))