Restructure flake.nix for a simpler config for building different images off the same NixOS config.

This commit is contained in:
Tom Alexander
2025-10-11 00:08:02 -04:00
parent 69b5cf9217
commit 3bf84445a3
121 changed files with 2937 additions and 3074 deletions

View File

@@ -5,52 +5,72 @@
...
}:
let
activatedWg = name: {
networking.wg-quick.interfaces."${name}".configFile = "/persist/manual/wireguard/${name}.conf";
systemd.services."wg-quick-${name}" = {
after = [
"network-online.target"
"nss-lookup.target"
];
preStart = "${pkgs.toybox}/bin/sleep 3";
};
};
deactivatedWg = name: {
networking.wg-quick.interfaces."${name}" = {
configFile = "/persist/manual/wireguard/${name}.conf";
autostart = false;
};
};
wireguard_enable = (config.me.wireguard.activated != [ ] || config.me.wireguard.deactivated != [ ]);
wireguard_enable = (
(lib.attrsets.filterAttrs (name: value: value) config.me.wireguard.activated) != [ ]
|| (lib.attrsets.filterAttrs (name: value: value) config.me.wireguard.deactivated) != [ ]
);
in
{
imports = [ ];
options.me = {
wireguard.activated = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = false;
example = true;
default = { };
example = lib.literalExpression ''
{
wgh = true;
colo = lib.mkForce false;
}
'';
type = lib.types.coercedTo (lib.types.listOf lib.types.str) (
enabled: lib.listToAttrs (map (fs: lib.nameValuePair fs true) enabled)
) (lib.types.attrsOf lib.types.bool);
description = "List of wireguard config names that should be activated at boot.";
};
wireguard.deactivated = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = false;
example = true;
default = { };
example = lib.literalExpression ''
{
wgf = true;
drmario = lib.mkForce false;
}
'';
type = lib.types.coercedTo (lib.types.listOf lib.types.str) (
enabled: lib.listToAttrs (map (fs: lib.nameValuePair fs true) enabled)
) (lib.types.attrsOf lib.types.bool);
description = "List of wireguard config names that are not activated at boot but can be manually activated later.";
};
};
config = lib.mkIf wireguard_enable (
lib.mkMerge [
{
networking.firewall.allowedUDPPorts = [ 51821 ];
networking.wireguard.enable = true;
}
(activatedWg "drmario")
(activatedWg "wgh")
(activatedWg "colo")
(deactivatedWg "wgf")
]
);
config = lib.mkIf wireguard_enable {
networking.firewall.allowedUDPPorts = [ 51821 ];
networking.wireguard.enable = true;
networking.wg-quick.interfaces =
(builtins.mapAttrs (
name: value:
(lib.attrsets.optionalAttrs value {
configFile = "/persist/manual/wireguard/${name}.conf";
})
) config.me.wireguard.activated)
// (builtins.mapAttrs (
name: value:
(lib.attrsets.optionalAttrs value {
configFile = "/persist/manual/wireguard/${name}.conf";
autostart = false;
})
) config.me.wireguard.deactivated);
systemd.services = lib.attrsets.mapAttrs' (
name: value:
(lib.attrsets.nameValuePair "wg-quick-${name}" {
after = [
"network-online.target"
"nss-lookup.target"
];
preStart = "${pkgs.toybox}/bin/sleep 3";
})
) config.me.wireguard.activated;
};
}