`unexpected fragment in flake reference` error when trying to use a git branch with a `#` in the name as a flake input
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
- Clone this repository.
- In the newly cloned repository, run
nix build.
Expected behavior
The project builds.
Metadata
nix-env (Nix) 2.24.14
Additional context
Checklist
- [x] checked latest Nix manual (source)
- [x] checked open bug issues and pull requests for possible duplicates
Add :+1: to issues you find important.
What if you URL-escape it by doing %23 in place of the # character?
@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
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.
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
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
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.
Fixed in https://github.com/NixOS/nix/pull/13729