server-config/lib.nix
2025-08-20 12:31:31 -04:00

79 lines
1.7 KiB
Nix

{
inputs,
pkgs,
...
}:
inputs.nixpkgs.lib.extend (
final: prev:
let
lib = prev;
in
{
serviceMountDeps =
serviceName: dirs:
{ pkgs, ... }:
{
systemd.services."${serviceName}_mounts" = {
wants = [ "zfs.target" ];
before = [ "${serviceName}.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${lib.getExe pkgs.ensureZfsMounts} ${lib.strings.concatStringsSep " " dirs}";
};
};
systemd.services.${serviceName} = {
wants = [ "${serviceName}_mounts.service" ];
after = [ "${serviceName}_mounts.service" ];
requires = [ "${serviceName}_mounts.service" ];
};
};
# stolen from: https://stackoverflow.com/a/42398526
optimizeWithFlags =
pkg: flags:
lib.overrideDerivation pkg (
old:
let
newflags = lib.foldl' (acc: x: "${acc} ${x}") "" flags;
oldflags = if (lib.hasAttr "NIX_CFLAGS_COMPILE" old) then "${old.NIX_CFLAGS_COMPILE}" else "";
in
{
NIX_CFLAGS_COMPILE = "${oldflags} ${newflags}";
# stdenv = pkgs.clang19Stdenv;
}
);
optimizePackage =
pkg:
final.optimizeWithFlags pkg [
"-O3"
"-march=znver3"
"-mtune=znver3"
];
vpnNamespaceOpenPort =
port:
{ ... }:
{
vpnNamespaces.wg = {
portMappings = [
{
from = port;
to = port;
}
];
openVPNPorts = [
{
port = port;
protocol = "both";
}
];
};
};
}
)