dotfiles/modules/common/shell/work.nix

60 lines
2.0 KiB
Nix
Raw Normal View History

2024-04-20 13:42:06 +00:00
{
config,
pkgs,
lib,
...
}:
{
home-manager.users.${config.user} = lib.mkIf pkgs.stdenv.isDarwin {
2024-04-20 13:42:06 +00:00
home.packages =
let
ldap_scheme = "ldaps";
magic_prefix = "take";
2024-11-05 20:31:04 +00:00
ldap_port = 3269;
jq_parse = pkgs.writeShellScriptBin "ljq" ''
jq --slurp \
--raw-input 'split("\n\n")|map(split("\n")|map(select(.[0:1]!="#" and length>0)) |select(length > 0)|map(capture("^(?<key>[^:]*:?): *(?<value>.*)") |if .key[-1:.key|length] == ":" then .key=.key[0:-1]|.value=(.value|@base64d) else . end)| group_by(.key) | map({key:.[0].key,value:(if .|length > 1 then [.[].value] else .[].value end)}) | from_entries)' | jq -r 'del(.[].thumbnailPhoto)'
'';
2024-04-20 13:42:06 +00:00
ldap_script = pkgs.writeShellScriptBin "ldap" ''
2024-10-07 23:23:35 +00:00
if ! [ "$LDAP_HOST" ]; then
echo "No LDAP_HOST specified!"
exit 1
fi
2024-04-20 13:42:06 +00:00
SEARCH_FILTER="$@"
ldapsearch -LLL \
-B -o ldif-wrap=no \
2024-11-05 20:31:04 +00:00
-E pr=5000/prompt \
2024-10-07 23:23:35 +00:00
-H "${ldap_scheme}://''${LDAP_HOST}:${builtins.toString ldap_port}" \
-D "${pkgs.lib.toUpper magic_prefix}2\\${pkgs.lib.toLower config.user}" \
2024-11-05 20:31:04 +00:00
-w "$(${pkgs._1password-cli}/bin/op item get T2 --fields label=password --reveal)" \
2024-10-07 23:23:35 +00:00
-b "dc=''${LDAP_HOST//./,dc=}" \
-s "sub" -x "(cn=''${SEARCH_FILTER})" \
2024-11-05 20:31:04 +00:00
| ${jq_parse}/bin/ljq
2024-04-20 13:42:06 +00:00
'';
ldapm_script = pkgs.writeShellScriptBin "ldapm" ''
2024-10-07 23:23:35 +00:00
if ! [ "$LDAP_HOST" ]; then
echo "No LDAP_HOST specified!"
exit 1
fi
2024-04-20 13:42:06 +00:00
${ldap_script}/bin/ldap "$@" | jq '[ .[].memberOf] | add'
'';
ldapg_script = pkgs.writeShellScriptBin "ldapg" ''
2024-10-07 23:23:35 +00:00
if ! [ "$LDAP_HOST" ]; then
echo "No LDAP_HOST specified!"
exit 1
fi
2024-04-20 13:42:06 +00:00
${ldap_script}/bin/ldap "$@" | jq '[ .[].member] | add'
'';
in
[
ldap_script
ldapm_script
ldapg_script
2024-11-05 20:31:04 +00:00
jq_parse
2024-04-20 13:42:06 +00:00
];
};
}