Skip to content

url-name: prefer fragment over dir= when deriving the install name#15901

Open
danfry1 wants to merge 12 commits into
NixOS:masterfrom
danfry1:fix-url-name-fragment-precedence
Open

url-name: prefer fragment over dir= when deriving the install name#15901
danfry1 wants to merge 12 commits into
NixOS:masterfrom
danfry1:fix-url-name-fragment-precedence

Conversation

@danfry1

@danfry1 danfry1 commented May 22, 2026

Copy link
Copy Markdown

Motivation

Fixes #15900.

nix profile install github:org/repo?dir=ai#my-tool was incorrectly named ai instead of my-tool, because getNameFromURL checked the dir= query argument before the fragment and returned early. Installing multiple attributes from the same ?dir= flake therefore produced collision-suffixed names (ai, ai-1, ...).

Context

The fragment names a specific attribute the user asked for; dir= only tells Nix where the flake.nix lives inside the repository. The fragment is the more specific signal and should win.

This PR swaps the order of the two checks in getNameFromURL and keeps dir= as a fallback when the fragment is absent, empty, or evaluates to default (the case where the existing code already chose to fall through).

Tests

The existing test cases in src/libflake-tests/url-name.cc covered ?dir= alone and #fragment alone but never the combination, so the buggy branch was untested. Added cases for:

  • ?dir=ai#my-toolmy-tool (the regression test)
  • ?dir=ai#packages.x86_64-linux.my-toolmy-tool
  • ?dir=ai#ai (empty fragment falls back to dir)
  • ?dir=ai#packages.x86_64-linux.defaultai (default fragment falls back to dir)

All existing assertions continue to pass under the new ordering — none of the existing ?dir= cases set a fragment.

Checklist

  • Tests, as appropriate (unit tests in src/libflake-tests/url-name.cc)
  • Release note in doc/manual/rl-next/
  • Commit message explains why the change was made

`getNameFromURL` checked the `dir=` query argument before the fragment
and returned early, so a URL like `github:org/repo?dir=ai#my-tool` was
named "ai" instead of "my-tool". This caused `nix profile install` to
generate collision-suffixed names ("ai-1", ...) when installing several
attributes from the same `?dir=` flake.

The fragment names a specific attribute the user asked for, while `dir=`
only tells Nix where the flake.nix lives in the repo, so the fragment
should win. Swap the order and keep `dir=` as a fallback when the
fragment is absent, empty, or `default`.

Closes: NixOS#15900

@ysndr ysndr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me.

@danfry1
danfry1 requested a review from ysndr July 2, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nix profile install derives wrong name when ?dir= is combined with a fragment

2 participants