refactor(flake): assemble repository modules

This commit is contained in:
2026-08-28 16:16:40 -04:00
parent 0f9b639c0d
commit 81732aef84
7 changed files with 36 additions and 22 deletions

View File

@@ -53,7 +53,7 @@ Use the simplest accurate word when a term is not listed.
| assembly file | Nix | A Nix file under `flake/` that constructs flake outputs or arguments passed to NixOS modules. | | assembly file | Nix | A Nix file under `flake/` that constructs flake outputs or arguments passed to NixOS modules. |
| assembly function | Nix | A project function exposed under `flake` because it creates modules, systems, or assembly values. | | assembly function | Nix | A project function exposed under `flake` because it creates modules, systems, or assembly values. |
| library file | Nix | A Nix file that defines reusable project functions instead of a NixOS module or flake output. | | library file | Nix | A Nix file that defines reusable project functions instead of a NixOS module or flake output. |
| library function | Nix | A pure project function exposed under `flake.lib`. | | library function | Nix | A pure project function exposed under `flake.util`. |
| NixOS module | Nix | A Nix file evaluated by `nixosSystem` for a NixOS configuration. | | NixOS module | Nix | A Nix file evaluated by `nixosSystem` for a NixOS configuration. |
| overlay | Nix | A function that modifies a Nix package set by adding or replacing packages. | | overlay | Nix | A function that modifies a Nix package set by adding or replacing packages. |
| workspace | Window management | A named or numbered group of windows in a graphical session. Some desktop environments call this a virtual desktop. | | workspace | Window management | A named or numbered group of windows in a graphical session. Some desktop environments call this a virtual desktop. |

View File

@@ -10,14 +10,14 @@ Read this document with `docs/dictionary.md`.
Use `lib` only for the base `nixpkgs.lib` value. Use `lib` only for the base `nixpkgs.lib` value.
Do not use `lib` for project functions. Do not use `lib` for project functions.
Use `flake.lib` for library functions passed through module arguments. Use `flake.util` for library functions passed through module arguments.
Use `flake.mk<Name>` for assembly functions passed through module arguments. Use `flake.mk<Name>` for assembly functions passed through module arguments.
Avoid: Avoid:
```nix ```nix
specialArgs = { specialArgs = {
lib = flakeLib; lib = util;
}; };
``` ```
@@ -38,20 +38,20 @@ Then call project functions through `flake` inside modules:
... ...
}: }:
{ {
networking.networkmanager.unmanaged = [ "interface-name:${flake.lib.bridgeName 10}" ]; networking.networkmanager.unmanaged = [ "interface-name:${flake.util.bridgeName 10}" ];
} }
``` ```
### Name project function namespaces by role ### Name project function namespaces by role
Put pure project functions under `flake.lib`. Put pure project functions under `flake.util`.
Put functions that create modules, systems, or assembly values under `flake`. Put functions that create modules, systems, or assembly values under `flake`.
This keeps data-only functions separate from functions that carry flake context. This keeps data-only functions separate from functions that carry flake context.
Avoid: Avoid:
```nix ```nix
flake.lib.mkGuest { name = "sample"; } flake.util.mkGuest { name = "sample"; }
``` ```
Use: Use:

View File

@@ -6,10 +6,15 @@
let let
# TODO: refactor the registry code # TODO: refactor the registry code
registryAssembly = import ./registry.nix { inherit lib self; }; registryAssembly = import ./registry.nix { inherit lib self; };
flakeLib = import ../lib { inherit lib; }; # TODO: remove this after the refactor of this file is done and clean
util = import ../lib { inherit lib; };
mkModules = import ./modules.nix { inherit util; };
moduleFiles = flakeLib.collectNixFiles (self + "/modules"); modules = mkModules {
guestFiles = flakeLib.collectNixFiles (self + "/guests"); root = self + "/modules";
};
repositoryModules = modules;
guestFiles = util.collectNixFiles (self + "/guests");
collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; }; collectedRegistry = registryAssembly.collect { hosts = self.nixosConfigurations; };
@@ -26,7 +31,7 @@ let
inherit (hosts) mkHosts; inherit (hosts) mkHosts;
inherit (guests) mkGuest; inherit (guests) mkGuest;
lib = flakeLib; inherit util;
}; };
guests = import ./guests.nix { guests = import ./guests.nix {
@@ -35,9 +40,9 @@ let
lib lib
self self
specialArgs specialArgs
moduleFiles repositoryModules
registryAssembly registryAssembly
flakeLib util
; ;
}; };
@@ -47,7 +52,7 @@ let
lib lib
self self
specialArgs specialArgs
moduleFiles repositoryModules
guestFiles guestFiles
registryAssembly registryAssembly
; ;

View File

@@ -3,9 +3,9 @@
lib, lib,
self, self,
specialArgs, specialArgs,
moduleFiles, repositoryModules,
registryAssembly, registryAssembly,
flakeLib, util,
}: }:
let let
mkGuest = mkGuest =
@@ -103,7 +103,7 @@ let
}; };
mac = lib.mkOption { mac = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = flakeLib.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 = ''
@@ -306,7 +306,7 @@ let
} }
(lib.mkIf networked ( (lib.mkIf networked (
let let
bridgeDevice = "sys-subsystem-net-devices-${lib.replaceStrings [ "-" ] [ "\\x2d" ] (flakeLib.bridgeName cfg.vlan)}.device"; bridgeDevice = "sys-subsystem-net-devices-${lib.replaceStrings [ "-" ] [ "\\x2d" ] (util.bridgeName cfg.vlan)}.device";
in in
{ {
after = [ bridgeDevice ]; after = [ bridgeDevice ];
@@ -319,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 (flakeLib.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 [
@@ -339,7 +339,9 @@ let
config = { config = {
imports = imports =
moduleFiles [
repositoryModules
]
++ [ ++ [
registryAssembly.module registryAssembly.module
inputs.sops-nix.nixosModules.sops inputs.sops-nix.nixosModules.sops

View File

@@ -3,7 +3,7 @@
lib, lib,
self, self,
specialArgs, specialArgs,
moduleFiles, repositoryModules,
guestFiles, guestFiles,
registryAssembly, registryAssembly,
}: }:
@@ -16,7 +16,9 @@ let
inputs.nixpkgs.lib.nixosSystem { inputs.nixpkgs.lib.nixosSystem {
inherit system specialArgs; inherit system specialArgs;
modules = modules =
moduleFiles [
repositoryModules
]
++ guestFiles ++ guestFiles
++ [ ++ [
inputs.chaotic.nixosModules.default inputs.chaotic.nixosModules.default

5
flake/modules.nix Normal file
View File

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

View File

@@ -8,7 +8,7 @@
let let
cfg = config.modules.network.vlans; cfg = config.modules.network.vlans;
inherit (flake.lib) bridgeName; inherit (flake.util) bridgeName;
vlanName = id: "${cfg.trunk}.${toString id}"; vlanName = id: "${cfg.trunk}.${toString id}";
in in