~ruther/guix-exprs

ref: 75eae2a21ecb408498bff4d5582f93bef9ce15cd guix-exprs/modules/ruther/home/services/gtk.scm -rw-r--r-- 6.5 KiB
75eae2a2 — Rutherther chore: move modules to modules subfolder 15 days ago
                                                                                
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
(define-module (ruther home services gtk)
  #:use-module (gnu services configuration)
  #:use-module (gnu services)
  #:use-module (guix packages)
  #:use-module (guix gexp)
  #:use-module (guix build-system trivial)
  #:use-module (gnu home services)
  #:use-module (ruther home services dconf)
  #:export (home-gtk-configuration
            gtk-theme-configuration
            home-gtk-service-type))

(define (maybe-string? str)
  (or (nil? str)
      (string? str)))
(define (maybe-package? pkg)
  (or (nil? pkg)
      (package? pkg)))

(define-configuration/no-serialization gtk-theme-configuration
  (package (maybe-package #f) "The package to add to profile, including theme {name}")
  (name (maybe-string #f) "Name of the theme")
  (size (integer 16) "Size of the pointer. Applies only for cursor-theme"))

(define-configuration/no-serialization home-gtk-configuration
  (gtk-theme
   (gtk-theme-configuration (gtk-theme-configuration))
   "The theme")
  (icon-theme
   (gtk-theme-configuration (gtk-theme-configuration))
   "The icon theme")
  (cursor-theme
   (gtk-theme-configuration (gtk-theme-configuration))
   "The cursor theme")
  (font-name
   (maybe-string #f)
   "Name of the font to use"))

(define* (serialize-field name value #:key (gtk2? #f))
  (if gtk2?
      (format #f "~a = \"~a\"~%" name value)
      (format #f "~a = ~a~%" name value)))

(define* (serialize-cons field #:key (gtk2? #f))
  (serialize-field (car field) (cdr field) #:gtk2? gtk2?))

(define (gtk4-css-import-file theme-package theme-name)
  `("gtk.css"
    "@import url(\"file://" ,theme-package "/share/themes/" ,theme-name "/gtk-4.0/gtk.css\");"))

(define (map-gtk-configuration-to-gtk-config config)
  (filter (lambda (x) (not (nil? (cdr x))))
          `((gtk-theme-name . ,(gtk-theme-configuration-name (home-gtk-configuration-gtk-theme config)))
            (gtk-icon-theme-name . ,(gtk-theme-configuration-name (home-gtk-configuration-icon-theme config)))
            (gtk-cursor-theme-name . ,(gtk-theme-configuration-name (home-gtk-configuration-cursor-theme config)))
            (gtk-cursor-theme-size . ,(gtk-theme-configuration-size (home-gtk-configuration-cursor-theme config)))
            (gtk-font-name . ,(home-gtk-configuration-font-name config)))))

(define* (serialize-gtk-config config #:key (gtk2? #f))
  (let* ((mapped (map-gtk-configuration-to-gtk-config config))
        (serialized (map (lambda (x) (serialize-cons x #:gtk2? gtk2?)) mapped)))
    (if gtk2?
        serialized
        (cons* "[Settings]\n"
               serialized))))

(define (add-gtk-theme-packages config)
  (filter
   (lambda (x) (not (nil? x)))
   (list
    (gtk-theme-configuration-package (home-gtk-configuration-gtk-theme config))
    (gtk-theme-configuration-package (home-gtk-configuration-icon-theme config))
    (gtk-theme-configuration-package (home-gtk-configuration-cursor-theme config))
    (if (nil? (gtk-theme-configuration-name (home-gtk-configuration-cursor-theme config)))
        '()
        (package
         (name "default-icon-inherits")
         (version "0.0.0")
         (source #f)
         (build-system trivial-build-system)
         (home-page #f)
         (synopsis #f)
         (description #f)
         (license #f)
         (arguments
          (list
           #:builder
           (with-imported-modules
            '((guix build utils))
            #~(begin
                (use-modules (guix build utils))
                (mkdir-p (string-append #$output "/share/icons/default"))
                (call-with-output-file (string-append #$output "/share/icons/default/index.theme")
                  (lambda (port)
                    (format port
                            "[Icon Theme]~%Name=Default~%Comment=Default cursor theme~%Inherits=~a~%"
                            #$(gtk-theme-configuration-name (home-gtk-configuration-cursor-theme config))))))))))))))

(define (add-xcursor-environment config)
  (let* ((cursor-theme (home-gtk-configuration-cursor-theme config))
        (cursor-name (gtk-theme-configuration-name cursor-theme))
        (cursor-package (gtk-theme-configuration-package cursor-theme))
        (cursor-size (gtk-theme-configuration-size cursor-theme)))
    (if (nil? cursor-name)
        '()
        `(("GTK2_RC_FILES" . "$HOME/.gtkrc-2.0")
          ("XCURSOR_THEME" . ,cursor-name)
           ("XCURSOR_SIZE" . ,(format #f "~a" cursor-size)))))) ;; TODO: this path should not be hardcoded here

(define (add-gtk-config-file config)
  (append
   `((".gtkrc-2.0"
      ,(apply mixed-text-file (cons* "gtkrc-2.0" (serialize-gtk-config config #:gtk2? #t))))
     (".config/gtk-3.0/settings.ini"
      ,(apply mixed-text-file (cons* "settings.ini" (serialize-gtk-config config))))
     (".config/gtk-4.0/settings.ini"
      ,(apply mixed-text-file (cons* "settings.ini" (serialize-gtk-config config)))))
   (if (nil? (gtk-theme-configuration-package (home-gtk-configuration-gtk-theme config)))
       '()
       `((".config/gtk-4.0/gtk.css"
          ,(apply mixed-text-file (gtk4-css-import-file
                                   (gtk-theme-configuration-package (home-gtk-configuration-gtk-theme config))
                                   (gtk-theme-configuration-name (home-gtk-configuration-gtk-theme config)))))))))

(define (add-gtk-dconf-config config)
  (let* ((data `((font-name . ,(home-gtk-configuration-font-name config))
                 (gtk-theme . ,(gtk-theme-configuration-name (home-gtk-configuration-gtk-theme config)))
                 (cursor-theme . ,(gtk-theme-configuration-name (home-gtk-configuration-cursor-theme config)))
                 (icon-theme . ,(gtk-theme-configuration-name (home-gtk-configuration-icon-theme config)))))
         (filtered-data (filter (lambda (x) (not (nil? (cdr x)))) data)))
    `((org/gnome/desktop/interface
       ,filtered-data))))

(define-public home-gtk-service-type
  (service-type (name 'home-gtk)
                (extensions
                 (list (service-extension
                        home-files-service-type
                        add-gtk-config-file)
                       (service-extension
                        home-dconf-service-type
                        add-gtk-dconf-config)
                       (service-extension
                        home-profile-service-type
                        add-gtk-theme-packages)
                       (service-extension
                        home-environment-variables-service-type
                        add-xcursor-environment)))
                (description "Create gtk theme configuration files for gtk2 and gtk3")))
Do not follow this link