summaryrefslogtreecommitdiff
path: root/johnlepikhin/home/ssh.scm
blob: 414435164f0384bd2a76baff2bcd0dd20b29b2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2022 Evgenii Lepikhin <johnlepikhin@gmail.com>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (johnlepikhin home ssh)
  #:use-module (gnu services)
  #:use-module (gnu services configuration)
  #:use-module (gnu home services)
  #:use-module (srfi srfi-1)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:export (home-ssh-host-configuration
            home-ssh-configuration
            home-ssh-service-type))

(define-record-type* <home-ssh-host-configuration>
  home-ssh-host-configuration make-home-ssh-host-configuration
  home-ssh-host-configuration?
  (host-mask home-ssh-host-configuration-host-mask)
  (hostname home-ssh-host-configuration-hostname (default #f))
  (port home-ssh-host-configuration-port (default #f))
  (user home-ssh-host-configuration-user (default #f))
  (compression home-ssh-host-configuration-compression (default #f))
  (server-alive-interval home-ssh-host-configuration-server-alive-interval (default #f))
  (connect-timeout home-ssh-host-configuration-connect-timeout (default #f))
  (control-master home-ssh-host-configuration-control-master (default #f))
  (control-persist home-ssh-host-configuration-control-persist (default #f))
  (proxy-jump home-ssh-host-configuration-proxy-jump (default #f))
  (pubkey-accepted-key-types home-ssh-host-configuration-pubkey-accepted-key-types (default #f))
  (host-key-algorithms home-ssh-host-configuration-host-key-algorithms (default #f))
  (comment home-ssh-host-configuration-comment (default #f))
  (identity-file home-ssh-host-configuration-identity-file (default (list)))
  (add-ssh-keys-to-agent home-ssh-host-configuration-add-ssh-keys-to-agent (default #f))
  (forward-agent home-ssh-host-configuration-forward-agent (default #f))
  )

(define (serialize-home-ssh-host-configuration val)
  (string-append
   (if (home-ssh-host-configuration-comment val)
       (format #f "# ~a\n" (home-ssh-host-configuration-comment val)) "")
   (format #f "Host ~a\n" (home-ssh-host-configuration-host-mask val))
   (if (home-ssh-host-configuration-hostname val)
       (format #f "  Hostname ~a\n" (home-ssh-host-configuration-hostname val)) "")
   (if (home-ssh-host-configuration-port val)
       (format #f "  Port ~d\n" (home-ssh-host-configuration-port val)) "")
   (if (home-ssh-host-configuration-user val)
       (format #f "  User ~a\n" (home-ssh-host-configuration-user val)) "")
   (if (home-ssh-host-configuration-compression val)
       (format #f "  Compression yes\n") "")
   (if (home-ssh-host-configuration-server-alive-interval val)
       (format #f "  ServerAliveInterval ~d\n" (home-ssh-host-configuration-server-alive-interval val)) "")
   (if (home-ssh-host-configuration-connect-timeout val)
       (format #f "  ConnectTimeout ~d\n" (home-ssh-host-configuration-connect-timeout val)) "")
   (if (home-ssh-host-configuration-control-master val)
       (format #f "  ControlMaster ~a\n" (home-ssh-host-configuration-control-master val)) "")
   (if (home-ssh-host-configuration-control-persist val)
       (format #f "  ControlPersist yes\n") "")
   (if (home-ssh-host-configuration-proxy-jump val)
       (format #f "  ProxyJump ~a\n" (home-ssh-host-configuration-proxy-jump val)) "")
   (if (home-ssh-host-configuration-pubkey-accepted-key-types val)
       (format #f "  PubkeyAcceptedKeyTypes ~a\n" (home-ssh-host-configuration-pubkey-accepted-key-types val)) "")
   (if (home-ssh-host-configuration-host-key-algorithms val)
       (format #f "  HostKeyAlgorithms ~a\n" (home-ssh-host-configuration-host-key-algorithms val)) "")
   (string-concatenate
    (map
     (lambda (file) (string-append "  IdentityFile " file "\n"))
     (home-ssh-host-configuration-identity-file val)))
   (if (home-ssh-host-configuration-add-ssh-keys-to-agent val)
       "  AddKeysToAgent yes\n" "")
   (if (home-ssh-host-configuration-forward-agent val)
       "  ForwardAgent yes\n" "")
   "\n"))

(define-record-type* <home-ssh-configuration>
  home-ssh-configuration make-home-ssh-configuration
  home-ssh-configuration?
  (hosts home-ssh-configuration-hosts (default '())))

(define (ssh-control-path _)
  (string-append (getenv "XDG_CACHE_HOME") "/ssh-connections"))

(define (add-ssh-config-file config)
  `((".ssh/config"
     ,(mixed-text-file
       "ssh-config"
       #~(string-append
          #$@(append
              (list "ControlPath " (ssh-control-path '()) "/%h:%p:%r\n\n")
              (map (lambda (host)
                     (serialize-home-ssh-host-configuration host))
                   (home-ssh-configuration-hosts config))))))))

(define (add-ssh-extensions config extensions)
  (home-ssh-configuration
   (inherit config)
   (hosts
    (append (home-ssh-configuration-hosts config)
            extensions))))

(define (home-ssh-activation config)
  (let ((path (ssh-control-path '())))
    #~(begin
        (format #t "Creating ~a for persistent ssh control path\n" #$path)
        (mkdir-p #$path))))

(define home-ssh-service-type
  (service-type (name 'home-ssh)
                (extensions
                 (list
                  (service-extension home-files-service-type add-ssh-config-file)
                  (service-extension home-activation-service-type home-ssh-activation)))
                (compose concatenate)
                (extend add-ssh-extensions)
                (description "Create @file{~/.ssh/config}")))