Subject: Re: [bug#68163] [PATCH] gnu: Prevent stale cache use when `%package-module-path' is parameterized.

From: antlers <[email-masked]>
Date: Mon, 08 Jan 2024 23:04:41 +0000

So I did! It's probably better to give a clear, in-line example anyways.

I take it you know *what* I'm doing, just not *why*; but I'm going to
include a full example anyway for the benefit of future readers.

Note that this is a compile-time error, so profiles containing such
code fail to build entirely but produce cached object files which may
continue to function correctly after eg. a roll-back. I was briefly
bit by one after pulling an upstream profile to confirm the behavior
shown :p

``` scheme
#!/usr/bin/env -S guix repl -L ./modules
!#

(define-module (example)
  #:use-module (gnu packages)
  #:use-module (gnu packages base)
  #:use-module (guix packages))

(define-public my-hello
  (package
    (inherit hello)
    (name "my-hello")))

;; Suppose we're an average user and keep our configuration in a
;; channel in our home-dir:
(format (current-output-port) "~s~%"
  (module-filename (current-module)))
;; => "/home/user/src/example/./example.scm"

;; Whether or not it's best practice, we may be inclined to use
;; `specification->package' to populate our `operating-system',
;; `home-environment', and even (*gasp*) `service' records.
(format (current-output-port) "~s~%"
  (specification->package "hello"))
;; => #<package [email-masked] gnu/packages/base.scm:92 7f958d8c09a0>

;; But this procedure fails when we attempt to reference a
;; package-variant defined within our configuration channel (even
;; when it's in another module, eg. `(example packages base)'):
(format (current-output-port) "~s~%"
  (specification->package "my-hello"))
;; => guix repl: error: my-hello: unknown package

;; (Note that this is a non-continuable exception, and would commented out
;; to execute the remaining stanza of this script.)

;; A clever user might parameterize `%package-search-path' to get
;; around this limitation:
(parameterize ((%package-module-path
                 (cons `(,(dirname (module-filename (current-module))) . "")
                       (%package-module-path))))
  (format (current-output-port) "~s~%"
    (specification->package "my-hello")))
;; => guix repl: error: my-hello: unknown package
;; But only with the patch do we enable this functionality:
;; => #<package [email-masked] /home/user/src/example/./example.scm:10 7fc6a6678a50>
```

Regarding my specific use-case, I have a channel featuring the following
at least the following four snippets.

antlers/home/files/emacs/init.el[1]:
``` elisp
(use-package all-the-icons
  :guix (emacs-all-the-icons)
  :defer t)

(use-package magit
  :guix emacs-magit git
  :custom (magit-diff-refine-hunk t))

(use-package embark
  :guix (emacs-embark ; This refers to my fork with a page-able which-key pop-up on `embark-collect'
          --with-git-url=emacs-embark=file:///home/antlers/[...]
          --with-branch=emacs-embark=fix/issue-647)
  :custom (prefix-help-command 'embark-prefix-help-command))

(use-package devil-mode
  :guix emacs-devil-mode
  :custom (devil-key "'"))
```

antlers/home/extract-emacs-packages.scm[2]:
``` scheme
(define extact-emacs-packages (file-path)
  [...]) ; this is not polished code, you don't wanna see it: just imagine
;; => (list #<package [...]> ...)
```

antlers/packages/emacs-xyz:
``` elisp
(define-public emacs-devil-mode
  (package
    (name "emacs-devil-mode")
    (version "0.6.0")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/susam/devil")
             (commit version)))
       (sha256
        (base32 "1pr9yf6f37sz5qy1snn8ag5bvg6lza7q635jh8jhaqqfp37jvv1y"))
       (file-name (git-file-name name version))))
    (build-system emacs-build-system)
    (home-page "https://susam.github.io/devil")
    (synopsis "Modifier-free editing in Emacs")
    (description
     "Devil mode trades your comma key in exchange for a modifier-free
editing experience in Emacs.")
    (license license:expat)))
```

antlers/home.scm:
``` scheme
(simple-service 'emacs-packages home-profile-service-type
  ;; Add `(antlers packages)' to `specification->package' PATH
  (parameterize ((%package-module-path
                   (cons `(,(dirname (dirname (module-filename (current-module))))
                           . "antlers/packages")
                         (%package-module-path))))
    (append (extract-emacs-packages "./home/files/emacs/init.el")
            (extract-emacs-packages "./home/files/emacs/early-init.el"))))
```

If there's a better way to do this (barring the obvious of upstreaming
assorted bits) let me know, I'm sure there are many ways, but this
one's mine. I don't like to keep the manifest seperate from my configuration,
but also don't want to keep them in an org file and have to invoke
emacs to build my channel.

I don't know if a.) there's any cause to be concerned about the
performance of this tweak, or b.) that you care to support the
behavior at all, but I hope I've made my own use-case clear and the
patch is there if you see fit c:

Thx for your time!

1: https://gist.github.com/AutumnalAntlers/efa81fbb3ecc2c4fdd97785731f4348c
2: https://gist.github.com/AutumnalAntlers/b3090d73b97779f977105b905be14453