infrastructure/lib/nix-lib/default.nix
catvayor ecb6b54b9e
All checks were successful
Check meta / check_dns (pull_request) Successful in 14s
Check meta / check_meta (pull_request) Successful in 15s
Check workflows / check_workflows (pull_request) Successful in 16s
Build all the nodes / ap01 (pull_request) Successful in 1m17s
Build all the nodes / bridge01 (pull_request) Successful in 1m50s
Build all the nodes / netaccess01 (pull_request) Successful in 33s
Build all the nodes / netcore01 (pull_request) Successful in 30s
Build all the nodes / build01 (pull_request) Successful in 2m9s
Build all the nodes / hypervisor01 (pull_request) Successful in 1m59s
Build all the nodes / hypervisor02 (pull_request) Successful in 2m8s
Build all the nodes / netcore02 (pull_request) Successful in 41s
Build all the nodes / geo01 (pull_request) Successful in 2m30s
Build all the nodes / hypervisor03 (pull_request) Successful in 1m40s
Build all the nodes / geo02 (pull_request) Successful in 2m39s
Build the shell / build-shell (pull_request) Successful in 35s
Run pre-commit on all files / pre-commit (pull_request) Successful in 47s
Build all the nodes / compute01 (pull_request) Successful in 3m17s
Build all the nodes / rescue01 (pull_request) Successful in 1m49s
Build all the nodes / tower01 (pull_request) Successful in 1m43s
Build all the nodes / storage01 (pull_request) Successful in 2m33s
Build all the nodes / vault01 (pull_request) Successful in 2m27s
Build all the nodes / web02 (pull_request) Successful in 2m29s
Build all the nodes / web01 (pull_request) Successful in 2m34s
Build all the nodes / web03 (pull_request) Successful in 2m36s
Build all the nodes / netaccess01 (push) Successful in 20s
Build all the nodes / netcore02 (push) Successful in 20s
Build all the nodes / netcore01 (push) Successful in 21s
Build all the nodes / ap01 (push) Successful in 32s
Build all the nodes / bridge01 (push) Successful in 1m53s
Build all the nodes / hypervisor02 (push) Successful in 1m52s
Build all the nodes / hypervisor03 (push) Successful in 1m53s
Build all the nodes / rescue01 (push) Successful in 2m0s
Build all the nodes / build01 (push) Successful in 2m26s
Build the shell / build-shell (push) Successful in 34s
Run pre-commit on all files / pre-commit (push) Successful in 35s
Build all the nodes / tower01 (push) Successful in 2m29s
Build all the nodes / web02 (push) Successful in 2m11s
Build all the nodes / geo02 (push) Successful in 2m37s
Build all the nodes / vault01 (push) Successful in 2m31s
Build all the nodes / geo01 (push) Successful in 2m52s
Build all the nodes / storage01 (push) Successful in 2m54s
Build all the nodes / hypervisor01 (push) Successful in 2m56s
Build all the nodes / web03 (push) Successful in 2m43s
Build all the nodes / compute01 (push) Successful in 3m25s
Build all the nodes / web01 (push) Successful in 3m5s
feat(lib): init genFuse
2025-02-10 16:52:50 +01:00

187 lines
3.9 KiB
Nix

# Copyright Tom Hubrecht, (2023)
# SPDX-FileCopyrightText: 2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>
#
# SPDX-License-Identifier: EUPL-1.2
{ lib }:
rec {
inherit (lib)
flip
optionals
optionalString
hasPrefix
recursiveUpdate
splitString
unique
warn
;
/*
Fuses a list of attribute sets into a single attribute set.
Type: [attrs] -> attrs
Example:
x = [ { a = 1; } { b = 2; } ]
fuseAttrs x
=> { a = 1; b = 2; }
*/
fuseAttrs = builtins.foldl' (attrs: x: attrs // x) { };
fuseValueAttrs = attrs: fuseAttrs (builtins.attrValues attrs);
/*
Applies a function to `attrsList` before fusing the resulting list
of attribute sets.
Type: ('a -> attrs) -> ['a] -> attrs
Example:
x = [ "to" "ta" "ti" ]
f = s: { ${s} = s + s; }
mapFuse f x
=> { to = "toto"; ta = "tata"; ti = "titi"; }
*/
mapFuse =
# 'a -> attrs
f:
# ['a]
attrsList:
fuseAttrs (builtins.map f attrsList);
/*
Generate an `attrsList` of given size with the generator before fusing
the resulting list of attribute sets.
Type: (Int -> attrs) -> Int -> attrs
Example:
f = s: { "a${toString s}" = s + s; }
genFuse f 3
=> { a0 = 0; a1 = 2; a2 = 4; }
*/
genFuse =
# Int -> attrs
f:
# Int
size:
fuseAttrs (builtins.genList f size);
/*
Equivalent of lib.singleton but for an attribute set.
Type: str -> 'a -> attrs
Example:
singleAttr "a" 1
=> { a = 1; }
*/
singleAttr = name: value: { ${name} = value; };
# Enables a list of modules.
enableAttrs' =
enable:
mapFuse (m: {
${m}.${enable} = true;
});
enableModules = enableAttrs' "enable";
/*
Create an attribute set from a list of values, mapping those
values through the function `f`.
Example:
mapSingleFuse (x: "val-${x}") [ "a" "b" ]
=> { a = "val-a"; b = "val-b" }
*/
mapSingleFuse = f: mapFuse (x: singleAttr x (f x));
/*
Creates a relative path as a string
Type: path -> str -> path
Example:
mkRel /home/test/ "file.txt"
=> "/home/test/file.txt"
*/
mkRel = path: file: path + "/${file}";
setDefault =
default:
mapFuse (name: {
${name} = default;
});
mkBaseSecrets =
root:
mapFuse (secret: {
${secret}.file = mkRel root secret;
});
getSecrets = dir: builtins.attrNames (import (mkRel dir "secrets.nix"));
subAttr = attrs: name: attrs.${name};
subAttrs = attrs: builtins.map (subAttr attrs);
optionalList = optionals;
/*
Same as fuseAttrs but using `lib.recursiveUpdate` to merge attribute
sets together.
Type: [attrs] -> attrs
*/
recursiveFuse = builtins.foldl' recursiveUpdate { };
mkImport =
root: file:
let
path = mkRel root file;
in
path + (optionalString (!(builtins.pathExists path)) ".nix");
mkImports = root: builtins.map (mkImport root);
/*
Creates a confugiration by merging enabled modules,
services and extraConfig.
Example:
mkConfig {
enabledModules = [ "ht-defaults" ];
enabledServices = [ "toto" ];
extraConfig = { services.nginx.enable = true; };
root = ./.;
}
=>
{
imports = [ ./toto ];
ht-defaults.enable = true;
services.nginx.enable = true;
}
*/
mkConfig =
{
# List of modules to enable with `enableModules`
enabledModules,
# List of services to import
enabledServices,
# Extra configuration, defaults to `{ }`
extraConfig ? { },
# Path relative to which the enabled services will be imported
root,
}:
recursiveFuse [
(enableModules enabledModules)
{
imports =
(extraConfig.imports or [ ]) ++ (mkImports root ([ "_hardware-configuration" ] ++ enabledServices));
}
(removeAttrs extraConfig [ "imports" ])
];
}