The length of integer in the Nix language

There’s a built-in integer type the Nix language. However, its range is left undocumented. What’s the specification of the range of integer?

On my x86_64-linux NixOS, the integer seems to be 64-bit long, using two’s complement for signing and wrapping for overflow.

Is this behavior the same across all platforms, or is it platform-specific?

I encountered this problem when trying to implement the hash base conversion function to workaround the lack of related builtins.

nix-repl> powWithTermInt = let f = n: base: term: if n == 0 t
hen term else if n < 0 then f (n + 1) base (builtins.div term
 base) else f (n - 1) base (term * base); in f

nix-repl> powWithTermInt 62 2 1
4611686018427387904

nix-repl> powWithTermInt 63 2 1
-9223372036854775808

nix-repl> -1 + powWithTermInt 63 2 1
9223372036854775807

nix-repl> powWithTermInt 64 2 1
0

nix-repl> -1 + powWithTermInt 64 2 1
-1
4 Likes

Please open a GitHub issue to track this. Discourse posts will get lost and one cannot visibly indicate when the problem is solved.

2 Likes

Issue opened.

2 Likes