pam-reattach.nix 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. { config
  2. , lib
  3. , pkgs
  4. , ...
  5. }:
  6. with lib;
  7. let
  8. cfg = config.security.pam;
  9. mkPamReattachScript =
  10. isEnabled:
  11. let
  12. file = "/etc/pam.d/sudo";
  13. option = "security.pam.reattach";
  14. sed = "${pkgs.gnused}/bin/sed";
  15. in
  16. ''
  17. ${
  18. if isEnabled then
  19. ''
  20. # If enable and in file, remove and re-add in case the store path has
  21. # changed
  22. if grep '${option}' ${file} > /dev/null; then
  23. ${sed} -i '/${option}/d' ${file}
  24. fi
  25. ${sed} -i '2i\
  26. auth optional ${pkgs.pam-reattach}/lib/pam/pam_reattach.so ignore_ssh # nix-darwin: ${option}
  27. ' ${file}
  28. ''
  29. else
  30. ''
  31. # Disable pam_reattach, if added by nix-darwin
  32. if grep '${option}' ${file} > /dev/null; then
  33. ${sed} -i '/${option}/d' ${file}
  34. fi
  35. ''
  36. }
  37. '';
  38. in
  39. {
  40. options = {
  41. security.pam.enablePamReattach = mkEnableOption "" // {
  42. description = lib.mdDoc ''
  43. Enable pam_reattach sudo authentication with Touch ID in tmux/screen.
  44. When enabled, this option adds the following line to
  45. {file}`/etc/pam.d/sudo`:
  46. ```
  47. auth optional pam_reattach.so ignore_ssh
  48. ```
  49. ::: {.note}
  50. macOS resets this file when doing a system update. As such,
  51. pam_reattach won't work after a system update
  52. until the nix-darwin configuration is reapplied.
  53. :::
  54. '';
  55. };
  56. };
  57. config = {
  58. environment.systemPackages = mkIf cfg.enablePamReattach [ pkgs.pam-reattach ];
  59. system.activationScripts.pam.text = ''
  60. # PAM settings
  61. echo >&2 "setting up pam..."
  62. ${mkPamReattachScript cfg.enablePamReattach}
  63. '';
  64. };
  65. }