refactor(lib): start using Std

This commit is contained in:
arcnmx 2024-05-02 11:01:10 -07:00
parent 9184146fed
commit 0a9747c396
5 changed files with 32 additions and 43 deletions

55
lib.nix
View file

@ -5,52 +5,40 @@
}: let }: let
nixlib = inputs.nixpkgs.lib; nixlib = inputs.nixpkgs.lib;
inherit (nixlib.modules) mkOrder mkOverride defaultOverridePriority; inherit (nixlib.modules) mkOrder mkOverride defaultOverridePriority;
inherit (nixlib.strings) splitString toLower; inherit (nixlib.attrsets) mapAttrs listToAttrs;
inherit (nixlib.lists) imap0 elemAt findFirst foldl; inherit (inputs.self.lib.Std) List Str Regex UInt Opt;
inherit (nixlib.attrsets) mapAttrs listToAttrs nameValuePair;
inherit (nixlib.strings) hasPrefix hasInfix removePrefix substring fixedWidthString replaceStrings concatMapStringsSep stringToCharacters match toInt;
inherit (nixlib.trivial) flip toHexString bitOr;
toHexStringLower = v: toLower (toHexString v);
hexCharToInt = let
hexChars = ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f"];
pairs = imap0 (flip nameValuePair) hexChars;
idx = listToAttrs pairs;
in
char: idx.${char};
hexToInt = str: foldl (value: chr: value * 16 + hexCharToInt chr) 0 (stringToCharacters (toLower str));
eui64 = mac: let eui64 = mac: let
parts = map toLower (splitString ":" mac); parts = List.map Str.toLower (Regex.splitOn ":" mac);
part = elemAt parts; part = List.index parts;
part0 = part: let part0 = part: let
nibble1' = hexCharToInt (substring 1 1 part); nibble1' = UInt.FromHexDigit (Str.index part 1);
nibble1 = bitOr 2 nibble1'; nibble1 = UInt.or' 2 nibble1';
nibble0 = substring 0 1 part; nibble0 = Str.index part 0;
in in
nibble0 + (fixedWidthString 1 "0" (toHexStringLower nibble1)); nibble0 + UInt.toHexLower nibble1;
in "${part0 (part 0)}${part 1}:${part 2}ff:fe${part 3}:${part 4}${part 5}"; in "${part0 (part 0)}${part 1}:${part 2}ff:fe${part 3}:${part 4}${part 5}";
parseUrl = url: let parseUrl = url: let
parts = match ''^([^:]+)://(\[[0-9a-fA-F:]+]|[^/:\[]+)(|:[0-9]+)(|/.*)$'' url; parts' = Regex.match ''^([^:]+)://(\[[0-9a-fA-F:]+]|[^/:\[]+)(|:[0-9]+)(|/.*)$'' url;
port' = elemAt parts 2; parts = parts'.value;
in assert parts != null; rec { port' = List.index parts 2;
in assert Opt.isJust parts'; rec {
inherit url parts; inherit url parts;
scheme = elemAt parts 0; scheme = List.index parts 0;
host = elemAt parts 1; host = List.index parts 1;
port = if port' != "" then toInt (removePrefix ":" port') else null; port = if port' != "" then UInt.Parse (Str.removePrefix ":" port') else null;
hostport = host + port'; hostport = host + port';
path = elemAt parts 3; path = List.index parts 3;
}; };
userIs = group: user: builtins.elem group (user.extraGroups ++ [user.group]); userIs = group: user: builtins.elem group (user.extraGroups ++ [user.group]);
mkWinPath = replaceStrings ["/"] ["\\"]; mkWinPath = Str.replace ["/"] ["\\"];
mkBaseDn = domain: concatMapStringsSep "," (part: "dc=${part}") (splitString "." domain); mkBaseDn = domain: Str.concatMapSep "," (part: "dc=${part}") (Regex.splitOn "\\." domain);
mkAddress6 = addr: if hasInfix ":" addr && ! hasPrefix "[" addr then "[${addr}]" else addr; mkAddress6 = addr: if Str.hasInfix ":" addr && ! Str.hasPrefix "[" addr then "[${addr}]" else addr;
coalesce = findFirst (v: v != null) null; coalesce = values: Opt.default null (List.find (v: v != null) values);
mapListToAttrs = f: l: listToAttrs (map f l); mapListToAttrs = f: l: listToAttrs (map f l);
overrideOptionDefault = 1500; overrideOptionDefault = 1500;
@ -99,7 +87,6 @@ in {
domain = "gensokyo.zone"; domain = "gensokyo.zone";
inherit treeToModulesOutput userIs inherit treeToModulesOutput userIs
eui64 parseUrl mkWinPath mkBaseDn mkAddress6 eui64 parseUrl mkWinPath mkBaseDn mkAddress6
toHexStringLower hexToInt hexCharToInt
mapListToAttrs coalesce mapListToAttrs coalesce
mkAlmostOptionDefault mkAlmostDefault mkAlmostForce mapOverride mapOptionDefaults mapAlmostOptionDefaults mapDefaults mkAlmostOptionDefault mkAlmostDefault mkAlmostForce mapOverride mapOptionDefaults mapAlmostOptionDefaults mapDefaults
overrideOptionDefault overrideAlmostOptionDefault overrideDefault overrideAlmostDefault overrideNone overrideAlmostForce overrideForce overrideVM overrideOptionDefault overrideAlmostOptionDefault overrideDefault overrideAlmostDefault overrideNone overrideAlmostForce overrideForce overrideVM
@ -111,7 +98,7 @@ in {
inherit inputs; inherit inputs;
inherit (inputs) self; inherit (inputs) self;
inherit (inputs.self) overlays; inherit (inputs.self) overlays;
inherit (inputs.self.lib) tree meta lib systems; inherit (inputs.self.lib) tree meta lib systems std Std;
}; };
generate = import ./generate.nix {inherit inputs tree;}; generate = import ./generate.nix {inherit inputs tree;};
} }

