help-guix
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Home service that serializes into an executable file


From: Mario Forzanini
Subject: Home service that serializes into an executable file
Date: Tue, 28 Mar 2023 22:53:24 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Hello everyone!

I've recently tried to write a simple home service that serializes into
an ~/.xsession file. As someone pointed out in an older message on this
list, this file has to be executable; I used to achieve this including
an external file like: (local-file "xsession" #recursive? #t). Is there
a procedure that allows changing permission bits while generating a file
in the store?

This is still in a rough state, but here is the definition of the
service:

<snip>
    (define (serialize-string field-name val) val)

    (define (string-join strings delimiter)
      (if (null? strings)
          ""
          (fold (lambda (s so-far) (string-append so-far delimiter s))
                (car strings)
                (cdr strings))))

    (define (xsession-serialize-program program args)
      (if (pair? program)
          (string-append
           (string-join (cons* "try" (cdr program) args) " ")
           "\n")
          (string-append
           (string-join (cons* "try" (package-name program) args) " ")
           "\n")))

    (define (serialize-xsession-programs field-name alist)
      (generic-serialize-alist string-append
                               xsession-serialize-program
                               alist))

    (define (serialize-xsession-window-manager field-name window-manager)
      (string-append "exec " (cdr window-manager) "\n"))

    (define-configuration home-xsession-configuration
      (window-manager
       (pair `(,dwm . "dwm"))
       "Cons pair of window manager package and executable name."
       serialize-xsession-window-manager)
      (programs
       (alist '())
       "Alist of programs to start and arguments to such programs."
       serialize-xsession-programs)
      (log-file
       (string "$HOME/.local/share/xsession.log")
       "Log file to store errors and info."))

    (define (xsession-filter-fields field)
      (filter-configuration-fields home-xsession-configuration-fields (list 
field)))

    (define (xsession-serialize-field config field)
      (serialize-configuration config (xsession-filter-fields field)))

    (define (xsession-file config)
      (define dot-xsession
        (mixed-text-file "xsession"
         "#!/bin/sh
    try() {
    pgrep -q $1 || $@ 2>> "
         (xsession-serialize-field config 'log-file)
         "\n}\n"
         (xsession-serialize-field config 'programs)
         (xsession-serialize-field config 'window-manager)))
      `((".xsession" ,dot-xsession)))

    (define (add-xsession-packages config)
      (cons*
       (car (home-xsession-configuration-window-manager config))
       (map (lambda (association)
              (if (pair? (car association))
                  (caar association)
                  (car association)))
            (home-xsession-configuration-programs config))))

    (define home-xsession-service-type
      (service-type (name 'xsession)
                    (extensions
                     (list (service-extension
                            home-files-service-type
                            xsession-file)
                           (service-extension
                            home-profile-service-type
                            add-xsession-packages)))
                    (compose identity)
                    (default-value (home-xsession-configuration))
                    (description "Configure the X session via ~/.xsession.")))
<snap>

This serializes a configuration such as this:

<snip>
    (service home-xsession-service-type
                (home-xsession-configuration
                 (window-manager `(,mf-dwm . "dwm"))
                 (programs
                  `((,picom . ())
                    (,dunst . ())
                    ((,blueman . "blueman-applet") . ())
                    (,pasystray . ())
                    ((,network-manager-applet . "nm-applet") . ())
                    (,mpd . ())
                    (,cbatticon . ("BAT0"))
                    (,cbatticon . ("BAT1"))
                    (,xsettingsd . ())
                    (,tmux . ("new-session" "-d"))
                    (,hsetroot . ("-cover" "$HOME/Pictures/forest.jpg"))
                    (,setxkbmap . ("-layout" "us" "-option" "compose:ralt"))
                    ((,emacs-next . "emacs") . ("--daemon"))))
                 (log-file "~/.local/share/xsession.log")))
<snap>

into:

<snip>
    #!/bin/sh
    try() {
    pgrep -q $1 || $@ 2>> ~/.local/share/xsession.log
    }
    try picom
    try dunst
    try blueman-applet
    try pasystray
    try nm-applet
    try mpd
    try cbatticon BAT0
    try cbatticon BAT1
    try xsettingsd
    try tmux new-session -d
    try hsetroot -cover $HOME/Pictures/forest.jpg
    try setxkbmap -layout us -option compose:ralt
    try emacs --daemon
    exec dwm
<snap>

But the permissions are wrong:

<snip>
    ┌(22:49:18) 
    └(~) % ls -l ~/.xsession                                         
    lrwxrwxrwx 1 mario users 52 Mar 28 22:23 /home/mario/.xsession -> 
/gnu/store/p213clvznwrrrasr3mygb6iqqnqsg2ra-xsession
    ┌(22:49:21) 
    └(~) % ls -l /gnu/store/p213clvznwrrrasr3mygb6iqqnqsg2ra-xsession 
    -r--r--r-- 1 root root 342 Jan  1  1970 
/gnu/store/p213clvznwrrrasr3mygb6iqqnqsg2ra-xsession
<snap>

If you spot something wrong, let me know.

Thank you in advance :)
-- 
Mario



reply via email to

[Prev in Thread] Current Thread [Next in Thread]