refactor(flake): split assembly by concept
This commit is contained in:
@@ -4,93 +4,59 @@
|
||||
self,
|
||||
}:
|
||||
let
|
||||
# TODO: refactor the registry code
|
||||
registryAssembly = import ./registry.nix { inherit lib self; };
|
||||
|
||||
collectNixFiles =
|
||||
dir:
|
||||
lib.flatten (
|
||||
lib.mapAttrsToList (
|
||||
name: type:
|
||||
let
|
||||
path = dir + "/${name}";
|
||||
in
|
||||
if type == "directory" then
|
||||
collectNixFiles path
|
||||
else if type == "regular" && lib.hasSuffix ".nix" name then
|
||||
[ path ]
|
||||
else
|
||||
[ ]
|
||||
) (builtins.readDir dir)
|
||||
);
|
||||
util = import ../lib { inherit lib; };
|
||||
mkModules = import ./module.nix { inherit util; };
|
||||
|
||||
moduleFiles = collectNixFiles (self + "/modules");
|
||||
guestFiles = collectNixFiles (self + "/guests");
|
||||
modules = mkModules {
|
||||
root = self + "/modules";
|
||||
};
|
||||
|
||||
#----------------------------------------------------------
|
||||
guestFiles = util.collectNixFiles (self + "/guests");
|
||||
|
||||
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
|
||||
|
||||
specialArgs = {
|
||||
inherit
|
||||
dotfiles
|
||||
flake
|
||||
inputs
|
||||
self
|
||||
;
|
||||
registry = collectedRegistry;
|
||||
};
|
||||
|
||||
bridgeName = id: "br-vlan${toString id}";
|
||||
flake = {
|
||||
inherit (hosts) mkHosts;
|
||||
inherit (guests) mkGuest;
|
||||
|
||||
dotfiles = {
|
||||
inherit (guests) guest;
|
||||
|
||||
lib = {
|
||||
inherit bridgeName;
|
||||
inherit (guests) deriveMac;
|
||||
};
|
||||
inherit util;
|
||||
};
|
||||
|
||||
guests = import ./guests.nix {
|
||||
guests = import ./guest.nix {
|
||||
inherit
|
||||
inputs
|
||||
lib
|
||||
self
|
||||
specialArgs
|
||||
bridgeName
|
||||
moduleFiles
|
||||
registryAssembly
|
||||
util
|
||||
;
|
||||
repositoryModules = modules;
|
||||
};
|
||||
|
||||
mkHost =
|
||||
{
|
||||
hostName,
|
||||
system ? "x86_64-linux",
|
||||
}:
|
||||
inputs.nixpkgs.lib.nixosSystem {
|
||||
inherit system specialArgs;
|
||||
modules =
|
||||
moduleFiles
|
||||
++ guestFiles
|
||||
++ [
|
||||
inputs.chaotic.nixosModules.default
|
||||
inputs.disko.nixosModules.disko
|
||||
inputs.sops-nix.nixosModules.sops
|
||||
inputs.stylix.nixosModules.stylix
|
||||
registryAssembly.module
|
||||
(self + "/hosts/${hostName}")
|
||||
{
|
||||
networking.hostName = hostName;
|
||||
modules.host.enable = true;
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
mkHosts =
|
||||
hostsDir:
|
||||
let
|
||||
hostNames = lib.attrNames (lib.filterAttrs (_name: type: type == "directory") (builtins.readDir hostsDir));
|
||||
in
|
||||
lib.genAttrs hostNames (hostName: mkHost { inherit hostName; });
|
||||
hosts = import ./host.nix {
|
||||
inherit
|
||||
inputs
|
||||
lib
|
||||
self
|
||||
specialArgs
|
||||
guestFiles
|
||||
registryAssembly
|
||||
;
|
||||
repositoryModules = modules;
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit mkHosts;
|
||||
}
|
||||
flake
|
||||
|
||||
@@ -3,22 +3,12 @@
|
||||
lib,
|
||||
self,
|
||||
specialArgs,
|
||||
bridgeName,
|
||||
moduleFiles,
|
||||
repositoryModules,
|
||||
registryAssembly,
|
||||
util,
|
||||
}:
|
||||
let
|
||||
inherit (lib) escapeShellArg mapAttrsToList;
|
||||
|
||||
deriveMac =
|
||||
name:
|
||||
let
|
||||
hash = builtins.hashString "sha256" name;
|
||||
octet = i: builtins.substring (i * 2) 2 hash;
|
||||
in
|
||||
lib.concatStringsSep ":" ([ "02" ] ++ map octet [ 0 1 2 3 4 ]);
|
||||
|
||||
guest =
|
||||
mkGuest =
|
||||
{
|
||||
name,
|
||||
interior ? { },
|
||||
@@ -59,13 +49,13 @@ let
|
||||
|
||||
createdMounts = lib.filterAttrs (_guestPath: m: m.create != null) cfg.mounts;
|
||||
mountCreateScript = lib.concatStringsSep "\n" (
|
||||
mapAttrsToList (
|
||||
lib.mapAttrsToList (
|
||||
_guestPath: m:
|
||||
let
|
||||
create = m.create;
|
||||
in
|
||||
''
|
||||
${config.system.path}/bin/install -d -o ${escapeShellArg (toString create.owner)} -g ${escapeShellArg create.group} -m ${escapeShellArg create.mode} ${escapeShellArg m.hostPath}
|
||||
${config.system.path}/bin/install -d -o ${lib.escapeShellArg (toString create.owner)} -g ${lib.escapeShellArg create.group} -m ${lib.escapeShellArg create.mode} ${lib.escapeShellArg m.hostPath}
|
||||
''
|
||||
) createdMounts
|
||||
);
|
||||
@@ -113,7 +103,7 @@ let
|
||||
};
|
||||
mac = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = deriveMac name;
|
||||
default = util.deriveMac name;
|
||||
defaultText = lib.literalMD "a stable address derived from the guest's namespace path";
|
||||
example = "bc:24:11:00:00:01";
|
||||
description = ''
|
||||
@@ -316,7 +306,7 @@ let
|
||||
}
|
||||
(lib.mkIf networked (
|
||||
let
|
||||
bridgeDevice = "sys-subsystem-net-devices-${lib.replaceStrings [ "-" ] [ "\\x2d" ] (bridgeName cfg.vlan)}.device";
|
||||
bridgeDevice = "sys-subsystem-net-devices-${lib.replaceStrings [ "-" ] [ "\\x2d" ] (util.bridgeName cfg.vlan)}.device";
|
||||
in
|
||||
{
|
||||
after = [ bridgeDevice ];
|
||||
@@ -329,7 +319,7 @@ let
|
||||
containers.${machineName} = lib.mkIf (cfg.backend == "container") {
|
||||
autoStart = cfg.autoStart;
|
||||
privateNetwork = lib.mkDefault true;
|
||||
hostBridge = lib.mkIf networked (bridgeName cfg.vlan);
|
||||
hostBridge = lib.mkIf networked (util.bridgeName cfg.vlan);
|
||||
privateUsers = lib.mkDefault "no";
|
||||
additionalCapabilities = lib.optionals cfg.nesting [ "CAP_NET_ADMIN" ];
|
||||
allowedDevices = lib.optionals cfg.nesting [
|
||||
@@ -349,7 +339,9 @@ let
|
||||
|
||||
config = {
|
||||
imports =
|
||||
moduleFiles
|
||||
[
|
||||
repositoryModules
|
||||
]
|
||||
++ [
|
||||
registryAssembly.module
|
||||
inputs.sops-nix.nixosModules.sops
|
||||
@@ -366,5 +358,5 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit deriveMac guest;
|
||||
inherit mkGuest;
|
||||
}
|
||||
46
flake/host.nix
Normal file
46
flake/host.nix
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
inputs,
|
||||
lib,
|
||||
self,
|
||||
specialArgs,
|
||||
repositoryModules,
|
||||
guestFiles,
|
||||
registryAssembly,
|
||||
}:
|
||||
let
|
||||
mkHost =
|
||||
{
|
||||
hostName,
|
||||
system ? "x86_64-linux",
|
||||
}:
|
||||
inputs.nixpkgs.lib.nixosSystem {
|
||||
inherit system specialArgs;
|
||||
modules =
|
||||
[
|
||||
repositoryModules
|
||||
]
|
||||
++ guestFiles
|
||||
++ [
|
||||
inputs.chaotic.nixosModules.default
|
||||
inputs.disko.nixosModules.disko
|
||||
inputs.sops-nix.nixosModules.sops
|
||||
inputs.stylix.nixosModules.stylix
|
||||
registryAssembly.module
|
||||
(self + "/hosts/${hostName}")
|
||||
{
|
||||
networking.hostName = hostName;
|
||||
modules.host.enable = true;
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
mkHosts =
|
||||
hostsDir:
|
||||
let
|
||||
hostNames = lib.attrNames (lib.filterAttrs (_name: type: type == "directory") (builtins.readDir hostsDir));
|
||||
in
|
||||
lib.genAttrs hostNames (hostName: mkHost { inherit hostName; });
|
||||
in
|
||||
{
|
||||
inherit mkHosts;
|
||||
}
|
||||
5
flake/module.nix
Normal file
5
flake/module.nix
Normal file
@@ -0,0 +1,5 @@
|
||||
{ util }:
|
||||
{ root }:
|
||||
{
|
||||
imports = util.collectNixFiles root;
|
||||
}
|
||||
Reference in New Issue
Block a user