Files
lux/modules/lib/helpers.nix
T
2026-04-25 01:28:28 +02:00

118 lines
2.5 KiB
Nix

{ lib, config, ... }:
let
nixosModules = config.flake.modules.nixos;
hmModules = config.flake.modules.homeManager;
resolvePackagePath =
{
pkgs,
path,
}:
lib.attrByPath path null pkgs;
mkCaddyReverseProxy =
{
domain,
port,
extraHeaders ? [ ],
extraConfigText ? "",
}:
let
headerLines = map (header: " header_up ${header.name} ${header.value}") extraHeaders;
extraConfigLines = map (line: " ${line}") (
lib.filter (line: line != "") (lib.splitString "\n" extraConfigText)
);
bodyLines = headerLines ++ extraConfigLines;
body = lib.concatStringsSep "\n" bodyLines;
in
{
services.caddy.virtualHosts.${domain}.extraConfig =
if body == "" then
"reverse_proxy :${toString port}"
else
''
reverse_proxy :${toString port} {
${body}
}
'';
};
mkHost =
machine:
{ pkgs, ... }:
{
imports = [
nixosModules.host-base
machine.module
];
meta.machine = machine;
networking.hostName = machine.name;
system.stateVersion = machine.stateVersion;
programs.zsh.enable = true;
users.users = lib.mapAttrs (_: user: {
isNormalUser = true;
home = user.account.homeDirectory;
extraGroups = [
"wheel"
"networkmanager"
];
shell = pkgs.zsh;
}) machine.users;
home-manager.users = lib.mapAttrs (name: user: {
imports = [ user.account.baseModule ];
meta = {
inherit machine user;
};
home = {
username = name;
homeDirectory = user.account.homeDirectory;
stateVersion = machine.hmStateVersion;
};
}) machine.users;
};
mkWorkstationHost =
machine:
{ ... }:
{
imports = [
(mkHost machine)
nixosModules.workstation-base
];
users.users = lib.mapAttrs (_: _: {
extraGroups = [ "networkmanager" ];
}) machine.users;
home-manager.users = lib.mapAttrs (_: user: {
imports = [
hmModules.workstation-base
user.account.workstationModule
];
}) machine.users;
};
in
{
options.repo.helpers = lib.mkOption {
type = lib.types.attrsOf lib.types.raw;
internal = true;
readOnly = true;
};
config.repo.helpers = {
inherit
mkCaddyReverseProxy
mkHost
mkWorkstationHost
resolvePackagePath
;
};
}