portage icon indicating copy to clipboard operation
portage copied to clipboard

Add emerge hooks

Open Schievel1 opened this issue 2 years ago • 10 comments

Right now Portage has already bashrc to hook into the ebuild process. However, initially this was thought to manipulate environment variables (I think so, hence the name?) for a single package build. Bashrc is sourced several times DURING the ebuild process. However it is not sourced BEFORE and AFTER the ebuild process.

My proposed feature would be to source emergerc, a script located in /etc/portage as well, several times and change a environment variable (e.g. EMERGE_PHASE) to let the users control which code is called. A purpose for that for example would be to make a btrfs snapshot every time before emerging an update.

There are several situations that come to mind, where a user could benefit from a source of emergerc. The value that the environment variable $EMERGE_PHASE will have it that situation is written behind those points:

  • every time emerge starts (emerge_startup)
  • right before the dependencies are calculated (pre_calc_deps)
  • after dependencies are successfully calculated (post_calc_deps_success)
  • after dependencies and they were not successfully calculated (post_calc_deps_fail)
  • after the prompt "Would you like to merge these packages?" (if --ask) right before emerge starts working with the first package (pre_first_emerge)
  • before --sync (pre_sync)
  • after --sync (although there is postsync.d for that) (post_sync_success) / (post_sync_fail)
  • when emerge installed all packages successfully (post_emerge_success)
  • when emerge failed with one package (post_emerge_fail)
  • when emerge exits (emerge_exit)
  • before and after depclean (pre_depclean) / (post_depclean)
  • before and after unmerge (pre_unmerge) / (post_unmerge)

To tell the different phases of emerge apart, one would do the same like in bashrc with environment variables https://wiki.gentoo.org/wiki//etc/portage/bashrc#Environment_variable

if [ "${EMERGE_PHASE}" == "pre_first_emerge" ]
then
  echo "your code here"
fi

See also: Bug: https://bugs.gentoo.org/839777

Schievel1 avatar Nov 02 '22 16:11 Schievel1

This works, I have no idea whatsoever about the security implications when calling a shell script like that in python. Maybe someone has some better suggestions and I would be happy to put hat in.

Schievel1 avatar Nov 02 '22 16:11 Schievel1

To check out when which phases are run, use

#!/bin/bash

echo ${EMERGE_PHASE}

in /etc/portage/emergerc (create the file)

Schievel1 avatar Nov 09 '22 12:11 Schievel1

Do you have any ideas what else could be passed as an environment variable?

Schievel1 avatar Nov 09 '22 21:11 Schievel1

I added some functionality to send the list of packages and the command line options of emerge to the hook, so users can write scripts that react to that accordingly. I converted this back to draft for the time being, because I still need to test that thoroughly.

Schievel1 avatar Nov 20 '22 13:11 Schievel1

Noob question. Can you make an ebuild for those of us that want to use it, ala local overlay?

MrByte avatar Sep 01 '23 23:09 MrByte

Noob question. Can you make an ebuild for those of us that want to use it, ala local overlay?

Not really. You can make a user patch, but the script is too much of a moving target. I tried that for a while but gave up. Every time the script is changed from upstream, you would need to update the patch.

Schievel1 avatar Sep 03 '23 19:09 Schievel1

Noob question. Can you make an ebuild for those of us that want to use it, ala local overlay?

Not really. You can make a user patch, but the script is too much of a moving target. I tried that for a while but gave up. Every time the script is changed from upstream, you would need to update the patch.

Yeah, sadly true. Right now I've got a script "zmerge" that looks for "world" in the parameters and does a ZFS snap for me if that's there, then runs emerge.

I may take a swing at a patch at some point, or some other form of hackery.

Thanks for the reply.

MrByte avatar Sep 04 '23 19:09 MrByte

Hey, sorry for ignoring this PR. I'm not really on board with the idea in concept, so I'm not very motivated to re-review it. I think someone else will need to run with this one.

floppym avatar Sep 06 '23 19:09 floppym

It gives options, options are always a good thing.

MrByte avatar Sep 08 '23 14:09 MrByte

Just a user's opinion here: I'd really like to have the ability to go back in time after emerge has done changes to the system. This feature would enable a btrfs snapshot hook before emerge makes changes. It can make life much more convenient, and it's standard practice in other distributions already - so why not allow the user to do this on Gentoo as well?

nagelp avatar Apr 05 '24 02:04 nagelp