Skip to content

Commit

Permalink
feat: import configuration based on convention
Browse files Browse the repository at this point in the history
  • Loading branch information
squirmy committed Apr 6, 2024
1 parent f45e6c7 commit a76fd48
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
12 changes: 11 additions & 1 deletion flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
}: let
cfg = config.nix-machine;

configurations = map (x: x.value) (lib.attrsets.attrsToList cfg.configurations);
parseConfig = import ./lib/parse-config.nix {inherit lib;};

configurations = map (x: x.value) (lib.attrsets.attrsToList (parseConfig cfg.configurations));

sharedOptions = lib.catAttrs "options" configurations;
nixDarwinConfigurations = lib.catAttrs "nixDarwin" configurations;
Expand All @@ -30,6 +32,14 @@
type = lib.types.deferredModule;
default = {};
};
path = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
};
scheme = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
};
in {
options.nix-machine.configurations = lib.mkOption {
Expand Down
35 changes: 35 additions & 0 deletions lib/parse-config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{lib, ...}: let
enumerateModules = basePath: fileName: let
inherit (builtins) readDir;
inherit (lib) attrNames filter filterAttrs pathExists foldl';
inherit (lib.trivial) pipe;

packagePaths = path: attrNames (filterAttrs (_: type: type == "directory") (readDir path));
modulesInPackage = package: [package "${fileName}.nix"];
renderPath = foldl' (path: elem: path + "/${elem}");

modules = pipe (packagePaths basePath) [
(map modulesInPackage)
(map (renderPath basePath))
(filter pathExists)
];
in
{...}: {
imports = modules;
};

importConfig = path: let
createModule = enumerateModules path;
in {
options = createModule "options";
nixDarwin = createModule "darwin-module";
homeManager = createModule "hm-module";
};

# If a path is specified,
main = configuration:
if configuration.path != null && configuration.scheme == "flat"
then importConfig configuration.path
else configuration;
in
configurations: builtins.mapAttrs (c: v: main v) configurations

0 comments on commit a76fd48

Please sign in to comment.