Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Disable load-theme confirmation - #7

Closed
aerz wants to merge 5 commits into
grtcdr:mainfrom
aerz:main
Closed

aerz wants to merge 5 commits into
grtcdr:mainfrom
aerz:main

Conversation

@aerz

@aerz aerz commented Jan 19, 2023 •

Copy link
Copy Markdown
Contributor

I've recently configured the package in my config and I have a problem when I enable darkman-mode because the load-theme function requires confirmation to run the code from a theme.

In my case I have a systemd unit to start emacs where I can't do any confirmation.
I suggest using the no-confirm argument to avoid this by default.

@grtcdr

grtcdr commented Jan 19, 2023

Copy link
Copy Markdown
Owner

Hi,

We should develop this more, as this change introduces a potential risk of running arbitrary Emacs Lisp code unbeknownst to the user, i.e. because they've delegated the theme loading task to this package.

We should introduce a new customizable variable, e.g. darkman-load-theme-no-confirm which toggles this behavior, and it should be nil by default.

@grtcdr

grtcdr commented Jan 19, 2023

Copy link
Copy Markdown
Owner

If we take this approach, the new variable should then be utilized in darkman--mode-changed-signal-handler and darkman-mode.

@grtcdr

grtcdr commented Jan 19, 2023

Copy link
Copy Markdown
Owner

Another approach would be to work hand in hand with the custom library, meaning users are instructed to trust their themes beforehand.

This has the added benefit of not introducing any code changes to the package, only documentation.

I personally like this approach a lot more; nevertheless, I would like to hear what our users think before deciding on anything final.

@grtcdr grtcdr added discussion This issue is an active discussion ergonomy Improvement in ergonomy labels Jan 19, 2023
@aerz

aerz commented Jan 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Hi, @grtcdr thanks for considering a workaround to handle my issue.
For me, when the user set in the config a light and dark theme is similar to add it in a whitelist that is safe to run because probably it was tested before doing the package configuration.

However, I understand the potential risk and adding a variable to do not confirm the changes looks a good way to deal with this. I'll happy to try add the code solution you proposed but maybe it took me a while since I never developed any real Elisp code.

In any case, I would like to see more opinions from other users.

@grtcdr

grtcdr commented Jan 19, 2023

Copy link
Copy Markdown
Owner

For me, when the user set in the config a light and dark theme is
similar to add it in a whitelist that is safe to run because probably
it was tested before doing the package configuration.

If we decide to go with the first proposal, you would have to define
the variable I previously mentioned using defcustom [1] like most
other variables of the package and then pass it as
the second parameter of every occurrence of the load-theme function.

In contrast, the second proposal requires that the documentation be
updated to make sure that users are aware of this distinction, and of
our decision to integrate with custom.el instead of developing our
own internal mechanism for solving the issue.

However, I understand the potential risk and adding a variable to do
not confirm the changes looks a good way to deal with this.

I'm glad we both agree.

I'll happy to try add the code solution you proposed but maybe it
took me a while since I never developed any real Elisp code.

I'm happy to guide you along the way :)


Footnotes:

  1. https://www.gnu.org/software/emacs/manual/html_node/elisp/Variable-Definitions.html

@aerz

aerz commented Jan 20, 2023

Copy link
Copy Markdown
Contributor Author

I'm happy to guide you along the way :)

Thank you very much 😄. Before start typing code, I did more research on the topic and I'd like your opinion.

  • Emacs manual about custom themes suggests to use custom-safe-themes adding a list of themes you consider safe to run or set it to true to prevent any confirmation. 1
  • There is another way to load a theme by using enable-theme instead load-theme which consult.el, doomemacs and a user config uses.

What are your thoughts on this? I see three possible solutions:

  • We could continue with the use of load-theme with the optional no-confirm variable
  • Or use that enable-theme but before adding the light and dark theme to custom-safe-themes list or add dynamically the theme to custom-enabled-themes which seems a safer solution because doesn't allow that any theme can load.
  • Or just simply add to the documentation this case and how to avoid this prompt, prioritising safety over convenience.

Footnotes

  1. https://www.gnu.org/software/emacs/manual/html_node/emacs/Custom-Themes.html ↩

@grtcdr

grtcdr commented Jan 20, 2023 •

Copy link
Copy Markdown
Owner

Do not use custom-safe-themes directly[1].

load-theme uses it to store[2] the hash of the themes you've told it
to trust when it prompted you, and that hash is prone to change on the
slightest modification to theme.

There is another way to load a theme by using enable-theme instead
load-theme which consult.el, doomemacs and a user config uses.

