pam-reattach.nix 1.8 KB

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