infra/users/lib.nix

24 lines
550 B
Nix
Raw Permalink Normal View History

2019-08-11 19:53:02 +01:00
{ lib }:
let
2024-07-24 19:05:26 +10:00
chrs = lib.listToAttrs (
lib.imap (i: v: {
name = v;
value = i + 96;
}) lib.lowerChars
);
2019-08-11 19:53:02 +01:00
ord = c: builtins.getAttr c chrs;
2020-01-22 12:37:13 +01:00
in
{
2019-08-11 19:53:02 +01:00
# Make a unique UID from a 4-char identifier
2024-07-24 19:05:26 +10:00
mkUid =
id:
let
chars = lib.stringToCharacters (builtins.substring 0 4 id);
n = builtins.map (c: lib.mod (ord c) 10) chars;
2023-01-01 15:30:41 +01:00
s = builtins.concatStringsSep "" (builtins.map builtins.toString n);
in
2021-01-03 00:07:49 +01:00
assert builtins.stringLength id >= 4;
assert builtins.length chars == 4;
1000 + lib.toInt s;
2019-08-11 19:53:02 +01:00
}