pkg_tar(strip_prefix = ".") does this. rules_pkg has a system for mapping paths, I find that passing files around like that is usually easier than implementing the logic myself.
The core of the logic is applying
dest_path to each element of `
src[DefaultInfo].files.to_list()`. That's a function which determines a path, and removes a prefix if it's present. In the `strip_prefix = "."` case, this prefix is `compute_data_path(ctx, ".")`, which evaluates to `ctx.label.package`. The path itself is calculated in `safe_short_path` like this:
working_path = file_.path
if not file_.is_source:
working_path = working_path[len(file_.root.path)+1:]
I think `working_path[len(ctx.label.package):]` will give you the result you're looking for, assuming your filegroup and your rule are in the same package. If not, `ctx.attr.src.label.package` will tell you the package for the filegroup being passed as an attribute.
If you're looking for any other tricky path manipulation, I find that rules_pkg has lots of examples.