refactor(dotfiles): share Nix file discovery
This commit is contained in:
@@ -17,7 +17,7 @@ let
|
|||||||
util = import ./util.nix { inherit lib; };
|
util = import ./util.nix { inherit lib; };
|
||||||
|
|
||||||
registry = import ./registry.nix {
|
registry = import ./registry.nix {
|
||||||
inherit lib;
|
inherit lib util;
|
||||||
hosts = self.nixosConfigurations;
|
hosts = self.nixosConfigurations;
|
||||||
root = root + "/registry";
|
root = root + "/registry";
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
util,
|
util,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
files = util.collectNixFiles root;
|
||||||
mkGuest =
|
mkGuest =
|
||||||
name:
|
name:
|
||||||
{ config, lib, ... }:
|
{ config, lib, ... }:
|
||||||
@@ -167,22 +168,22 @@ let
|
|||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
assertions =
|
assertions =
|
||||||
let
|
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 (
|
mountsWithInvalidSource = lib.attrNames (
|
||||||
lib.filterAttrs (_: mount: !hasOneSource mount) cfg.mounts
|
lib.filterAttrs (_: mount: !hasOneSource mount) cfg.mounts
|
||||||
);
|
);
|
||||||
|
hasOneSource = mount: (mount.directory != null) != (mount.file != null);
|
||||||
fileMountsWithHostSettings = lib.attrNames (
|
fileMountsWithHostSettings = lib.attrNames (
|
||||||
lib.filterAttrs (_: mount: mount.file != null && hasDirectorySettings mount) cfg.mounts
|
lib.filterAttrs (_: mount: mount.file != null && hasDirectorySettings mount) cfg.mounts
|
||||||
);
|
);
|
||||||
|
hasDirectorySettings = mount: mount.owner != null || mount.group != null || mount.mode != null;
|
||||||
directoryMountsWithPartialHostSettings = lib.attrNames (
|
directoryMountsWithPartialHostSettings = lib.attrNames (
|
||||||
lib.filterAttrs (
|
lib.filterAttrs (
|
||||||
_: mount:
|
_: mount:
|
||||||
mount.directory != null && hasDirectorySettings mount && !hasCompleteDirectorySettings mount
|
mount.directory != null && hasDirectorySettings mount && !hasCompleteDirectorySettings mount
|
||||||
) cfg.mounts
|
) cfg.mounts
|
||||||
);
|
);
|
||||||
|
hasCompleteDirectorySettings =
|
||||||
|
mount: mount.owner != null && mount.group != null && mount.mode != null;
|
||||||
in
|
in
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@@ -275,11 +276,7 @@ let
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
files = util.collectNixFiles root;
|
names = map (path: lib.removeSuffix ".nix" (builtins.baseNameOf path)) files;
|
||||||
guestFiles = lib.filterAttrs (file: type: type == "regular" && lib.hasSuffix ".nix" file) (
|
|
||||||
builtins.readDir root
|
|
||||||
);
|
|
||||||
names = map (file: lib.removeSuffix ".nix" file) (lib.attrNames guestFiles);
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
imports = files ++ map mkGuest names;
|
imports = files ++ map mkGuest names;
|
||||||
|
|||||||
@@ -2,20 +2,14 @@
|
|||||||
hosts,
|
hosts,
|
||||||
lib,
|
lib,
|
||||||
root,
|
root,
|
||||||
|
util,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
definitions =
|
definitions = lib.listToAttrs (
|
||||||
lib.mapAttrs'
|
map (path: lib.nameValuePair (registryName (builtins.baseNameOf path)) (mkRegistry path)) (
|
||||||
(
|
util.collectNixFiles root
|
||||||
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)
|
|
||||||
);
|
|
||||||
|
|
||||||
registryName = fileName: lib.removeSuffix ".nix" fileName;
|
registryName = fileName: lib.removeSuffix ".nix" fileName;
|
||||||
|
|
||||||
|
|||||||
12
src/skills/style/nix/rules/general.md
Normal file
12
src/skills/style/nix/rules/general.md
Normal 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.
|
||||||
Reference in New Issue
Block a user