refactor(dotfiles): share Nix file discovery

This commit is contained in:
2026-09-26 22:58:30 -04:00
parent 09a2093f90
commit 0cfd7c260c
4 changed files with 25 additions and 22 deletions

View File

@@ -17,7 +17,7 @@ let
util = import ./util.nix { inherit lib; };
registry = import ./registry.nix {
inherit lib;
inherit lib util;
hosts = self.nixosConfigurations;
root = root + "/registry";
};

View File

@@ -8,6 +8,7 @@
util,
}:
let
files = util.collectNixFiles root;
mkGuest =
name:
{ config, lib, ... }:
@@ -167,22 +168,22 @@ let
config = lib.mkIf cfg.enable {
assertions =
let
hasDirectorySettings = mount: mount.owner != null || mount.group != null || mount.mode != null;
hasCompleteDirectorySettings =
mount: mount.owner != null && mount.group != null && mount.mode != null;
hasOneSource = mount: (mount.directory != null) != (mount.file != null);
mountsWithInvalidSource = lib.attrNames (
lib.filterAttrs (_: mount: !hasOneSource mount) cfg.mounts
);
hasOneSource = mount: (mount.directory != null) != (mount.file != null);
fileMountsWithHostSettings = lib.attrNames (
lib.filterAttrs (_: mount: mount.file != null && hasDirectorySettings mount) cfg.mounts
);
hasDirectorySettings = mount: mount.owner != null || mount.group != null || mount.mode != null;
directoryMountsWithPartialHostSettings = lib.attrNames (
lib.filterAttrs (
_: mount:
mount.directory != null && hasDirectorySettings mount && !hasCompleteDirectorySettings mount
) cfg.mounts
);
hasCompleteDirectorySettings =
mount: mount.owner != null && mount.group != null && mount.mode != null;
in
[
{
@@ -275,11 +276,7 @@ let
};
};
files = util.collectNixFiles root;
guestFiles = lib.filterAttrs (file: type: type == "regular" && lib.hasSuffix ".nix" file) (
builtins.readDir root
);
names = map (file: lib.removeSuffix ".nix" file) (lib.attrNames guestFiles);
names = map (path: lib.removeSuffix ".nix" (builtins.baseNameOf path)) files;
in
{
imports = files ++ map mkGuest names;

View File

@@ -2,20 +2,14 @@
hosts,
lib,
root,
util,
}:
let
definitions =
lib.mapAttrs'
(
name: _type:
let
path = root + "/${name}";
in
lib.nameValuePair (registryName name) (mkRegistry path)
)
(
lib.filterAttrs (name: type: type == "regular" && lib.hasSuffix ".nix" name) (builtins.readDir root)
);
definitions = lib.listToAttrs (
map (path: lib.nameValuePair (registryName (builtins.baseNameOf path)) (mkRegistry path)) (
util.collectNixFiles root
)
);
registryName = fileName: lib.removeSuffix ".nix" fileName;

View File

@@ -0,0 +1,12 @@
## Discover Nix files
### Rule
Use `util.collectNixFiles` to discover Nix files.
Do not use `builtins.readDir` with a `.nix` suffix filter for this purpose.
Pass `util` to Nix functions that need the helper.
### Rationale
`util.collectNixFiles` centralizes recursive discovery of Nix files.
Repository Nix code uses one implementation.