From 474e3e2e4513a5d2df89789885725b176e7ec532 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 11 Dec 2018 23:21:42 +0100 Subject: lib: import generally useful stuff from genipv6 --- lib/default.nix | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib/default.nix') diff --git a/lib/default.nix b/lib/default.nix index e352c7be9..64b2d48ab 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -112,6 +112,33 @@ let (if test ".*::.*" a then a else group-zeros (drop-leading-zeros a)); + + hashToLength = n: s: substring 0 n (hashString "sha256" s); + + dropLast = n: xs: reverseList (drop n (reverseList xs)); + takeLast = n: xs: reverseList (take n (reverseList xs)); + + # Split string into list of chunks where each chunk is at most n chars long. + # The leftmost chunk might shorter. + # Example: stringToGroupsOf "123456" -> ["12" "3456"] + stringToGroupsOf = n: s: let + acc = + foldl' + (acc: c: if stringLength acc.chunk < n then { + chunk = acc.chunk + c; + chunks = acc.chunks; + } else { + chunk = c; + chunks = acc.chunks ++ [acc.chunk]; + }) + { + chunk = ""; + chunks = []; + } + (stringToCharacters s); + in + filter (x: x != []) ([acc.chunk] ++ acc.chunks); + }; in -- cgit v1.2.3 From 35be9c66bfa6dd03437f919ec610aed0e9b20b58 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 12 Dec 2018 00:34:32 +0100 Subject: lib.normalize-ip6-addr: no :: for single section --- lib/default.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/default.nix') diff --git a/lib/default.nix b/lib/default.nix index 64b2d48ab..347830e8c 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -93,7 +93,13 @@ let in if max.pos == 0 then a - else "${concatStringsSep ":" lhs}::${concatStringsSep ":" rhs}"; + else let + sep = + if 8 - (length lhs + length rhs) == 1 + then ":0:" + else "::"; + in + "${concatStringsSep ":" lhs}${sep}${concatStringsSep ":" rhs}"; drop-leading-zeros = let -- cgit v1.2.3