Shared code used both by project build.gradle.kts & by code under src/main

I’m creating a Gradle plugin, P, that I will use to configure all of my Gradle projects.

I’ve written some code that I want to use both in the build.gradle.kts for P, and in code under src/main in P.

What is the best way to achieve this?

I imagine that I should put this code under buildSrc/src/main, which will allow me to use it in build.gradle.kts. To use this buildSrc code from under src/main, would I then do something like the following in build.gradle.kts?

sourceSets.main {

Or is there some better way of achieving this?

Is there any way of using code in a buildscript block (in build.gradle.kts) from code under src/main?

If so, how would I achieve that?