* Using Go vanity URL with cgit


This year I wanted to experiment with moving away from Github (and other cloud based VCS). So I setup my own server with git, cgit as the interface and nginx as the web server. The basic setup was fairly easy.

I recently published one of my tools (snowy) and when I tried to install it from my repository I got an error message:

unrecognized import path "git.claw0ry.net/snowy" (parse https://git.claw0ry.net/snowy?go-get=1: no go-import meta tags ())

I went to the internet and did some research. Apparently, Go treats github.com (and other popular vcs) specially, so if we want to our custom domain and cgit hosted repository to work with go install and go get we need to do some extra work on our end. You can read the official documentation about the subject here: https://pkg.go.dev/cmd/go#hdr-Remote_import_paths

Basically what we need is an HTML meta-tag to tell go how to map a package name to a repository. For instance, go does not know that my package name git.claw0ry.net/snowy should resolve to https://git.claw0ry.net/snowy by itself.

This is the tag that go will look for.

<meta name="go-import" content="git.claw0ry.net/snowy git https://git.claw0ry.net/snowy">

To use this meta-tag cgit has a repo.extra-head-content option that we can use to inject the meta-tag. The downside of this is that it must be done pr repository. Instead we can use NGINX sub_filter module to inject this meta-tag on very repository.

So in your NGINX configuration for our domain we will replace a part of the HTML before returning it.

server {
    server_name git.claw0ry.net;
    # ...

    location / {
        # ...

        sub_filter '</head>' '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';
        sub_filter_once on;
    }
}

The next time you do a go install it should work.