Skip to content

Instantly share code, notes, and snippets.

@yorickvP
Created February 18, 2021 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickvP/90ea75d7201dd208be9555666453c291 to your computer and use it in GitHub Desktop.
Save yorickvP/90ea75d7201dd208be9555666453c291 to your computer and use it in GitHub Desktop.
with import <nixpkgs/lib>;
let
inherit (builtins) typeOf;
cfg = import ./router-config.nix;
indent = str:
let
parts = (splitString "\n" str);
plen = length parts;
in concatStringsSep "\n"
(imap0 (i: v: if (i == 0 || i == plen - 1) then v else " ${v}") parts);
prefixAttrs = prefix: mapAttrs' (name: nameValuePair (prefix + name));
fromSet = config:
concatStrings (mapAttrsToList (name: val:
if name == "value" then
"${indent (toText val)}\n"
else if name == "disable" && val then
"disable\n"
else if name == "disable" && (!val) then
""
else if (typeOf val) == "list" then
(fromSet (prefixAttrs (name + " ") (head val)))
else "${name} ${indent (toText val)}\n"
) config);
fromString = str: if hasInfix " " str || str == "" then ''"${str}"'' else str;
toText = config:
let type = builtins.typeOf config;
in if type == "bool" then
(if config then "enable" else "disable")
else if type == "string" then
(fromString config)
else if type == "int" then
toString config
else if type == "set" then
"{\n" + (fromSet config) + "}"
else
builtins.throw "unknown type ${type}";
in fromSet cfg + ''
/* Warning: Do not remove the following line. */
/* === vyatta-config-version: "config-management@1:conntrack@1:cron@1:dhcp-relay@1:dhcp-server@4:firewall@5:ipsec@5:nat@3:qos@1:quagga@2:suspend@1:system@4:ubnt-pptp@1:ubnt-udapi-server@1:ubnt-unms@1:ubnt-util@1:vrrp@1:vyatta-netflow@1:webgui@1:webproxy@1:zone-policy@1" === */
/* Release version: v2.0.8-hotfix.1.5278088.200305.1641 */
''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment