Use plugin extension at buildscript section of build.gradle.kts

Hi,

I have a question involves using plugin extension at buildscript section:

I have create a standalone custom plugin with extension. The extension object exposes an API call foo(). Now I wish to apply the plugin at buildscript section of build.gradle.kts of a consumer package Bar and use the extension

import xxx.CustomExtension

buildscript {

   repository {
   		maven {
   			url = uri(location of plugin artifact)
   		}
   }
  
  // Apply plugin
  apply(plugin= "CustomPlugin")

  //Initialize Extension
  val extension = the<CustomExtension>()


  //Use foo()
  val result = extension.foo()
}

I have also create pluginManagement block in the setting.gradle.kts file to configure the repository and was also able to apply the plugin outside of buildscript section. However, when I applied
it inside of buildscript section, it returned “org.gradle.api.plugins.UnknownPluginException: Plugin with id ‘CustomPlugin’ not found”. I wish to know if there is a solution/workaround using plugin extension at buildscript section for build.gradle.kts?

Thank you very much!