Is there any way to capture the config file inside workflow when passed on command line (pass via -c) option

How to achive the following?
I want Nextflow workflow to automatically copy the config file into the output directory (outdir) when I run my pipeline with config file passed via -c.
Suppose if I run a pipeline like this:
nextflow run main.nf –input sample.csv –outdir ./results -c custom.config
Inside workflow i want custom.config automatically coppied into result directory.

Nextflow has a nextflow.configFiles attribute that you can access.

Use this with the workflow.onComplete function and copyTo function (Types — Nextflow documentation) to copy them to your outdir, or perhaps use the Channel.fromPath to take the list of files and combine with the new publishing format (Workflows — Nextflow documentation), or pass to a native process and use the old publishing method ( publishDir ). It’ll be down to you to make sure the filenames don’t collide though ( share the same name ).

workflow {
    Channel.fromPath(workflow.configFiles).view()
}