Compatibility, bringing Sumireko into the fold. Deprecate katexprs.

This commit is contained in:
kat 2022-04-12 13:25:56 +01:00 committed by kat
parent efae399c70
commit 40edeef897
Signed by: kat
GPG key ID: 465E64DECEA8CF0F
39 changed files with 879 additions and 175 deletions

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})"
);
}

7
overlays/lib/default.nix Normal file
View file

@ -0,0 +1,7 @@
self: super: {
lib = super.lib.extend (self: super: import ./overlay.nix {
inherit super;
lib = self;
isOverlayLib = true;
});
}

View file

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

View file

@ -0,0 +1,7 @@
{ 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))

View file

@ -0,0 +1,5 @@
{ 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}))

View file

@ -0,0 +1,29 @@
{ 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;
modList = (listToAttrs modules);
in
modList

View file

@ -0,0 +1,21 @@
{ lib }: { nixosImports, homeImports, users, hostName, profiles }: with lib;
let
importLists = {
nixos = nixosImports;
home = homeImports;
};
replacedLists = mapAttrs
(_: fileList:
map (builtins.replaceStrings [ "HN" ] [ "${hostName}" ]) fileList
)
importLists;
homeScaffold = user: {
home-manager.users.${user} = {
imports = filter builtins.pathExists replacedLists.home;
};
};
scaffoldedUsers = map homeScaffold users;
baseProfile = singleton profiles.base;
in
filter builtins.pathExists replacedLists.nixos ++ baseProfile ++ scaffoldedUsers

23
overlays/lib/overlay.nix Normal file
View file

@ -0,0 +1,23 @@
{ 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;
nodeImport = import ./node-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

View file

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