1
0

pam-reattach.nix 1.8 KB

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