gnupg.scm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu home services gnupg)
  19. #:use-module (guix gexp)
  20. #:use-module ((guix records) #:select (match-record))
  21. #:use-module (gnu services)
  22. #:use-module (gnu services configuration)
  23. #:use-module (gnu home services)
  24. #:use-module (gnu home services shepherd)
  25. #:autoload (gnu packages gnupg) (gnupg pinentry)
  26. #:export (home-gpg-agent-configuration
  27. home-gpg-agent-configuration?
  28. home-gpg-agent-configuration-gnupg
  29. home-gpg-agent-configuration-pinentry-program
  30. home-gpg-agent-configuration-ssh-support?
  31. home-gpg-agent-configuration-default-cache-ttl
  32. home-gpg-agent-configuration-max-cache-ttl
  33. home-gpg-agent-configuration-max-cache-ttl-ssh
  34. home-gpg-agent-configuration-extra-content
  35. home-gpg-agent-service-type))
  36. (define raw-configuration-string? string?)
  37. ;; Configuration of 'gpg-agent'.
  38. (define-configuration/no-serialization home-gpg-agent-configuration
  39. (gnupg
  40. (file-like gnupg)
  41. "The GnuPG package to use.")
  42. (pinentry-program
  43. (file-like (file-append pinentry "/bin/pinentry-curses"))
  44. "Pinentry program to use. Pinentry is a small user interface that
  45. @command{gpg-agent} delegates to anytime it needs user input for a passphrase
  46. or @acronym{PIN, personal identification number} (@pxref{Top,,, pinentry,
  47. Using the PIN-Entry}).")
  48. (ssh-support?
  49. (boolean #f)
  50. "Whether to enable @acronym{SSH, secure shell} support. When true,
  51. @command{gpg-agent} acts as a drop-in replacement for OpenSSH's
  52. @command{ssh-agent} program, taking care of OpenSSH secret keys and directing
  53. passphrase requests to the chosen Pinentry program.")
  54. (default-cache-ttl
  55. (integer 600)
  56. "Time a cache entry is valid, in seconds.")
  57. (max-cache-ttl
  58. (integer 7200)
  59. "Maximum time a cache entry is valid, in seconds. After this time a cache
  60. entry will be expired even if it has been accessed recently.")
  61. (default-cache-ttl-ssh
  62. (integer 1800)
  63. "Time a cache entry for SSH keys is valid, in seconds.")
  64. (max-cache-ttl-ssh
  65. (integer 7200)
  66. "Maximum time a cache entry for SSH keys is valid, in seconds.")
  67. (extra-content
  68. (raw-configuration-string "")
  69. "Raw content to add to the end of @file{~/.gnupg/gpg-agent.conf}."))
  70. (define (home-gpg-agent-configuration-file config)
  71. "Return the @file{gpg-agent.conf} file for @var{config}."
  72. (match-record config <home-gpg-agent-configuration>
  73. (pinentry-program default-cache-ttl max-cache-ttl
  74. default-cache-ttl-ssh max-cache-ttl-ssh
  75. extra-content)
  76. (mixed-text-file "gpg-agent.conf"
  77. "pinentry-program " pinentry-program "\n"
  78. "default-cache-ttl "
  79. (number->string default-cache-ttl) "\n"
  80. "max-cache-ttl "
  81. (number->string max-cache-ttl) "\n"
  82. "default-cache-ttl-ssh "
  83. (number->string default-cache-ttl-ssh) "\n"
  84. "max-cache-ttl-ssh "
  85. (number->string max-cache-ttl-ssh) "\n"
  86. extra-content)))
  87. (define (home-gpg-agent-shepherd-services config)
  88. "Return the possibly-empty list of Shepherd services for @var{config}."
  89. (match-record config <home-gpg-agent-configuration>
  90. (gnupg ssh-support?)
  91. ;; 'gpg-agent' is started on demand by GnuPG's programs, but it has to be
  92. ;; started explicitly when OpenSSH support is enabled (info "(gnupg) Agent
  93. ;; Options").
  94. (if ssh-support?
  95. (let ((endpoint (lambda (name socket)
  96. #~(endpoint
  97. (make-socket-address
  98. AF_UNIX
  99. (string-append %user-runtime-dir
  100. "/gnupg/" #$socket))
  101. #:name #$name
  102. #:socket-directory-permissions #o700))))
  103. (list (shepherd-service
  104. (provision '(gpg-agent ssh-agent))
  105. (modules '((shepherd support))) ;for '%user-runtime-dir'
  106. (start #~(make-systemd-constructor
  107. (list #$(file-append gnupg "/bin/gpg-agent")
  108. "--supervised" "--enable-ssh-support")
  109. (list #$(endpoint "ssh" "S.gpg-agent.ssh")
  110. #$(endpoint "browser" "S.gpg-agent.browser")
  111. #$(endpoint "extra" "S.gpg-agent.extra")
  112. ;; #$(endpoint "scdaemon" "S.scdaemon")
  113. #$(endpoint "std" "S.gpg-agent"))))
  114. (stop #~(make-systemd-destructor))
  115. (documentation "Start 'gpg-agent', the GnuPG passphrase
  116. agent, with support for handling OpenSSH material."))))
  117. '())))
  118. (define (home-gpg-agent-files config)
  119. `((".gnupg/gpg-agent.conf" ,(home-gpg-agent-configuration-file config))))
  120. (define (home-gpg-agent-environment-variables config)
  121. "Return GnuPG environment variables needed for @var{config}."
  122. (if (home-gpg-agent-configuration-ssh-support? config)
  123. `(("SSH_AUTH_SOCK"
  124. . "$XDG_RUNTIME_DIR/gnupg/S.gpg-agent.ssh"))
  125. '()))
  126. (define home-gpg-agent-service-type
  127. (service-type
  128. (name 'home-gpg-agent)
  129. (extensions
  130. (list (service-extension home-files-service-type
  131. home-gpg-agent-files)
  132. (service-extension home-shepherd-service-type
  133. home-gpg-agent-shepherd-services)
  134. (service-extension home-environment-variables-service-type
  135. home-gpg-agent-environment-variables)))
  136. (default-value (home-gpg-agent-configuration))
  137. (description
  138. "Configure GnuPG's agent, @command{gpg-agent}, which is responsible for
  139. managing OpenPGP and optionally SSH private keys. When SSH support is
  140. enabled, @command{gpg-agent} acts as a drop-in replacement for OpenSSH's
  141. @command{ssh-agent}.")))