Skip to content

manual: Update Names section to exclude indented strings and URIs#15806

Open
toonn wants to merge 1 commit into
NixOS:masterfrom
toonn:attrset-names-doc
Open

manual: Update Names section to exclude indented strings and URIs#15806
toonn wants to merge 1 commit into
NixOS:masterfrom
toonn:attrset-names-doc

Conversation

@toonn
Copy link
Copy Markdown
Contributor

@toonn toonn commented May 6, 2026

Stating that names can be "double-quoted string literals" rather than any string literals removes potential confusion.

Motivation

Reading the Nix 2.28.7 reference manual suggests attribute names are allowed to be string literals, which includes indented string literals and URIs, but this doesn't seem to be the case.

I did my tests with nix (Nix) 2.31.4. Creating files as in the snippets below and then running nix-instantiate --eval <snippet-file>.

Context

Indented strings and URIs are string literals but they are not allowed as names for attrsets, let or inherit. The following snippets illustrate that they lead to syntax errors.

==> examples/ind_str-attr.nix <==
{
  ''indented string'' = "error: syntax error, unexpected IND_STRING_OPEN, expecting INHERIT";
}

==> examples/ind_str-inherit.nix <==
{ inherit ({ "non-indented string" = "error: syntax error, unexpected IND_STRING_OPEN"; })
    ''non-indented string'';
}

==> examples/ind_str-let.nix <==
let
  ''indented string'' = "error: syntax error, unexpected URI, expecting IN_KW or INHERIT";
in
  true

==> examples/uri-attr.nix <==
{
  http://example.org/foo.tar.bz2 = "error: syntax error, unexpected URI, expecting INHERIT";
}

==> examples/uri-inherit.nix <==
{ inherit ({ "http://example.org/foo.tar.bz2" = "error: syntax error, unexpected URI"; })
    http://example.org/foo.tar.bz2;
}

==> examples/uri-let.nix <==
let
  http://example.org/foo.tar.bz2 = "error: syntax error, unexpected URI, expecting IN_KW or INHERIT";
in
  true

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

Indented strings and URIs are string literals but they are not allowed
as names for attrsets, `let` or `inherit`. The following snippets
illustrate that they lead to syntax errors.

==> examples/ind_str-attr.nix <==
{
  ''indented string'' = "error: syntax error, unexpected IND_STRING_OPEN, expecting INHERIT";
}

==> examples/ind_str-inherit.nix <==
{ inherit ({ "non-indented string" = "error: syntax error, unexpected IND_STRING_OPEN"; })
    ''non-indented string'';
}

==> examples/ind_str-let.nix <==
let
  ''indented string'' = "error: syntax error, unexpected URI, expecting IN_KW or INHERIT";
in
  true

==> examples/uri-attr.nix <==
{
  http://example.org/foo.tar.bz2 = "error: syntax error, unexpected URI, expecting INHERIT";
}

==> examples/uri-inherit.nix <==
{ inherit ({ "http://example.org/foo.tar.bz2" = "error: syntax error, unexpected URI"; })
    http://example.org/foo.tar.bz2;
}

==> examples/uri-let.nix <==
let
  http://example.org/foo.tar.bz2 = "error: syntax error, unexpected URI, expecting IN_KW or INHERIT";
in
  true
@toonn toonn requested a review from edolstra as a code owner May 6, 2026 16:05
Copy link
Copy Markdown
Contributor

@MattSturgeon MattSturgeon left a comment

Choose a reason for hiding this comment

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

There's also the complication of writing names as raw (unquoted) interpolation, e.g.:

${if true then "foo" else null} = "bar";

IIRC, this is not mentioned by the manual.

Related, but separate issue.

@xokdvium
Copy link
Copy Markdown
Contributor

xokdvium commented May 7, 2026

This would be a slightly different feature - dynamic attributes.

@toonn
Copy link
Copy Markdown
Contributor Author

toonn commented May 7, 2026

I agree dynamic attributes probably deserve a bit more documentation. Technically they are documented here, in the string interpolation section, but that's not very easily found if you start at the section on Attribute sets and want to know what names are allowed (you'd have to click through string literal > string interpolation, somehow divining that the string literal section is misleading when it says interpolation elements can occur inside quotes).

I could expand on them a bit but I'm not very familiar with the feature so I'd prefer someone else tackling that in a separate PR.

@toonn
Copy link
Copy Markdown
Contributor Author

toonn commented May 12, 2026

I've opened a similar PR to the Lix manual.

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.

3 participants