# SPDX-FileCopyrightText: 2021 Kirill Elagin # # SPDX-License-Identifier: MPL-2.0 or MIT {lib}: let inherit (builtins) concatStringsSep genList stringLength substring ; inherit (lib.strings) concatMapStrings concatMapStringsSep fixedWidthString splitString stringToCharacters ; inherit (lib.lists) filter reverseList; /* Split a string into byte chunks, such that each output String is less then or equal to `n` bytes. # Type splitInGroupsOf :: Integer -> String -> [String] # Arguments n : The number of bytes to put into each String. s : The String to split. */ splitInGroupsOf = n: s: let groupCount = (stringLength s - 1) / n + 1; in genList (i: substring (i * n) n s) groupCount; # : str -> str # Prepares a Nix string to be written to a zone file as a character-string # literal: breaks it into chunks of 255 (per RFC 1035, 3.3) and encloses # each chunk in quotation marks. writeCharacterString = s: if stringLength s <= 255 then ''"${s}"'' else concatMapStringsSep " " (x: ''"${x}"'') (splitInGroupsOf 255 s); # : str -> str, with length 4 (zeros are padded to the left) align4Bytes = fixedWidthString 4 "0"; # : int -> str -> str # Expands "" to 4n zeros and aligns the rest on 4 bytes align4BytesOrExpand = n: v: if v == "" then (fixedWidthString (4 * n) "0" "") else align4Bytes v; # : str -> [ str ] # Returns the record of the ipv6 as a list mkRecordAux = v6: let splitted = splitString ":" v6; n = 8 - builtins.length (filter (x: x != "") splitted); in stringToCharacters (concatMapStrings (align4BytesOrExpand n) splitted); # : str -> str # Returns the reversed record of the ipv6 mkReverseRecord = v6: concatStringsSep "." (reverseList (mkRecordAux v6)) + ".ip6.arpa"; in { inherit writeCharacterString mkReverseRecord; }