diff --git a/configurations/default.nix b/configurations/default.nix index 39a7bd38..82e25c01 100644 --- a/configurations/default.nix +++ b/configurations/default.nix @@ -1,72 +1,37 @@ -{ nix, nixpkgs, flake, ... }: +{ nix, nixpkgs, flake, utils, ... }: let - inherit (builtins) - isAttrs - readDir - ; - - inherit (nixpkgs.lib) - filterAttrs - hasSuffix - mapAttrs' - nameValuePair - removeSuffix + inherit (utils) + reqImport + vimport ; - configs = let - configs' = let - config = this: - nixpkgs.lib.nixosSystem rec { - system = "x86_64-linux"; + config = self: + nixpkgs.lib.nixosSystem rec { + system = "x86_64-linux"; - modules = let - core = ../profiles/core.nix; + modules = let + core = ../profiles/core.nix; - global = { - system.configurationRevision = flake.rev; + global = { + _module.args.utils = utils; - networking.hostName = "${this}"; + system.configurationRevision = flake.rev; - nix.package = nix.defaultPackage."${system}"; - }; - - local = ./. + "/${this}.nix"; - - in - [ - core - global - local - ]; + networking.hostName = self; + nix.package = nix.defaultPackage."${system}"; }; - dot = readDir ./.; + local = vimport ./. "${self}.nix"; - in - mapAttrs' - ( - name: value: - if - name != "default.nix" - && hasSuffix ".nix" name - && value == "regular" - - then let - name' = removeSuffix ".nix" name; - in - nameValuePair (name') (config name') - - else - nameValuePair ("") (null) - ) - dot; - - removeInvalid = - filterAttrs (_: value: isAttrs value); - in - removeInvalid configs'; + in + [ + core + global + local + ]; + }; in -configs + reqImport { dir = ./.; _import = config; } diff --git a/flake.nix b/flake.nix index 68731151..607002a4 100644 --- a/flake.nix +++ b/flake.nix @@ -9,6 +9,7 @@ configs = import ./configurations { inherit nix nixpkgs; flake = self; + utils = import ./lib/utils.nix { lib = nixpkgs.lib; }; }; in diff --git a/lib/utils.nix b/lib/utils.nix index 4b42ce71..6a9275d9 100644 --- a/lib/utils.nix +++ b/lib/utils.nix @@ -1,2 +1,51 @@ -{ ... }: -{} +{ lib, ... }: +let + inherit (builtins) + attrNames + isAttrs + readDir + ; + + + inherit (lib) + filterAttrs + hasSuffix + mapAttrs' + nameValuePair + removeSuffix + ; + +in +rec { + # mapFilterAttrs :: + # (name -> value -> bool ) + # (name -> value -> { name = any; value = any; }) + # attrs + mapFilterAttrs = seive: f: attrs: + filterAttrs seive (mapAttrs' f attrs); + + vimport = path: name: import (path + "/${name}"); + + reqImport = { + dir, + _import ? base: vimport dir (base + ".nix") + }: + mapFilterAttrs + (_: v: v != null) + ( + n: v: + if + n != "default.nix" + && hasSuffix ".nix" n + && v == "regular" + + then let + name = removeSuffix ".nix" n; + in + nameValuePair (name) (_import name) + + else + nameValuePair ("") (null) + ) + (readDir dir); +}