Thanks for the links, they're all very interesting approaches.

What are your thoughts on this? I see three possible solutions:

load-theme is kind of a misnomer; the function delegates the logic
of loading themes to enable-theme which has no inherent security
features.

Or just simply add to the documentation this case and how to avoid
this prompt, prioritising safety over convenience.

I think this package should rely on the pre-existing mechanisms of
Emacs which helps us minimize code duplication and keep the attack
surface to an absolute minimum. The documentation should therefore not
display a preference towards any particular method.


Footnotes:

  1. Unless you're setting it to t to bypass the safety features,
    like in purcell's example.
  2. The custom-theme-load-confirm function is what's pushing the
    hashes to the custom-safe-themes variable.

* Include documentation to aware users of this change
@aerz

aerz commented Jan 20, 2023

Copy link
Copy Markdown
Contributor Author

Thank you for sharing your thoughts @grtcdr I missed some details on the usage of custom-safe-themes and the code of load-theme as you point out. I also agree with your code approach.

I've done some basic work on the optional variable, just in case you want a quicker solution for now.

I'd like comments on the code or any suggestions to go further and improve anything you're considering.

@aerz

aerz commented Jan 20, 2023

Copy link
Copy Markdown
Contributor Author

I have included documentation changes which can be also used to explain other options such as darkman-switch-themes-silently or future ones.

@grtcdr

grtcdr commented Jan 21, 2023 •

Copy link
Copy Markdown
Owner

Thank you for sharing your thoughts @grtcdr I missed some details on the usage of custom-safe-themes and the code of load-theme as you point out. I also agree with your code approach.

Thank you :)

I've done some basic work on the optional variable, just in case you want a quicker solution for now.

That won't be necessary if we adopt custom.el fully.

I'd like comments on the code or any suggestions to go further and improve anything you're considering.

I'm going to try and make some tiny adjustments.

EDIT: I've opened a pull request on your fork at aerz#1

@aerz

aerz commented Jan 21, 2023

Copy link
Copy Markdown
Contributor Author

Merged! 😁

That won't be necessary if we adopt custom.el fully.

That's what I was thinking when I did the commit because you mentioned in #7 (comment) a better integration. However to be honest, I don't know how integrate that custom.el with your package. Sorry for that but if you could explain it I can continue to make further changes.

@grtcdr

grtcdr commented Jan 21, 2023 •

Copy link
Copy Markdown
Owner

Merged! grin

That won't be necessary if we adopt custom.el fully.

That's what I was thinking when I did the commit because you mentioned in #7 (comment) a better integration. However to be honest, I don't know how integrate that custom.el with your package. Sorry for that but if you could explain it I can continue to make further changes.

Thanks for merging the changes.

Integrating with custom.el requires no more than following the instructions suggested by the soon-to-be-merged documentation, or setting custom-safe-themes to t (which I do not recommend).

@grtcdr

grtcdr commented Jan 24, 2023

Copy link
Copy Markdown
Owner

Hi, can you please review the comments I left?

@aerz

aerz commented Jan 24, 2023 •

Copy link
Copy Markdown
Contributor Author

Hi, can you please review the comments I left?

Hi, I checked the new changes, I didn't notice it yesterday, sorry for the late. I think everything is great and well explained.
I wouldn't know about the piece of advice functionallity in other languages, really exciting. The only thing I'm confused about is where is the no-theme-stacking function? Is this a new function you are going to implement?

On the other hand, you already add the changes to the main branch, so I can close the PR whenever you want.

@grtcdr

grtcdr commented Jan 24, 2023

Copy link
Copy Markdown
Owner

Hi, I checked the new changes, I didn't notice it yesterday, sorry
for the late. I think everything is great and well explained.

I'm glad you think so :)

I wouldn't know about the piece of advice functionallity in other
languages, really exciting.

I completely agree, it's very handy to be able to advise functions!

The only thing I'm confused about is where is the no-theme-stacking
function? Is this a new function you are going to implement?

no-theme-stacking is actually the name of the piece of advice.

The structure of a defadvice is as follows:

(defadvice FUNCTION (CLASS NAME [POSITION] [ARGLIST] FLAG...)
    [DOCSTRING] [INTERACTIVE-FORM]
    BODY...)

The snippet from the documentation sets the CLASS, NAME and FLAG
variables to before, no-theme-stacking and activate respectively.

You can at any time disable this advice interactively by hitting M-x ad-remove-advice RET, selecting load-theme as the function,
before as the class and finally no-theme-stacking.

@grtcdr

grtcdr commented Jan 26, 2023

Copy link
Copy Markdown
Owner

On the other hand, you already add the changes to the main branch, so I can close the PR whenever you want.

I shouldn't have done that, your contribution will still count. :)

@aerz

aerz commented Jan 26, 2023 •

Copy link
Copy Markdown
Contributor Author

I shouldn't have done that, your contribution will still count. :)

Great! 🙂

I've continued testing your piece of advice. If I'm not wrong, the idea to add it is: if there's no custom-enabled-themes, the error will be gone because Emacs should load in any case a default theme and allowing to start the Emacs server.

I personally use doomemacs and I was using this config:

;; $DOOMDIR/package.el
(package! darkman
  :recipe (:host github :repo "grtcdr/darkman.el"))
;; $DOOMDIR/config.el
(use-package! darkman
  :config
  (setq darkman-themes '(:light doom-tomorrow-day
                         :dark doom-tomorrow-night))
  (darkman-mode)
  (defadvice darkman-set (before no-theme-stacking activate)
    "Disable the previous theme before loading a new one."
    (mapc #'disable-theme custom-enabled-themes)))

However, the confirmation kept appearing at start when I set a theme that is not in custom-safe themes and darkman-mode sync with my preferences because the use-package! does an eager load according to the doomemacs/getting_started#packages-are-eagerly-loaded.

Because of that, I was seeking for a way to only load darkman-mode when I run emacsclient -c -a "emacs" to prevent the confirmation where running the daemon and keeping the popup to not bypass the security mechanisms. I finally managed with this config:

(use-package! darkman
  :config
  (setq darkman-themes '(:light doom-tomorrow-day
                         :dark doom-tomorrow-night))
  (defadvice darkman-set (before no-theme-stacking activate)
    "Disable the previous theme before loading a new one."
    (mapc #'disable-theme custom-enabled-themes))
  (if (display-graphic-p)
      (darkman-mode))
  (if (daemonp)
      (add-hook 'server-after-make-frame-hook #'darkman-mode)))

This works both with and without an Emacs server. The hook I used is documented here. I'm sure you could provide a better solution. 😄

I think the idea is useful for this issue. Maybe this could be added it to the package or documentation.

@grtcdr

grtcdr commented Jan 27, 2023

Copy link
Copy Markdown
Owner

I've continued testing your piece of advice. If I'm not wrong, the idea to add it is: if there's no custom-enabled-themes, the error will be gone because Emacs should load in any case a default theme and allowing to start the Emacs server.

The defadvice I added has nothing to do with the security features, it's just a nicety. Instead of themes stacking on each other, they'll replace each other instead.

However, the confirmation kept appearing at start when I set a theme that is not in custom-safe themes and darkman-mode sync with my preferences because the use-package! does an eager load according to the doomemacs/getting_started#packages-are-eagerly-loaded.

As previously mentioned, I don't intend to support or condone any methods besides the defaults because they seem very reasonable to me. If you wish to avoid confirmation, please enable the theme beforehand. :)

This works both with and without an Emacs server. The hook I used is documented here. I'm sure you could provide a better solution

If I understand the documentation correctly, server-after-make-frame-hook is run every time the server creates a new client frame, which means that every time you start an emacsclient instance, you're enabling/disabling darkman-mode... I'm not sure why you'd want to do that.

@aerz

aerz commented Jan 30, 2023 •

Copy link
Copy Markdown
Contributor Author

Ok, I misunderstood the intention of defadvise, but I agree with you on the way to deal with this issue. I also didn't want to avoid any security confirmation as we talk before. I'm just trying to avoid a crash when I start Emacs from systemd or any other init system where you don't have an opportunity to do any confirmation. Let me explain it.

My Emacs server is stopped and the value of custom-safe-themes is (default). My config.el has a basic darkman config:

(use-package darkman
  :ensure t
  :load-path "darkman.el"
  :config
  (setq darkman-themes '(:light doom-tomorrow-day
                         :dark doom-tomorrow-night))
  (darkman-mode))

Now I start the server and the minor darkman-mode will be enabled because it's in my config it will sync the theme I set in darkman-themes but the server crashes because I can't confirm the security mechanism and the theme is not in custom-safe-themes. I don't want to skip any default behaviour, so I added that extra code in my config to load darkman-mode only when I could confirm it.

(if (display-graphic-p)
      (darkman-mode))

This will only happen when I run Emacs without a server backend.

(if (daemonp)
      (add-hook 'server-after-make-frame-hook #'darkman-mode))

This will load only when I open a new frame from a server.

In both cases I can confirm or not the elisp execution code and it will update custom-safe-themes when I answer yes.
This is what I meant, we can add this information to the documentation about how to avoid this crash in case the user forgot to enable the theme and can't confirm anything.

@grtcdr

grtcdr commented Feb 2, 2023 •

Copy link
Copy Markdown
Owner

I understand now.

I was thinking we should add what you're proposing in the package
itself, most of the logic (which isn't very much) would be implemented
in the darkman-mode function.

Hooking darkman-mode to server-after-make-frame is a tiny bit
wasteful as the function will be called whenever a new frame is
created. We should remove the hook right before the function returns.

This ensures that the daemon does not get blocked by the prompt and
that darkman-mode does not get called more than once.

We can start with something like this:

(define-minor-mode darkman-mode
  "Minor mode providing integration with Darkman."
  :global t
  :init-value nil
  :require 'dbus
  :version "0.1"
  (if darkman-mode
      (progn
	(and (darkman--check-dbus-service)
	     (setq darkman--dbus-signal
		   (dbus-register-signal
		    :session
		    darkman--dbus-service
		    darkman--dbus-path
		    darkman--dbus-interface
		    "ModeChanged"
		    #'darkman--mode-changed-signal-handler))
	     (load-theme (darkman-get-theme)))
	(when (daemonp)
	  (remove-hook 'server-after-make-frame-hook #'darkman-mode)))
    (dbus-unregister-object darkman--dbus-signal)
    (setq darkman--dbus-signal nil)))

And right at the end of the package, we include that handy hook:

(when (daemonp) (add-hook 'server-after-make-frame-hook #'darkman-mode))

@grtcdr

grtcdr commented Feb 2, 2023

Copy link
Copy Markdown
Owner

I'll let you include the change so we can finally merge this pull request :)

@aerz

aerz commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

Hey @grtcdr, sorry for the late. I'm glad you understood that case.
I was checking the latest changes you made and testing but there is something it doesn't work. I'll try to explain it and I hope you could give some thought to what might be happening.

I downloaded the latest commits from darkman.el and now my config looks like:

;; $DOOMDIR/config.el
(use-package! darkman
  :custom
  (darkman-themes '(:light doom-tomorrow-day
                    :dark doom-tomorrow-night))
  :config
  (darkman-mode))
;; $DOOMDIR/packages.el
(package! darkman
  :recipe (:host github :repo "grtcdr/darkman.el"))

The variable custom-safe-themes contains the hashes of both themes but despite everything the Emacs daemon still requests the load-theme request (Emacs daemon crashes) and also running Emacs without the daemon always request the confirmation the first time. Once I answered yes or no, I can run darkman toggle from the terminal and the theme synchronises with no request.

Are there any limitations on what a package can do in this case?

I'll check it and comment if I found some workaround. 😥

@grtcdr

grtcdr commented Feb 8, 2023

Copy link
Copy Markdown
Owner

I can't reproduce the problem you're describing, but I put together a video that demonstrates the expected behavior. I hope that clears any confusion.

2023-02-08.mp4

@aerz

aerz commented Feb 8, 2023 •

Copy link
Copy Markdown
Contributor Author

Ok, I didn't notice you always add the hook to load darkman-mode when the package is in daemon mode. I remove the :config (darkman-mode) and now it's working perfectly.

I'm still getting confirmation request when I run Emacs without the daemon but I think this is because doom emacs. Maybe it loads the package too early and custom.el where custom-safe-themes is defined isn't loaded.

@grtcdr

grtcdr commented Feb 8, 2023 •

Copy link
Copy Markdown
Owner

Glad things are working again.

You should still keep (darkman-mode) in your configuration, because (a) that removes the hook that the package sets when it is required/sourced by Emacs (b) handles the incoming D-Bus signals.

@aerz

aerz commented Feb 8, 2023 •

Copy link
Copy Markdown
Contributor Author

I fixed it. Doom Emacs loads the packages too early even before of custom.el. I'm finally using the following to delay the loading of the darkman-mode:

;; $DOOMDIR/config.el
(use-package! darkman
  :custom
  (darkman-themes '(:light doom-tomorrow-day
                    :dark doom-tomorrow-night))
  :config
  (if (display-graphic-p)
      (add-hook! 'after-init-hook (darkman-mode))))

Using this config it works with and with no daemon and only requests confirmation when the theme it's not in custom-safe-themes.

I'm sorry for this @grtcdr. If I had more knowledge about all this, I wouldn't have gone round and round and bothered you a bit. 😅 Anyway, I think the original idea of adding darkman-load-theme-no-confirm variable is no longer needed, or do you want to merge the PR to provide more flexibility?

@grtcdr

grtcdr commented Feb 8, 2023 •

Copy link
Copy Markdown
Owner

I fixed it. Doom Emacs loads the packages too early even before of
custom.el. I'm finally using the following to delay the loading of
the darkman-mode:

Good catch :-)

;; $DOOMDIR/config.el
(use-package! darkman
  :custom
  (darkman-themes '(:light doom-tomorrow-day
                    :dark doom-tomorrow-night))
  :config
  (if (display-graphic-p)
      (add-hook! 'after-init-hook (darkman-mode))))

Using this config it works with and with no daemon and only requests confirmation when the theme it's not in custom-safe-themes.

I failed to reproduce this behavior using a vanilla configuration, so
I won't introduce the hook you used to into the package. I will
however document it so that future Doom users can benefit from the
information you've shared throughout this pull request.

I'm sorry for this @grtcdr. If I had more knowledge about all this,
I wouldn't have gone round and round and bothered you a
bit.

Don't apologize! This has been a wonderful learning experience for me,
too. It's one of the few pull requests that felt more like an exchange
and a conversation than actual work.

Anyway, I think the original idea of adding
darkman-load-theme-no-confirm variable is no longer needed, or do
you want to merge the PR to provide more flexibility?

Yes, we won't require any of that.

@aerz

aerz commented Feb 8, 2023

Copy link
Copy Markdown
Contributor Author

I failed to reproduce this behavior using a vanilla configuration, so I won't introduce the hook you used to into the package.

Yes, as future references I found a comment in the doomemacs code about what is the load order. We could confirm that $DOOMDIR/config.el is loaded before $DOOMDIR/custom.el where required darkman variable lives, so it's recommended for Doom Emacs users to use that hook.

I will however document it so that future Doom users can benefit from the information you've shared throughout this pull request.

Great! Thank you 😁

Don't apologize! This has been a wonderful learning experience for me, too. It's one of the few pull requests that felt more like an exchange and a conversation than actual work.

Yes! I've learnt a lot and thank you for making this package, it makes Emacs is almost the perfect tool for me.

Back to the PR, I would prefer you to close it if you think we have fixed the issue. From my side everything's fine now.
If you need anything in the future, testing or anything, you can mention me. I'll be happy to help.

@grtcdr

grtcdr commented Feb 8, 2023

Copy link
Copy Markdown
Owner

Yes, as future references I found a comment
in the doomemacs code about what is the load order. We could confirm
that $DOOMDIR/config.el is loaded before $DOOMDIR/custom.el
where required darkman variable lives, so it's recommended for Doom
Emacs users to use that hook.

Thank you so much for linking this resource - and kudos to Henrik,
Doom looks like a really well-documented codebase.

Yes! I've learnt a lot and thank you for making this package, it
makes Emacs is almost the perfect tool for me.

I'm very glad to hear that it's enhancing your experience.

If you need anything in the future, testing or anything, you can
mention me. I'll be happy to help.

Will do, thanks a lot for your help and patience. :-)

@grtcdr grtcdr closed this Feb 8, 2023
@grtcdr

grtcdr commented Mar 6, 2023

Copy link
Copy Markdown
Owner

Hey @aerz, I just wanted to let you know that in commit d1da4a7 we dropped the hook that addresses this issue, it has to be manually added from now on, though the relevant snippet is mentioned in the documentation. I've been told it may be impolite to register a hook such as this one which the user may or may not know about.

@aerz

aerz commented Mar 6, 2023

Copy link
Copy Markdown
Contributor Author

Hey @aerz, I just wanted to let you know that in commit d1da4a7 we dropped the hook that addresses this issue, it has to be manually added from now on, though the relevant snippet is mentioned in the documentation. I've been told it may be impolite to register a hook such as this one which the user may or may not know about.

Okay, thanks for letting me know about the change. I understand the reason and it makes sense, even if it should be included in the user configuration, wouldn't it make more sense to use a defavice (after) for darkman-mode to remove the hook?

@grtcdr

grtcdr commented Mar 6, 2023 •

Copy link
Copy Markdown
Owner

Advising the function after it is run will probably not fix the issue of a prompt appearing too early in the initialization process, if that's what you mean. With a hook, you're attaching the function to when the server's made a client frame, with a piece of advice with an after class, you're running a piece of code any and every time said function is called.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

discussion This issue is an active discussion ergonomy Improvement in ergonomy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants