Files
dotfiles/lib/default.nix
alexion c00eed81e9 Trim verbose comments to concise, self-contained notes
Cut restated "what", domain-glossary framing, cross-file consumption
narration, and against-alternative justification from in-file comments;
keep only non-obvious "why" and load-bearing pointers. Drop the
`generateCompletions` line (a no-op restatement of the upstream default)
and its comment.
2026-07-19 07:57:44 -04:00

82 lines
2.0 KiB
Nix

{
lib,
inputs,
self,
}:
let
inherit (lib)
attrNames
filterAttrs
genAttrs
flatten
hasSuffix
mapAttrsToList
;
# Recursively collect every `.nix` file under `dir` as a flat list, for a
# module's `imports`.
collectNixFiles =
dir:
flatten (
mapAttrsToList (
name: type:
let
path = dir + "/${name}";
in
if type == "directory" then
collectNixFiles path
else if type == "regular" && hasSuffix ".nix" name then
[ path ]
else
[ ]
) (builtins.readDir dir)
);
# Build one host: every module is imported unconditionally (inert until its
# `enable` flag is set), alongside home-manager, chaotic, the shared base, and
# the host's own directory.
mkHost =
{
hostName,
system ? "x86_64-linux",
}:
inputs.nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = {
inherit inputs;
my = self.lib;
};
modules =
(collectNixFiles (self + "/modules"))
++ [
inputs.home-manager.nixosModules.home-manager
inputs.chaotic.nixosModules.default
inputs.disko.nixosModules.disko
(self + "/system")
(self + "/hosts/${hostName}")
{ networking.hostName = hostName; }
];
};
# Discover every host (a subdirectory of `hostsDir`) and build each one.
mkHosts =
hostsDir:
let
hostNames = attrNames (filterAttrs (_name: type: type == "directory") (builtins.readDir hostsDir));
in
genAttrs hostNames (hostName: mkHost { inherit hostName; });
# --- Script-from-file helper --------------------------------------------
# Turn a standalone script file into a package on PATH, keeping the script
# itself editable as a real file rather than an inlined heredoc.
scriptFromFile = pkgs: name: path: pkgs.writeShellScriptBin name (builtins.readFile path);
in
{
inherit
collectNixFiles
mkHost
mkHosts
scriptFromFile
;
}