Files
nix-config/lib/module/default.nix
mjallen18 36010a4230 lib
2025-08-23 19:54:05 -05:00

122 lines
2.5 KiB
Nix

{ inputs }:
let
inherit (inputs.nixpkgs.lib)
mapAttrs
mkOption
types
toUpper
substring
stringLength
mkDefault
mkForce
;
base64Lib = import ../base64 { inherit inputs; };
in
rec {
# Conditionally enable modules based on system
enableForSystem =
system: modules:
builtins.filter (
mod: mod.systems or [ ] == [ ] || builtins.elem system (mod.systems or [ ])
) modules;
# Create a module with common options
mkModule =
{
name,
description ? "",
options ? { },
config ? { },
}:
{ lib, ... }:
{
options.mjallen.${name} = lib.mkOption {
type = lib.types.submodule {
options = {
enable = lib.mkEnableOption description;
}
// options;
};
default = { };
};
config = lib.mkIf config.mjallen.${name}.enable config;
};
mkContainer =
{
name,
localAddress ? "127.0.0.1",
port ? "80",
bindMounts ? { },
config ? { }
}:
{ lib, ... }:
{
containers.${name} = {
inherit localAddress bindMounts config;
autoStart = lib.mkDefault true;
privateNetwork = lib.mkDefault true;
hostAddress = lib.mkDefault "10.0.1.3";
};
networking = {
nat = {
forwardPorts = [
{
destination = lib.mkDefault "${localAddress}:${toString port}";
sourcePort = lib.mkDefault port;
}
];
};
firewall = {
allowedTCPPorts = [ port ];
allowedUDPPorts = [ port ];
};
};
};
# Migrated mjallen utilities
# Option creation helpers
mkOpt =
type: default: description:
mkOption { inherit type default description; };
mkOpt' = type: default: mkOpt type default null;
mkBoolOpt = mkOpt types.bool;
mkBoolOpt' = mkOpt' types.bool;
# Standard enable/disable patterns
enabled = {
enable = true;
};
disabled = {
enable = false;
};
# String utilities
capitalize =
s:
let
len = stringLength s;
in
if len == 0 then "" else (toUpper (substring 0 1 s)) + (substring 1 len s);
# Boolean utilities
boolToNum = bool: if bool then 1 else 0;
# Attribute manipulation utilities
default-attrs = mapAttrs (_key: mkDefault);
force-attrs = mapAttrs (_key: mkForce);
nested-default-attrs = mapAttrs (_key: default-attrs);
nested-force-attrs = mapAttrs (_key: force-attrs);
}
// base64Lib