refactor pkgs/lib -> lib, kw.fw -> network.firewall, ...

This commit is contained in:
kat witch 2021-08-13 21:17:56 +01:00
parent ce5c5f7f7c
commit 0e126e2a2e
No known key found for this signature in database
GPG key ID: 1B477797DCA5EC72
32 changed files with 35 additions and 35 deletions

25
lib/color-helpers.nix Normal file
View file

@ -0,0 +1,25 @@
{ lib }: rec {
hexChars =
[ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" ];
hexCharToInt = char:
let
pairs = lib.imap0 (lib.flip lib.nameValuePair) hexChars;
idx = builtins.listToAttrs pairs;
in
idx.${lib.toLower char};
hexToInt = str:
lib.foldl (value: chr: value * 16 + hexCharToInt chr) 0
(lib.stringToCharacters str);
hextorgba = hex: alpha:
(
let
r_hex = lib.substring 1 2 hex;
g_hex = lib.substring 3 2 hex;
b_hex = lib.substring 5 2 hex;
r_dec = hexToInt r_hex;
g_dec = hexToInt g_hex;
b_dec = hexToInt b_hex;
in
"rgba(${toString r_dec}, ${toString g_dec}, ${toString b_dec}, ${toString alpha})"
);
}

20
lib/default.nix Normal file
View file

@ -0,0 +1,20 @@
{ pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib
# for internal use...
, super ? if !isOverlayLib then lib else { }
, self ? if isOverlayLib then lib else { }
, before ? if !isOverlayLib then lib else { }
, isOverlayLib ? false
}@args: let
colorHelpers = import ./color-helpers.nix { inherit lib; };
lib = before // katlib // self;
katlib = with before; with katlib; with self;
{
inherit (colorHelpers) hextorgba;
hostImport = import ./host-import.nix { inherit lib; };
virtualHostGen = import ./virtual-host-gen.nix { inherit lib; };
domainMerge = import ./domain-merge.nix { inherit lib; };
modListMerge = import ./intersect-merge.nix { inherit lib; };
modList = import ./module-list.nix { inherit lib; };
folderList = import ./folder-list.nix { inherit lib; };
}; in katlib

9
lib/domain-merge.nix Normal file
View file

@ -0,0 +1,9 @@
{ lib }: { folder, defaultFile ? "default.nix", folderPaths ? [ (../depot + "/${folder}") (../depot/trusted + "/${folder}") ] }: with lib; let
defaultFileFinal = if (defaultFile == "default.nix" && folder == "hosts") then
"meta.nix"
else defaultFile;
folderModLists = map (folderPath: modList {
modulesDir = folderPath;
defaultFile = defaultFileFinal;
}) (filter builtins.pathExists folderPaths);
in foldl modListMerge { } folderModLists

5
lib/folder-list.nix Normal file
View file

@ -0,0 +1,5 @@
{ lib }: path: excludes: let
filterAttrNamesToList = filter: set:
lib.foldl' (a: b: a ++ b) [ ]
(map (e: if (filter e set.${e}) then [ e ] else [ ]) (lib.attrNames set));
in (filterAttrNamesToList (name: type: ! (builtins.elem name excludes) && type == "directory") (builtins.readDir path))

12
lib/host-import.nix Normal file
View file

@ -0,0 +1,12 @@
{ lib }: { hostName, profiles }: with lib; filter builtins.pathExists [
(../depot/hosts + "/${hostName}/nixos.nix")
(../depot/trusted/hosts + "/${hostName}/nixos.nix")
] ++ (if builtins.isAttrs profiles.base then profiles.base.imports
else singleton profiles.base) ++ singleton {
home-manager.users.kat = {
imports = filter builtins.pathExists [
(../depot/hosts + "/${hostName}/home.nix")
(../depot/trusted/hosts + "/${hostName}/home.nix")
];
};
}

4
lib/intersect-merge.nix Normal file
View file

@ -0,0 +1,4 @@
{ lib }: pathsA: pathsB: with lib; let
pathIntersection = intersectLists (attrNames pathsA) (attrNames pathsB);
pathMerger = pathA: pathB: { imports = [ pathA pathB ]; };
in pathsA // pathsB // genAttrs pathIntersection (key: (pathMerger pathsA.${key} pathsB.${key}))

28
lib/module-list.nix Normal file
View file

@ -0,0 +1,28 @@
{ lib }: { modulesDir, defaultFile ? "default.nix", importAll ? false }:
with builtins;
let
isModule = m: lib.isFunction m && (m.isModule or true);
filterAttrNamesToList = filter: set:
foldl' (a: b: a ++ b) [ ]
(map (e: if (filter e set.${e}) then [ e ] else [ ]) (attrNames set));
nameValuePair = name: value: { inherit name value; };
listToAttrs = foldl' (acc: val: acc // { ${val.name} = val.value; }) { };
directories =
filterAttrNamesToList (_: type: type == "directory") (readDir modulesDir);
files = map (dir: nameValuePair dir (modulesDir + "/${dir}/${defaultFile}"))
(filter (f: builtins.pathExists (modulesDir + "/${f}/${defaultFile}")) directories);
modules = map
({ name, value }:
# if the file contains a function, assume it to be a module and pass the path
# (for dedup and such). if it contains anything else, pass that.
let m = import value;
in
{
inherit name;
value = if lib.isFunction m && ! isModule m then m { inherit lib; } else if isModule m && !importAll then value else m;
})
files;
in
(listToAttrs modules)

9
lib/virtual-host-gen.nix Normal file
View file

@ -0,0 +1,9 @@
{ lib }: { config, networkFilter ? [ ], addresses ? [ ], block }: with lib;
let
networks = config.network.addresses;
filteredNetworks = filterAttrs (n: v: elem n networkFilter && v.enable) networks;
networkValues = attrValues filteredNetworks;
addressList = concatMap (n: n.out.identifierList) networkValues;
hostBlocks = map (host: nameValuePair host block) addressList;
in listToAttrs hostBlocks