Files
nfpm/rpm/relations.go
Laurent CoustetandLaurent Coustet edf3552720 feat(rpm): add script_requires to require script interpreters (#1151)
rpmbuild runs script.req on the executable files of a package, so a
package that ships a "#!/bin/bash" script requires /bin/bash without
any work from the packager. nfpm has no such generator, so the
interpreter of every script must be listed by hand, which is
error-prone.

Add the opt-in rpm.script_requires option. Like script.req with the
exeonly flag of script.attr, it reads the "#!" line of each executable
file and requires its interpreter, and the absolute program run by
env, flagged RPMSENSE_FIND_REQUIRES like the dependencies rpmbuild
generates. Documentation files, symlinks, directories and ghosts are
skipped. The mode checked is the one in the package, so file_info.mode
is honored and the option also works when building on Windows.

The generated spec sets AutoReqProv: no, so the requires are written
to it explicitly and rpmbuild --rebuild keeps them.

Add unit tests, a scriptrequires acceptance stage that reads the
requires back with rpm, and an executable script to the srpm rebuild
acceptance test.

Co-authored-by: Laurent Coustet <laurent.coustet@bluemind.net>
2026-10-28 16:45:58 +00:00

49 lines
1.2 KiB
Go

package rpm
import (
"github.com/goreleaser/nfpm/v2"
"go.digitalxero.dev/rpm"
)
// rpmSenseScriptPost marks a dependency as Requires(post).
// https://github.com/rpm-software-management/rpm/blob/master/include/rpm/rpmds.h
const rpmSenseScriptPost = 1 << 10
// applyRelations maps the nfpm dependency lists onto the package builder.
func applyRelations(b rpm.PackageBuilder, info *nfpm.Info) error {
addAll := func(set rpm.RelationSetBuilder, items []string) {
for _, item := range items {
set.AddParsed(item)
}
set.Done()
}
addAll(b.Provides(), info.Provides)
addAll(b.Requires(), info.Depends)
addAll(b.Recommends(), info.Recommends)
addAll(b.Obsoletes(), info.Replaces)
addAll(b.Suggests(), info.Suggests)
addAll(b.Conflicts(), info.Conflicts)
// Requires(post) dependencies carry the post-install scriptlet sense.
for _, item := range info.RPM.Requires.Post {
rel, err := rpm.ParseRelation(item)
if err != nil {
return err
}
b.Requires().
With(rel.Name(), rel.Version(), rel.Sense()|rpm.SenseScriptPost).
Done()
}
interpreters, err := scriptRequires(info)
if err != nil {
return err
}
for _, interpreter := range interpreters {
b.Requires().With(interpreter, "", rpmSenseFindRequires).Done()
}
return nil
}