utils: create utility functions to ease repetition

`reqImport` in particular, is useful for easily importing an entire
directory of nix files into an attribute set.
This commit is contained in:
Timothy DeHerrera 2019-12-13 21:30:43 -07:00
parent 7bffd55c6f
commit ae0746a5a4
No known key found for this signature in database
GPG key ID: 8985725DB5B0C122
3 changed files with 75 additions and 60 deletions

View file

@ -1,72 +1,37 @@
{ nix, nixpkgs, flake, ... }: { nix, nixpkgs, flake, utils, ... }:
let let
inherit (builtins) inherit (utils)
isAttrs reqImport
readDir vimport
;
inherit (nixpkgs.lib)
filterAttrs
hasSuffix
mapAttrs'
nameValuePair
removeSuffix
; ;
configs = let config = self:
configs' = let nixpkgs.lib.nixosSystem rec {
config = this: system = "x86_64-linux";
nixpkgs.lib.nixosSystem rec {
system = "x86_64-linux";
modules = let modules = let
core = ../profiles/core.nix; core = ../profiles/core.nix;
global = { global = {
system.configurationRevision = flake.rev; _module.args.utils = utils;
networking.hostName = "${this}"; system.configurationRevision = flake.rev;
nix.package = nix.defaultPackage."${system}"; networking.hostName = self;
};
local = ./. + "/${this}.nix";
in
[
core
global
local
];
nix.package = nix.defaultPackage."${system}";
}; };
dot = readDir ./.; local = vimport ./. "${self}.nix";
in in
mapAttrs' [
( core
name: value: global
if local
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 in
configs reqImport { dir = ./.; _import = config; }

View file

@ -9,6 +9,7 @@
configs = import ./configurations { configs = import ./configurations {
inherit nix nixpkgs; inherit nix nixpkgs;
flake = self; flake = self;
utils = import ./lib/utils.nix { lib = nixpkgs.lib; };
}; };
in in

View file

@ -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);
}