Skip to content

Support hexadecimal and octal syntax for integers #7578

Open
@roberth

Description

Is your feature request related to a problem? Please describe.

Some configuration formats support hex or octal syntax. It would be nice if those numbers were easily expressed in Nix as well.

Describe the solution you'd like

Extend the parser to parse 0x[:xdigit]+ as a integer converted from base 16
Currently e.g. 0xff is parsed as 0 xff, which would never return anything is only valid syntax when used inside a list literal. We can assume that all expressions containing a hexadecimal integer are currently invalid expressions. That makes this change an improvement instead of a breaking change. Nonetheless, this seems like a very unlikely sub-expression in practice, as most people have used actual whitespace to separate numbers and identifiers in lists, if they even consider building a heterogeneous list.

Similarly, extend the parser to parse 0o[0-7]+ as a integer converted from base 8.

Similarly, make sure negative integers in these bases work.

I would suggest not to save the base of the integer in its Value representation. This keeps integers simple.

Describe alternatives you've considered

  • Add base 8 or 16 conversion functions to the Nixpkgs lib. The end user syntax would not be great, for example: lib.fromHex "0xff".

  • Saving the base of the integer in its Value representation might be useful for detecting decimal integers used in places where an octal integer is expected. If the user writes an octal integer using decimal syntax, we could detect that mistake. However, I don't think it's currently worth complicating the meaning of integers for this.

Additional context

Inspired by NixOS/nixpkgs#208747 (comment)

Priorities

Add 👍 to issues you find important.

Metadata

Assignees

No one assigned

    Labels

    featureFeature request or proposallanguageThe Nix expression language; parser, interpreter, primops, evaluation, etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions