nix icon indicating copy to clipboard operation
nix copied to clipboard

`unexpected fragment in flake reference` error when trying to use a git branch with a `#` in the name as a flake input

Open nothingnesses opened this issue 7 months ago • 4 comments

Describe the bug

The unexpected fragment in flake reference error is displayed when building a flake that has a git input with a # in the name.

Steps To Reproduce

  1. Clone this repository.
  2. In the newly cloned repository, run nix build.

Expected behavior

The project builds.

Metadata

nix-env (Nix) 2.24.14

Additional context

Checklist


Add :+1: to issues you find important.

nothingnesses avatar Jun 05 '25 10:06 nothingnesses

What if you URL-escape it by doing %23 in place of the # character?

cole-h avatar Jun 05 '25 14:06 cole-h

@cole-h I get this error instead:

error: in URL 'github:nothingnesses/flake-pound-issue/its/a%23branch', 'its/a#branch' is not a branch/tag name

nothingnesses avatar Jun 06 '25 22:06 nothingnesses

Discussed in Nix meeting today:

URL escaping should be the solution. We should probably get rid of https://github.com/NixOS/nix/blob/d46ce52fac62d2b8dbc95aeb7a31af3a2ae1a78d/src/libutil/include/nix/util/url-parts.hh#L36 or replace it with an accurate regex, if at all possible.

Since we use libgit2, we're somewhat less susceptible to git ref syntax injection attacks (not sure about the vector). With that in mind, it might be ok to just leave the checking to libgit2 instead.

roberth avatar Jun 11 '25 20:06 roberth

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2025-06-11-nix-team-meeting-minutes-231/65543/1

nixos-discourse avatar Jun 11 '25 20:06 nixos-discourse

Curiously, the behavior seems to have changed with 2.25 (for the worse):

❯ nix run github:nixos/nix/2.24-maintenance -- eval --expr 'builtins.parseFlakeRef "github:aaa/bbbb/branch%23"'
error:
       … while calling the 'parseFlakeRef' builtin
         at «string»:1:1:
            1| builtins.parseFlakeRef "github:aaa/bbbb/branch%23"
             | ^

       error: in URL 'github:aaa/bbbb/branch%23', 'branch#' is not a commit hash or branch/tag name
❯ nix run github:nixos/nix/2.25-maintenance -- eval --expr 'builtins.parseFlakeRef "github:aaa/bbbb/branch%23"'
error:
       … while calling the 'parseFlakeRef' builtin
         at «string»:1:1:
            1| builtins.parseFlakeRef "github:aaa/bbbb/branch%23"
             | ^

       error: flake reference 'github:aaa/bbbb/branch%23' is not an absolute path

xokdvium avatar Aug 10 '25 19:08 xokdvium

Ah, this is because since the FlakeURL can't be parsed it instead tries to parse it as a path reference. This is just an error message regression it seems.

xokdvium avatar Aug 10 '25 19:08 xokdvium

Fixed in https://github.com/NixOS/nix/pull/13729

xokdvium avatar Aug 11 '25 10:08 xokdvium