refactor(flake): split assembly code #73

Merged
alexion merged 10 commits from refactor/flake-assembly into main 2026-08-29 17:21:41 -04:00
14 changed files with 149 additions and 97 deletions
Showing only changes of commit 647b20b934 - Show all commits

View File

@@ -4,93 +4,59 @@
self, self,
}: }:
let let
# TODO: refactor the registry code
registryAssembly = import ./registry.nix { inherit lib self; }; registryAssembly = import ./registry.nix { inherit lib self; };
collectNixFiles = util = import ../lib { inherit lib; };
dir: mkModules = import ./module.nix { inherit util; };
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)
);
moduleFiles = collectNixFiles (self + "/modules"); modules = mkModules {
guestFiles = collectNixFiles (self + "/guests"); root = self + "/modules";
};
#----------------------------------------------------------
guestFiles = util.collectNixFiles (self + "/guests");
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; }; collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
specialArgs = { specialArgs = {
inherit inherit
dotfiles flake
inputs inputs
self self
; ;
registry = collectedRegistry; registry = collectedRegistry;
}; };
bridgeName = id: "br-vlan${toString id}"; flake = {
inherit (hosts) mkHosts;
inherit (guests) mkGuest;
dotfiles = { inherit util;
inherit (guests) guest;
lib = {
inherit bridgeName;
inherit (guests) deriveMac;
};
}; };
guests = import ./guests.nix { guests = import ./guest.nix {
inherit inherit
inputs inputs
lib lib
self self
specialArgs specialArgs
bridgeName
moduleFiles
registryAssembly registryAssembly
util
; ;
repositoryModules = modules;
}; };
mkHost = hosts = import ./host.nix {
{ inherit
hostName, inputs
system ? "x86_64-linux", lib
}: self
inputs.nixpkgs.lib.nixosSystem { specialArgs
inherit system specialArgs; guestFiles
modules = registryAssembly
moduleFiles ;
++ guestFiles repositoryModules = modules;
++ [ };
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 in
{ flake
inherit mkHosts;
}

View File

@@ -3,22 +3,12 @@
lib, lib,
self, self,
specialArgs, specialArgs,
bridgeName, repositoryModules,
moduleFiles,
registryAssembly, registryAssembly,
util,
}: }:
let let
inherit (lib) escapeShellArg mapAttrsToList; mkGuest =
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 =
{ {
name, name,
interior ? { }, interior ? { },
@@ -59,13 +49,13 @@ let
createdMounts = lib.filterAttrs (_guestPath: m: m.create != null) cfg.mounts; createdMounts = lib.filterAttrs (_guestPath: m: m.create != null) cfg.mounts;
mountCreateScript = lib.concatStringsSep "\n" ( mountCreateScript = lib.concatStringsSep "\n" (
mapAttrsToList ( lib.mapAttrsToList (
_guestPath: m: _guestPath: m:
let let
create = m.create; create = m.create;
in 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 ) createdMounts
); );
@@ -113,7 +103,7 @@ let
}; };
mac = lib.mkOption { mac = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = deriveMac name; default = util.deriveMac name;
defaultText = lib.literalMD "a stable address derived from the guest's namespace path"; defaultText = lib.literalMD "a stable address derived from the guest's namespace path";
example = "bc:24:11:00:00:01"; example = "bc:24:11:00:00:01";
description = '' description = ''
@@ -316,7 +306,7 @@ let
} }
(lib.mkIf networked ( (lib.mkIf networked (
let 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 in
{ {
after = [ bridgeDevice ]; after = [ bridgeDevice ];
@@ -329,7 +319,7 @@ let
containers.${machineName} = lib.mkIf (cfg.backend == "container") { containers.${machineName} = lib.mkIf (cfg.backend == "container") {
autoStart = cfg.autoStart; autoStart = cfg.autoStart;
privateNetwork = lib.mkDefault true; privateNetwork = lib.mkDefault true;
hostBridge = lib.mkIf networked (bridgeName cfg.vlan); hostBridge = lib.mkIf networked (util.bridgeName cfg.vlan);
privateUsers = lib.mkDefault "no"; privateUsers = lib.mkDefault "no";
additionalCapabilities = lib.optionals cfg.nesting [ "CAP_NET_ADMIN" ]; additionalCapabilities = lib.optionals cfg.nesting [ "CAP_NET_ADMIN" ];
allowedDevices = lib.optionals cfg.nesting [ allowedDevices = lib.optionals cfg.nesting [
@@ -349,7 +339,9 @@ let
config = { config = {
imports = imports =
moduleFiles [
repositoryModules
]
++ [ ++ [
registryAssembly.module registryAssembly.module
inputs.sops-nix.nixosModules.sops inputs.sops-nix.nixosModules.sops
@@ -366,5 +358,5 @@ let
}; };
in in
{ {
inherit deriveMac guest; inherit mkGuest;
} }

46
flake/host.nix Normal file
View 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
View File

@@ -0,0 +1,5 @@
{ util }:
{ root }:
{
imports = util.collectNixFiles root;
}

View File

@@ -1,7 +1,7 @@
{ {
config, config,
lib, lib,
dotfiles, flake,
... ...
}: }:
let let
@@ -10,7 +10,7 @@ let
in in
{ {
imports = [ imports = [
(dotfiles.guest { (flake.mkGuest {
name = "actualbudget"; name = "actualbudget";
interior = { interior = {
modules.services.actualbudget = { modules.services.actualbudget = {

View File

@@ -1,7 +1,7 @@
{ {
config, config,
lib, lib,
dotfiles, flake,
... ...
}: }:
let let
@@ -35,7 +35,7 @@ let
in in
{ {
imports = [ imports = [
(dotfiles.guest { (flake.mkGuest {
name = "copyparty"; name = "copyparty";
interior = { interior = {
modules.services.copyparty = { modules.services.copyparty = {

View File

@@ -1,7 +1,7 @@
args@{ dotfiles, ... }: args@{ flake, ... }:
# A sample guest whose interior runs an OCI container on Podman. # A sample guest whose interior runs an OCI container on Podman.
# The image is pulled at runtime, so the guest builds with no build-time fetch. # The image is pulled at runtime, so the guest builds with no build-time fetch.
dotfiles.guest { flake.mkGuest {
name = "nesting-sample"; name = "nesting-sample";
interior = { interior = {
virtualisation.oci-containers.containers.hello = { virtualisation.oci-containers.containers.hello = {

View File

@@ -1,7 +1,7 @@
{ {
config, config,
lib, lib,
dotfiles, flake,
... ...
}: }:
let let
@@ -9,7 +9,7 @@ let
in in
{ {
imports = [ imports = [
(dotfiles.guest { (flake.mkGuest {
name = "reverse-proxy"; name = "reverse-proxy";
interior = { interior = {
modules.services.reverse-proxy.enable = true; modules.services.reverse-proxy.enable = true;

View File

@@ -1,5 +1,5 @@
args@{ dotfiles, ... }: args@{ flake, ... }:
# The tracer-bullet guest: the thinnest complete path from discovery to a # The tracer-bullet guest: the thinnest complete path from discovery to a
# running nested container. Its interior is just the guest-base — the baseline # running nested container. Its interior is just the guest-base — the baseline
# toolset and SSH access — so it proves the concept without carrying a service. # toolset and SSH access — so it proves the concept without carrying a service.
dotfiles.guest { name = "sample"; } args flake.mkGuest { name = "sample"; } args

9
lib/default.nix Normal file
View File

@@ -0,0 +1,9 @@
{ lib }:
let
files = import ./files.nix { inherit lib; };
network = import ./network.nix { inherit lib; };
in
{
inherit (files) collectNixFiles;
inherit (network) bridgeName deriveMac;
}

22
lib/files.nix Normal file
View File

@@ -0,0 +1,22 @@
{ lib }:
let
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)
);
in
{
inherit collectNixFiles;
}

12
lib/network.nix Normal file
View File

@@ -0,0 +1,12 @@
{ lib }:
{
bridgeName = id: "br-vlan${toString id}";
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 ]);
}

View File

@@ -1,7 +1,7 @@
{ {
config, config,
lib, lib,
dotfiles, flake,
pkgs, pkgs,
inputs, inputs,
... ...
@@ -64,7 +64,7 @@ in
useGlobalPkgs = true; useGlobalPkgs = true;
useUserPackages = true; useUserPackages = true;
extraSpecialArgs = { extraSpecialArgs = {
inherit dotfiles inputs; inherit flake inputs;
}; };
users.${user.name} = { users.${user.name} = {
home.username = user.name; home.username = user.name;

View File

@@ -1,14 +1,14 @@
{ {
config, config,
lib, lib,
dotfiles, flake,
... ...
}: }:
# The host networking foundation: per-VLAN bridges over a tagged trunk. # The host networking foundation: per-VLAN bridges over a tagged trunk.
let let
cfg = config.modules.network.vlans; cfg = config.modules.network.vlans;
inherit (dotfiles.lib) bridgeName; inherit (flake.util) bridgeName;
vlanName = id: "${cfg.trunk}.${toString id}"; vlanName = id: "${cfg.trunk}.${toString id}";
in in