Working on modularizing stuff
This commit is contained in:
parent
835faf218d
commit
8349809fec
15 changed files with 256 additions and 41 deletions
21
lib/attrs.nix
Normal file
21
lib/attrs.nix
Normal file
|
@ -0,0 +1,21 @@
|
|||
{ lib, ... }:
|
||||
let
|
||||
inherit (lib.lists) any count;
|
||||
inherit (lib.attrsets) filterAttrs listToAttrs mapAttrs' mapAttrsToList;
|
||||
in rec
|
||||
{
|
||||
attrsToList = attrs:
|
||||
mapAttrsToList (name: value: { inherit name value; }) attrs;
|
||||
|
||||
mapFilterAttrs = pred: f: attrs:
|
||||
filterAttrs pred (mapAttrs' f attrs);
|
||||
|
||||
getAttrs' = values: f:
|
||||
listToAttrs (map f values);
|
||||
|
||||
anyAttrs = pred: attrs:
|
||||
any (attr: pred attr.name attr.value) (attrsToList attrs);
|
||||
|
||||
countAttrs = pred: attrs:
|
||||
count (attr: pred attr.name attr.value) (attrsToList attrs);
|
||||
}
|
22
lib/default.nix
Normal file
22
lib/default.nix
Normal file
|
@ -0,0 +1,22 @@
|
|||
{ inputs, lib, pkgs, ... }:
|
||||
let
|
||||
inherit (lib.attrsets) attrValues;
|
||||
inherit (lib.fixedPoints) makeExtensible;
|
||||
inherit (lib.lists) foldr;
|
||||
inherit (modules) mapModules;
|
||||
|
||||
modules = import ./modules.nix {
|
||||
inherit lib;
|
||||
|
||||
self.attrs = import ./attrs.nix {
|
||||
inherit lib;
|
||||
|
||||
self = {};
|
||||
};
|
||||
};
|
||||
|
||||
mylib = makeExensible (self:
|
||||
mapModules ./. (file: import file { inherit self lib pkgs inputs; })
|
||||
);
|
||||
in
|
||||
mylib.extend (self: super: foldr (a: b: a // b) {} (attrValues super))
|
36
lib/modules.nix
Normal file
36
lib/modules.nix
Normal file
|
@ -0,0 +1,36 @@
|
|||
{ lib, self, ... }:
|
||||
let
|
||||
inherit (builtins) attrValues readDir pathExists concatLists;
|
||||
inherit (lib.attrsets) mapAttrsToList filterAttrs nameValuePair;
|
||||
inherit (lib.strings) hasPrefix hasSuffix removeSuffix;
|
||||
inherit (lib.trivial) id;
|
||||
inherit (self.attrs) mapFilterAttrs;
|
||||
in rec
|
||||
{
|
||||
mapModules = dir: fn:
|
||||
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let path = "${toString dir}/${n}"; in
|
||||
if v == "directory" && pathExists "${path}/default.nix"
|
||||
then nameValuePair n (fn path)
|
||||
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n
|
||||
then nameValuePair (removeSuffix ".nix" n) (fn path)
|
||||
else nameValuePair "" null
|
||||
) (readDir dir);
|
||||
|
||||
mapModules' = dir: fn: attrValues (mapModules dir fn);
|
||||
|
||||
mapModulesRec = dir: fn:
|
||||
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let path = "${toString dir}/${n}"; in
|
||||
if v == "directory" && pathExists "${path}/default.nix"
|
||||
then nameValuePair n (mapModulesRec path fn)
|
||||
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n
|
||||
then nameValuePair (removeSuffix ".nix" n) (fn path)
|
||||
else nameValuePair "" null
|
||||
) (readDir dir);
|
||||
|
||||
mapModulesRec' = dir: fn: let
|
||||
dirs = mapAttrsToList (k: _: "${dir}/${k}") (filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n)) (readDir dir));
|
||||
files = attrValues (mapModules dir id);
|
||||
paths = files ++ contactLists (map (d: mapModulesRec' d id) dirs);
|
||||
in
|
||||
map fn paths;
|
||||
}
|
30
lib/nixos.nix
Normal file
30
lib/nixos.nix
Normal file
|
@ -0,0 +1,30 @@
|
|||
{ inputs, lib, pkgs, self, ... }:
|
||||
let
|
||||
inherit (inputs.nixpkgs.lib) nixosSystem;
|
||||
inherit (builtins) baseNameOf elem;
|
||||
inherit (lib.attrsets) filterAttrs;
|
||||
inherit (lib.modules) mkDefault;
|
||||
inherit (lib.strings) removeSuffix;
|
||||
inherit (self.modules) mapModules;
|
||||
in rec
|
||||
{
|
||||
mkHost = path: attrs @ {sytem ? "x86_64-linux", ...}:
|
||||
nixosSystem {
|
||||
inherit system;
|
||||
|
||||
specialArgs = { inherit lib input system; };
|
||||
|
||||
modules = [
|
||||
{
|
||||
nixpkgs.pkgs = pkgs;
|
||||
networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path));
|
||||
}
|
||||
(filterAttrs (n: v: !elem n ["system"]) attrs)
|
||||
../. # ../default.nix
|
||||
(import path)
|
||||
];
|
||||
};
|
||||
|
||||
mapHosts = dir: attrs @ { system ? system, ... }:
|
||||
mapModules dir (hostPath: mkHost hostPath attrs);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue