I am trying to build a debian package which automatically configures all my machines to use the same configuration files and settings.
For example I am trying to set things like alter /etc/default/hostapd. I am currently doing this by using dh_install to copy a file to /etc/default/hostapd with the configuration I want it to have.
This results in the error:
trying to overwrite '/etc/default/hostapd', which is also in package hostapd
What is the correct way to create a debian package which overwrites the settings such as /etc/default/hostapd ?
The reason for the error is because any package that writes a file to the system is marked as its owner. The deb mechanism does not like it when two packages own the same file.
Your best option is to use dpkg-divert in order to rename the other package's file to a different name. In your preinst script, put the following command (assuming your package is called my_package:
dpkg-divert --divert /etc/default/hostapd.saved.my_package --package my_package --rename --add /etc/default/hostapd
In your postrm script, you need to put the opposite command (untested, RTFM to get the precise syntax correctly):
dpkg-divert --divert /etc/default/hostapd.saved.my_package --package my_package --rename --remove /etc/default/hostapd
At least in the past, there was a simpler (though less safe) option to put a "conffile" field in the debian control file. This tells the system that this package installs files that overwrite another package's files. I'm not sure I would recommend this method, however, as it might break things in case of uninstall.