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

70 lines
1.4 KiB
Nix
Raw Permalink Normal View History

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