View file

@ -1,6 +1,6 @@
let let
allowListModule = {config, name, gensokyo-zone, lib, ...}: let allowListModule = {config, name, gensokyo-zone, lib, ...}: let
inherit (gensokyo-zone.lib) hexToInt; inherit (gensokyo-zone.Std) UInt;
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.modules) mkOptionDefault; inherit (lib.modules) mkOptionDefault;
inherit (builtins) typeOf; inherit (builtins) typeOf;
@ -26,7 +26,7 @@ let
}; };
config = let config = let
xuid = { xuid = {
string = toString (hexToInt config.xuid); string = toString (UInt.FromHex config.xuid);
int = toString config.xuid; int = toString config.xuid;
}.${typeOf config.xuid}; }.${typeOf config.xuid};
in { in {

View file

@ -3,6 +3,7 @@
config, config,
meta, meta,
std, std,
Std,
lib, lib,
inputs, inputs,
... ...
@ -104,7 +105,7 @@ in {
inherit (config) system modules specialArgs; inherit (config) system modules specialArgs;
}) config.builder); }) config.builder);
specialArgs = { specialArgs = {
inherit name inputs std meta; inherit name inputs std Std meta;
inherit (inputs.self.lib) gensokyo-zone; inherit (inputs.self.lib) gensokyo-zone;
systemType = config.folder; systemType = config.folder;
system = config; system = config;

View file

@ -1,5 +1,6 @@
{config, lib, inputs, ...}: let {config, gensokyo-zone, lib, Std, ...}: let
inherit (inputs.self.lib.lib) unmerged eui64 toHexStringLower mkAlmostOptionDefault mapAlmostOptionDefaults; inherit (Std) UInt;
inherit (gensokyo-zone.lib) unmerged eui64 mkAlmostOptionDefault mapAlmostOptionDefaults;
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault; inherit (lib.modules) mkIf mkMerge mkDefault mkOptionDefault;
inherit (lib.attrsets) attrValues; inherit (lib.attrsets) attrValues;
@ -154,7 +155,7 @@
name = mkIf system.proxmox.container.enable (mkAlmostOptionDefault "eth9"); name = mkIf system.proxmox.container.enable (mkAlmostOptionDefault "eth9");
bridge = mkAlmostOptionDefault "vmbr9"; bridge = mkAlmostOptionDefault "vmbr9";
address4 = mkAlmostOptionDefault "10.9.1.${toString index}/24"; address4 = mkAlmostOptionDefault "10.9.1.${toString index}/24";
address6 = mkAlmostOptionDefault "fd0c::${toHexStringLower index}/64"; address6 = mkAlmostOptionDefault "fd0c::${UInt.toHexLower index}/64";
macAddress = mkIf (system.proxmox.network.interfaces.net0.macAddress or null != null && hasPrefix "BC:24:11:" system.proxmox.network.interfaces.net0.macAddress) (mkAlmostOptionDefault ( macAddress = mkIf (system.proxmox.network.interfaces.net0.macAddress or null != null && hasPrefix "BC:24:11:" system.proxmox.network.interfaces.net0.macAddress) (mkAlmostOptionDefault (
replaceStrings [ "BC:24:11:" ] [ "BC:24:19:" ] system.proxmox.network.interfaces.net0.macAddress replaceStrings [ "BC:24:11:" ] [ "BC:24:19:" ] system.proxmox.network.interfaces.net0.macAddress
)); ));
@ -163,7 +164,7 @@
domains = mkDefault [ ]; # int.${domain}? domains = mkDefault [ ]; # int.${domain}?
linkConfig.RequiredForOnline = false; linkConfig.RequiredForOnline = false;
ipv6AcceptRAConfig = { ipv6AcceptRAConfig = {
Token = mkOptionDefault "static:::${toHexStringLower index}"; Token = mkOptionDefault "static:::${UInt.toHexLower index}";
DHCPv6Client = mkOptionDefault false; DHCPv6Client = mkOptionDefault false;
}; };
networkConfig = { networkConfig = {

View file

@ -1,7 +1,7 @@
{inputs}: let {inputs}: let
# The purpose of this file is to set up the host module which allows assigning of the system, e.g. aarch64-linux and the builder used with less pain. # The purpose of this file is to set up the host module which allows assigning of the system, e.g. aarch64-linux and the builder used with less pain.
lib = inputs.self.lib.nixlib; lib = inputs.self.lib.nixlib;
inherit (inputs.self.lib) meta std; inherit (inputs.self.lib) meta std Std;
inherit (lib.modules) evalModules; inherit (lib.modules) evalModules;
inherit (std) set; inherit (std) set;
hostConfigs = set.map (name: path: hostConfigs = set.map (name: path:
@ -11,7 +11,7 @@
meta.modules.system meta.modules.system
]; ];
specialArgs = { specialArgs = {
inherit name inputs std meta; inherit name inputs std Std meta;
inherit (inputs.self.lib) gensokyo-zone; inherit (inputs.self.lib) gensokyo-zone;
}; };
}) })