aboutsummaryrefslogtreecommitdiff
;;; Whispers --- Stealth VPN and ssh tunneler
;;; Copyright © 2024 Runciter <runciter@whispers-vpn.org>
;;;
;;; This file is part of Whispers.
;;;
;;; Whispers 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.
;;;
;;; Whispers 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 Whispers.  If not, see <http://www.gnu.org/licenses/>.

(define-module (whispers services gps)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services admin)
  #:use-module (gnu packages gps)
  #:export (gpsd-configuration
            gpsd-configuration?
            gpsd-service-type))

(define-record-type* <gpsd-configuration>
  gpsd-configuration make-gpsd-configuration
  gpsd-configuration?
  this-gpsd-configuration
  ;; A file-like object
  (gpsd-package         gpsd-configuration-gpsd-package
                        (default gpsd))
  ;; A symbol
  (provision            gpsd-configuration-provision
                        (default (string->symbol
                                  (string-append
                                   "gpsd-"
                                   (number->string
                                    (gpsd-configuration-port
                                     this-gpsd-configuration)))))
                        (thunked))
  ;; A string
  (source               gpsd-configuration-source
                        (default "/dev/ttyUSB0"))
  ;; An integer
  (port                 gpsd-configuration-port
                        (default 2947))
  ;; An integer
  (listen-any?          gpsd-configuration-listen-any?
                        (default #f))
  ;; A boolean value
  (%auto-start?          gpsd-configuration-auto-start?
                         (default #t)))

(define (constructor-gexp config)
  "Returns a G-exp to start a gpsd shepherd service, configurable by
CONFIG, a record of the <gpsd-configuration> type."
  (let ((gpsd-package (gpsd-configuration-gpsd-package config))
        (listen-any? (gpsd-configuration-listen-any? config))
        (port (gpsd-configuration-port config))
        (source (gpsd-configuration-source config)))
    #~(make-forkexec-constructor (append (list #$(file-append gpsd-package
                                                              "/sbin/gpsd")
                                               "-N"
                                               "-P"
                                               #$(number->string port))
                                         (if #$listen-any?
                                             (list "-G")
                                             (list))
                                         (list #$source)))))

(define (gpsd-shepherd-services config)
  "Returns a list of shepherd services handling a gpsd daemon
configured by CONFIG, a record of the <gpsd-configuration>
type."
  (let ((auto-start? (gpsd-configuration-auto-start? config)))
    (list
     (shepherd-service
      (documentation (string-append "gpsd service."))
      (provision (list (gpsd-configuration-provision config)))
      (requirement '())
      (start (constructor-gexp config))
      (stop #~(make-kill-destructor))
      (auto-start? auto-start?)))))

(define gpsd-service-type
  (service-type
   (name 'gpsd)
   (description "gpsd service")
   (extensions
    (list (service-extension shepherd-root-service-type
                             gpsd-shepherd-services)
          (service-extension
           profile-service-type
           (lambda (config)
             (list
              (gpsd-configuration-gpsd-package config))))))
   (default-value (gpsd-configuration))))