Nix Flake and private repository (again)

I already found another post which asked something similar (Nix Flakes and Private Repositories) and I tried the suggestion mentioned by @siraben.
However when I run it I get the following error:

nix build ssh+git://git@git.example.com:User/repo.git
error: 'ssh+git://git@git.example.com:User/repo.git' is not a valid URL

The repo exists and I was able to build the Flake within the cloned repo, I just try now to build it directly.

1 Like

It looks like you need to use git+ssh://… rather than ssh+git://…. This seems like a bug as ssh+git://… is the traditional form here.

1 Like

Hm that didn’t work either (same error).
git+https with a slightly modified URL worked but I would prefer to use SSH if possible.

In addition to what @lilyball said, the colon : directly after the domain name should be a forward slash /. So the resulting URL would be

git+ssh://git@git.example.com/User/repo.git
5 Likes

That was it thank you.
It’s a pitty that we can’t use the standard git URL format for this.

To be fair, if you leave out the git+ at the start that is “standard git URL format” for ssh-based repositories. The <user>@<domain>:<path> form is just shorthand syntax copied from scp that git also happens to support. But I assume that since it is not a canonical URL, nix does not support it.

1 Like

Fair enough, it’s just a bit inconvenient because GitHub, etc. all provide the scp format for SSH.

Yeah that’s also fair. Btw I forgot that nix used to support URLs in that format, so when you’re used to that it’s extra annoying that suddenly it gives an “invalid URL” error.

BTW, If you’re using Github, then you can use the access-tokens option like so (to specify a pre-generated access token, see this):

nix.conf:

access-tokens = github.com=ghp_XXXXXXXXX

CLI (one-time use):

nix <command> --option access-tokens= github.com=ghp_XXXXXXXXX

and reference the private flake inputs as usual (like github:USER/REPO). You can read about it here

P.S. You can probably also use it with other providers (like Gitlab), but I’ve never tried it

1 Like

Is there a way to specify the branch with the git+ssh syntax?

You can specify a branch by appending a ?ref=<branch> parameter, e.g.

git+ssh://git@git.example.com/User/repo.git?ref=master

See the “Flake references” section of nix flake --help for more info on how references should be formatted.

2 Likes

Just a note that you can use your ssh config definitions in this invocation, you just have to use the colon and slash.

Example:

nix build "git+ssh://private_git:/user/repo.git?ref=main"

where private_git is set in .ssh/config

Ha, and (just a small heads-up) indeed the quotes are very necessary, because of the ?, otherwise one often gets away with unquoted URLs, but this had me scratching my head a bit due to the funky error message (go figure, as a 25+years linux veteran :man_facepalming: )

Hosted by Flying Circus.