Help with PKGBUILD

Basically I want to replace a file on user’s system with the newly installed one but at the same time i want to make a backup of it given that it exists.

Tl;dr → mv ~.abc ~.abc.bak if it exists. Skip this step if it doesn’t

Found something here: How can be this done inside PKGBUILD? Also is it okay to use &&?

To keep a backup of a file if it has been changed by the user, use the backup array, https://wiki.archlinux.org/title/PKGBUILD#backup

A package shouldn’t touch files under a user’s $HOME directory. (If nothing else, what happens if there are multiple users?)

2 Likes

You can’t do this in a PKGBUILD. The PKGBUILD runs at the time the package is built, not at the time the package is installed. The package may even be built on a different machine.

If you wanted to do something like that, you would need to do it in the install file. However, that will be running as root so ~ might not mean what you think it means.

That being said, even if you could, you shouldn’t because:

3 Likes

Yeah, as mentioned above, PKGBUILD is not the place to do such things.
Instead, write a script that does it, when the user calls it.

Maybe you are thinking something like this?

if [ -r ~/.abc ] ; then
    rm -f ~/.abc.bak         # remove possible old .bak
    mv ~/.abc ~/.abc.bak
fi

Thanks everyone for the help. I am actually creating a custom zsh theme and uploading it in the AUR. You guys are right. One shouldn’t touch someone’s $HOME. So my last q is that should I drop the .zshrc in /etc/skel and let the user copy/paste it? or could there be any other approoach. Thanks.

You shouldn’t try to change .zshrc. You should create the theme somewhere centrally and ask the user to source it into their .zshrc.

I think saving it to /etc/skel is a okayish way. If it exists than pacman should automatically tell the user that’s it is saved as .zshrc.new. if it isn’t there than no problem.

This can be done but how to tell the user? echo "copy .zshrc from /blah/blah in PKGBUILD?

You shouldn’t do that. /etc/skel/.zshrc on some systems will be owned by another package. pacman won’t allow that.

You don’t need to modify .zshrc directly. Look at how other zsh addons are packaged.

Also /etc/skel folder is used by useradd only not there to store configs to be used from already created users…
it would be more some place like /usr/share/packagename/

@joekamprad Both these packages are saving .zshrc to /etc/skel

https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=dbuch-zsh-config

https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=grml-zsh-config-git

I have come to the conclusion that i should put backup=(etc/skel/.zshrc) in PKGBUILD so original doesnt get overwritten and the new is saved as .zshrc.new.

  install -Dm644 etc/zsh/zshrc "$pkgdir/etc/zsh/zshrc"

not only in /etc/skel
And as I mentioned /etc/skel will only be used on new user creation after it is present there… to get configs for already installed user you will need somehow a tutorial or a script to run as user…

I will make one more attempt and then let you do what you want.

Many Arch-based distros ship an /etc/skel/.zshrc by default from a settings package. Your package won’t be installable on those distros. pacman will throw a file exists error and refuse to proceed. The backup array doesn’t handle conflicting packages with the same file. It lets the user retain changes made to that file.

This is how you should package a set of zsh settings. Write a file to /usr/share and ask the user to include that file at the end of their .zshrc

2 Likes

As there were only two important source ... line in .zshrc, as suggested above i will put (echo put source ... in zshrc) in post install file rather than messing around with there files.

Thanks everyone

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.