# \`String -\> int\` function

**URL:** https://discourse.nixos.org/t/string-int-function/69291
**Category:** Help
**Created:** [September 12, 2025, 3:08pm UTC](https://discourse.nixos.org/t/string-int-function/69291 "2025-09-12T15:08:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![l0b0](https://discourse.nixos.org/user_avatar/discourse.nixos.org/l0b0/32/39028_2.png) [@l0b0](https://discourse.nixos.org/u/l0b0)
#### Post date: [September 12, 2025, 3:08pm UTC](https://discourse.nixos.org/t/string-int-function/69291/1 "2025-09-12T15:08:16Z")

</div>

**Background:**

I’ve set up some remote builders for use by a group of people, and it works pretty well. I’d like to achieve two additional goals with this setup:

1. The machines are semi-equally loaded. This is to make sure each job has the maximum amount of resources.
2. Builds from a single person all go to the same machine as long as that machine has capacity. This is to make sure that jobs reuse the local Nix cache as much as possible, since a single user’s builds are probably going to be pretty similar to each other.

I couldn’t find some built-in way to achieve this, so I’m thinking of a hack: Since each user has a unique SSH username, and each person’s machine is configured to connect to all the builders using that username, I can

1. convert the username to a number N,
2. convert the hostname to a number M, and finally
3. set nix.buildMachines.\*.speedFactor to N×M (possibly modulo X).

For example, `nix.buildMachines.0.speedFactor` might be 1004 for Alice, 1234 for Bob, and 1345 for Charlie.

**tl;dr:** Is there some easy way to convert a string to a number (possibly including modulo, to avoid overflow), short of splitting the string into characters, mapping each to an integer, creating some giant base-[however many characters are allowed in a username or hostname] number, and doing modulo on that?

Alternatively, is there a better way to achieve load balancing _and_ “personal favourites” with remote builders?

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [September 12, 2025, 4:17pm UTC](https://discourse.nixos.org/t/string-int-function/69291/2 "2025-09-12T16:17:50Z")

</div>

> [@l0b0](#):
>
> **tl;dr:** Is there some easy way to convert a string to a number

```auto
nix-repl> builtins.fromJSON "123"
123

nix-repl> lib.toIntBase10 "10237"
10237

```

Pick whatever works best.

EDIT: actually it seems the latter uses `fromJSON` underneath 🙂 and is a bit more robust to e.g. spaces.

---

<div class="post-metadata">

### Author: ![l0b0](https://discourse.nixos.org/user_avatar/discourse.nixos.org/l0b0/32/39028_2.png) [@l0b0](https://discourse.nixos.org/u/l0b0)
#### Post date: [September 12, 2025, 4:36pm UTC](https://discourse.nixos.org/t/string-int-function/69291/3 "2025-09-12T16:36:41Z")

</div>

I meant an _arbitrary_ string, not a numeric one.

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [September 12, 2025, 4:39pm UTC](https://discourse.nixos.org/t/string-int-function/69291/4 "2025-09-12T16:39:23Z")

</div>

No, and writing your own would be inefficient at best (and x-y).

---

<div class="post-metadata">

### Author: ![lassulus](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lassulus/32/31292_2.png) [@lassulus](https://discourse.nixos.org/u/lassulus)
#### Post date: [September 12, 2025, 5:51pm UTC](https://discourse.nixos.org/t/string-int-function/69291/5 "2025-09-12T17:51:47Z")

</div>

> **[nix-hashlib](https://git.clan.lol/clan/nix-hashlib)**
>
> A nix hashing library

---

<div class="post-metadata">

### Author: ![joelmccracken](https://discourse.nixos.org/user_avatar/discourse.nixos.org/joelmccracken/32/8006_2.png) [@joelmccracken](https://discourse.nixos.org/u/joelmccracken)
#### Post date: [September 12, 2025, 6:02pm UTC](https://discourse.nixos.org/t/string-int-function/69291/6 "2025-09-12T18:02:14Z")

</div>

maybe combine

> <https://github.com/NixOS/nixpkgs/blob/9c6d827f8d4803d2880f052661fad342ebca9c0f/lib/trivial.nix#L1122-L1156>

and

[https://teu5us.github.io/nix-lib.html#builtins.hashstring](https://teu5us.github.io/nix-lib.html#builtins.hashstring)

and then parse with the aforementioned `fromJSON` function?

---

<div class="post-metadata">

### Author: ![l0b0](https://discourse.nixos.org/user_avatar/discourse.nixos.org/l0b0/32/39028_2.png) [@l0b0](https://discourse.nixos.org/u/l0b0)
#### Post date: [September 13, 2025, 11:18am UTC](https://discourse.nixos.org/t/string-int-function/69291/7 "2025-09-13T11:18:00Z")

</div>

Thank you for the tips! Ended up with a [blog post](https://paperless.blog/string-to-pseudorandom-integer-in-nix) since the code base I’m using it in is not public.

---

<div class="post-metadata">

### Author: ![joelmccracken](https://discourse.nixos.org/user_avatar/discourse.nixos.org/joelmccracken/32/8006_2.png) [@joelmccracken](https://discourse.nixos.org/u/joelmccracken)
#### Post date: [September 13, 2025, 5:44pm UTC](https://discourse.nixos.org/t/string-int-function/69291/8 "2025-09-13T17:44:26Z")

</div>

Great, I’m glad it worked!
