Skip to content

Commit 3ac00f8

Browse files
committed
initial commit
0 parents  commit 3ac00f8

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

flake-module.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sysmodule-flake:
2+
3+
{ self, flake-parts-lib, lib, ... }: {
4+
imports = [
5+
./modules/sys-discovery.nix
6+
];
7+
8+
config._module.args = { inherit sysmodule-flake; };
9+
10+
_file = __curPos.file;
11+
}

flake.lock

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
description = "Importable flake.parts module for system modules";
3+
4+
inputs = {
5+
flake-parts.url = "github:hercules-ci/flake-parts";
6+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
7+
};
8+
9+
outputs = inputs: inputs.flake-parts.lib.mkFlake { inherit inputs; } ({ withSystem, flake-parts-lib, ... }: {
10+
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
11+
perSystem = { config, self', inputs', pkgs, system, ... }: { };
12+
flake = {
13+
flakeModules.default = flake-parts-lib.importApply ./flake-module.nix { inherit withSystem; };
14+
lib = import ./lib.nix inputs.nixpkgs.lib;
15+
};
16+
});
17+
}

lib.nix

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
lib:
2+
3+
let
4+
discoverNixOSConfigFiles = dir:
5+
let
6+
inherit (builtins)
7+
attrNames
8+
filter
9+
listToAttrs
10+
;
11+
names = attrNames (
12+
lib.filterAttrs
13+
(_: v: v == "directory")
14+
(builtins.readDir dir)
15+
);
16+
hasConfigurationNix = name:
17+
builtins.readDir "${dir}/${name}" ? "configuration.nix";
18+
namesWithConfigs = filter hasConfigurationNix names;
19+
toAttrItem = name: {
20+
inherit name;
21+
value = "${dir}/${name}/configuration.nix";
22+
};
23+
in
24+
listToAttrs (map toAttrItem namesWithConfigs);
25+
26+
toNixOSSystem = specialArgs: configPath:
27+
lib.nixosSystem {
28+
inherit specialArgs;
29+
modules = [ configPath ];
30+
};
31+
in
32+
33+
{
34+
inherit
35+
discoverNixOSConfigFiles
36+
toNixOSSystem
37+
;
38+
39+
discoverNixOSConfigs = specialArgs: dir:
40+
let
41+
configs = discoverNixOSConfigFiles dir;
42+
in
43+
builtins.mapAttrs (_: toNixOSSystem specialArgs) configs;
44+
45+
modulesFromDir =
46+
let
47+
getNixFilesInDir = dir: builtins.filter
48+
(file: lib.hasSuffix ".nix" file && file != "default.nix")
49+
(builtins.attrNames (builtins.readDir dir));
50+
genKey = str: lib.replaceStrings [ ".nix" ] [ "" ] str;
51+
moduleFrom = dir: str: { "${genKey str}" = "${dir}/${str}"; };
52+
in
53+
dir:
54+
builtins.foldl' (x: y: x // (moduleFrom dir y)) { } (getNixFilesInDir dir);
55+
}

modules/sys-discovery.nix

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{ config, lib, self, sysmodule-flake, ... }:
2+
3+
let
4+
localLib = import ../lib.nix lib;
5+
cfg = config.sysmodules-flake;
6+
7+
modulesDirs = {
8+
darwinModules = "modules-darwin";
9+
homeManagerModules = "modules-home-manager";
10+
nixosModules = "modules-nixos";
11+
};
12+
13+
configsDirs = {
14+
darwinConfigurations = {
15+
folder = "configs-darwin";
16+
function = specialArgs: configPath: cfg.nix-darwin.lib.darwinSystem {
17+
inherit specialArgs;
18+
modules = [ configPath ];
19+
};
20+
};
21+
nixosConfigurations = {
22+
folder = "configs-nixos";
23+
function = specialArgs: configPath: lib.nixosSystem {
24+
inherit specialArgs;
25+
modules = [ configPath ];
26+
};
27+
};
28+
};
29+
30+
fileInPathExists = path: file:
31+
let
32+
paths = builtins.attrNames (builtins.readDir path);
33+
in
34+
builtins.elem file paths;
35+
36+
commonAttrs = pathAttrs: pathFunction:
37+
let
38+
existingPaths = lib.filterAttrs
39+
(_: fileInPathExists cfg.modulesPath)
40+
pathAttrs;
41+
f = _: value: pathFunction "${cfg.modulesPath}/${value}";
42+
in
43+
builtins.mapAttrs f existingPaths;
44+
45+
modulesAttrs = commonAttrs modulesDirs localLib.modulesFromDir;
46+
47+
configAttrs =
48+
let
49+
specialArgs = cfg.specialArgs // { inherit (cfg) flakeInputs; };
50+
existingPaths = lib.filterAttrs
51+
(_: { folder, ... }: fileInPathExists cfg.modulesPath folder)
52+
configsDirs;
53+
toConfigs = _: { folder, function }:
54+
let
55+
configs = localLib.discoverNixOSConfigFiles "${cfg.modulesPath}/${folder}";
56+
in
57+
builtins.mapAttrs (_: function specialArgs) configs;
58+
in
59+
builtins.mapAttrs toConfigs existingPaths;
60+
61+
in
62+
{
63+
options.sysmodules-flake = {
64+
modulesPath = lib.mkOption {
65+
type = lib.types.path;
66+
description = "Path with module folders configs.";
67+
default = builtins.toPath self;
68+
};
69+
flakeInputs = lib.mkOption {
70+
type = lib.types.attrs;
71+
default = self.inputs;
72+
};
73+
specialArgs = lib.mkOption {
74+
type = lib.types.attrs;
75+
default = { };
76+
};
77+
nix-darwin = lib.mkOption {
78+
type = lib.types.nullOr lib.types.attrs;
79+
default = null;
80+
};
81+
};
82+
config.flake = configAttrs // modulesAttrs;
83+
}

0 commit comments

Comments
 (0)