Last active
January 28, 2018 01:33
-
-
Save corpix/739e3d854a9a63e6c1768499c6324f05 to your computer and use it in GitHub Desktop.
My very naive implementation of IP6 address expansion in nix language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
$ nix-instantiate --eval --expr 'with (import <nixpkgs> {}); with lib; builtins.toJSON ((import ./ip6.nix { lib = pkgs.lib; }).expandIP6 "ea5f::")' | jq -r . | jq . | |
"ea5f:0000:0000:0000:0000:0000:0000:0000" | |
$ nix-instantiate --eval --expr 'with (import <nixpkgs> {}); with lib; builtins.toJSON ((import ./ip6.nix { lib = pkgs.lib; }).expandIP6 "::ea5f")' | jq -r . | jq . | |
"0000:0000:0000:0000:0000:0000:0000:ea5f" | |
$ nix-instantiate --eval --expr 'with (import <nixpkgs> {}); with lib; builtins.toJSON ((import ./ip6.nix { lib = pkgs.lib; }).expandIP6 "ea5f::ff")' | jq -r . | jq . | |
"ea5f:0000:0000:0000:0000:0000:0000:00ff" | |
*/ | |
{ lib }: | |
with lib; | |
with builtins; | |
rec { | |
inherit (import ./lists.nix { inherit lib; }) padListLeft padListRight repeatListItem; | |
inherit (import ./strings.nix { inherit lib; }) padStringLeft; | |
expandIP6 = v: | |
let | |
octetsSep = ":"; | |
octets = splitString octetsSep v; | |
octetsNum = 8; | |
octetLength = 4; | |
paddedOctets = | |
if head octets == "" then padListLeft octetsNum "" octets else | |
if last octets == "" then padListRight octetsNum "" octets else | |
foldl (a: v: # will break if find two "", so address requires additional validation | |
if v == "" | |
then (a ++ [v] ++ (repeatListItem (octetsNum - (length octets)) v)) | |
else (a ++ [v]) | |
) [] octets; | |
in concatStringsSep | |
octetsSep | |
(map (padStringLeft octetLength "0") paddedOctets); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib }: | |
with lib; rec { | |
padListLeft = padSize: padWith: v: | |
if length v < padSize | |
then (padListLeft padSize padWith ([ padWith ] ++ v)) | |
else v; | |
padListRight = padSize: padWith: v: | |
if length v < padSize | |
then (padListRight padSize padWith (v ++ [ padWith ])) | |
else v; | |
repeatListItem = times: v: | |
( | |
if times > 1 | |
then (repeatListItem (times - 1) v) | |
else [] | |
) ++ [v]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ lib }: | |
with lib; rec { | |
padStringLeft = padSize: padWith: v: | |
if stringLength v < padSize | |
then (padStringLeft padSize padWith "${padWith}${v}") | |
else v; | |
padStringRight = padSize: padWith: v: | |
if stringLength v < padSize | |
then (padStringRight padSize padWith "${v}${padWith}") | |
else v; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment