1
0
mirror of https://github.com/kmein/niveum synced 2026-03-16 10:11:08 +01:00
Files
niveum/modules/networkmanager-declarative.nix

70 lines
1.4 KiB
Nix

# https://github.com/jmackie/nixos-networkmanager-profiles/
{
lib,
config,
...
}:
with lib; let
nm = config.networking.networkmanager;
mkProfile = profileAttrs:
if !(isAttrs profileAttrs)
then throw "error 1"
else {
enable = true;
mode = "0400"; # readonly (user)
text =
(foldlAttrs (accum: {
name,
value,
}: ''
${accum}
[${name}] ${mkProfileEntry value}'')
"# Generated by nixos-networkmanager-profiles"
profileAttrs)
+ "\n";
};
mkProfileEntry = entryAttrs:
if !(isAttrs entryAttrs)
then throw "error 2"
else
foldlAttrs (accum: {
name,
value,
}: ''
${accum}
${name}=${toString value}'') ""
entryAttrs;
foldlAttrs = op: nul: attrs:
foldl (accum: {
fst,
snd,
}:
op accum (nameValuePair fst snd))
nul
(lists.zipLists (attrNames attrs) (attrValues attrs));
attrLength = attrs: length (attrValues attrs);
in {
options.networking.networkmanager.profiles = mkOption {
type = types.attrs;
default = {};
};
config = mkIf (attrLength nm.profiles > 0) {
environment.etc = foldlAttrs (accum: {
name,
value,
}:
accum
// {
"NetworkManager/system-connections/${name}.nmconnection" =
mkProfile value;
}) {}
nm.profiles;
};
}