From patchwork Wed Jan 18 16:54:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bruno Victal X-Patchwork-Id: 46270 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 17EA227BBEB; Wed, 18 Jan 2023 16:56:20 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 98F0527BBE9 for ; Wed, 18 Jan 2023 16:56:17 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pIBjI-000273-84; Wed, 18 Jan 2023 11:56:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pIBjG-00026p-R7 for guix-patches@gnu.org; Wed, 18 Jan 2023 11:56:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pIBjG-0003SW-I5 for guix-patches@gnu.org; Wed, 18 Jan 2023 11:56:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pIBjG-0004iB-78 for guix-patches@gnu.org; Wed, 18 Jan 2023 11:56:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#60735] [PATCH v2 1/3] services: Add etc-hosts-service-type. References: In-Reply-To: Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 18 Jan 2023 16:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60735 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 60735@debbugs.gnu.org Cc: ludo@gnu.org, Bruno Victal Received: via spool by 60735-submit@debbugs.gnu.org id=B60735.167406092518064 (code B ref 60735); Wed, 18 Jan 2023 16:56:02 +0000 Received: (at 60735) by debbugs.gnu.org; 18 Jan 2023 16:55:25 +0000 Received: from localhost ([127.0.0.1]:41643 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pIBie-0004hH-LU for submit@debbugs.gnu.org; Wed, 18 Jan 2023 11:55:25 -0500 Received: from smtpm2.myservices.hosting ([185.26.105.233]:45910) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pIBic-0004h9-Kz for 60735@debbugs.gnu.org; Wed, 18 Jan 2023 11:55:23 -0500 Received: from mail1.netim.hosting (unknown [185.26.106.172]) by smtpm2.myservices.hosting (Postfix) with ESMTP id E2F3620CF2; Wed, 18 Jan 2023 17:55:20 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id 8215580098; Wed, 18 Jan 2023 17:55:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail1.netim.hosting Received: from mail1.netim.hosting ([127.0.0.1]) by localhost (mail1-1.netim.hosting [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id FRxeiXFIR4aA; Wed, 18 Jan 2023 17:55:20 +0100 (CET) Received: from guix-nuc.home.arpa (bl9-118-236.dsl.telepac.pt [85.242.118.236]) (Authenticated sender: lumen@makinata.eu) by mail1.netim.hosting (Postfix) with ESMTPSA id B1A2880099; Wed, 18 Jan 2023 17:55:19 +0100 (CET) From: Bruno Victal Date: Wed, 18 Jan 2023 16:54:11 +0000 Message-Id: X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/services.scm (etc-hosts-service-type): New variable. * doc/guix.texi: Document it. --- doc/guix.texi | 46 +++++++++++++++++++++++++++++++++++ gnu/services.scm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/doc/guix.texi b/doc/guix.texi index 9b478733eb..5fb3df441c 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -112,6 +112,7 @@ Copyright @copyright{} 2022 Ivan Vilata-i-Balaguer@* Copyright @copyright{} 2023 Giacomo Leidi@* Copyright @copyright{} 2022 Antero Mejr@* +Copyright @copyright{} 2023 Bruno Victal@* Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or @@ -40191,6 +40192,51 @@ Service Reference pointing to the given file. @end defvr +@defvar hosts-service-type +Type of the service that populates the entries for (@file{/etc/hosts}). +This service type can be extended by passing it a list of +@code{host-entry} records. + +@c TRANSLATORS: The domain names below SHOULD NOT be translated. +@c They're domains reserved for use in documentation. (RFC6761 Section 6.5) +@c The addresses used are explained in RFC3849 and RFC5737. +@lisp +(simple-service 'add-extra-hosts + hosts-service-type + (list (host-entry + (address "192.0.2.1") + (canonical-name "example.com") + (aliases '("example.net" "example.org"))) + (host-entry + (address "2001:DB8::1") + (canonical-name "example.com") + (aliases '("example.net" "example.org"))))) +@end lisp + +@deftp {Data Type} host-entry +Available @code{host-entry} fields are: + +@table @asis +@item @code{address} (type: string) +IP address. + +@item @code{canonical-name} (type: string) +Hostname. + +@item @code{aliases} (default: @code{'()}) (type: list-of-string) +Additional aliases that map to the same @code{canonical-name}. + +@end table +@end deftp + +For convenience, the procedure @code{host} can be for creating +@code{host-entry} records. + +@defun host address canonical-name [aliases] +Procedure for creating @code{host-entry} records. +@end defun +@end defvar + @defvr {Scheme Variable} setuid-program-service-type Type for the ``setuid-program service''. This service collects lists of executable file names, passed as gexps, and adds them to the set of diff --git a/gnu/services.scm b/gnu/services.scm index 2abef557d4..12ecfa4492 100644 --- a/gnu/services.scm +++ b/gnu/services.scm @@ -6,6 +6,7 @@ ;;; Copyright © 2021 raid5atemyhomework ;;; Copyright © 2020 Christine Lemmer-Webber ;;; Copyright © 2020, 2021 Brice Waegeneire +;;; Copyright © 2023 Bruno Victal ;;; ;;; This file is part of GNU Guix. ;;; @@ -51,6 +52,7 @@ (define-module (gnu services) #:use-module (srfi srfi-35) #:use-module (ice-9 vlist) #:use-module (ice-9 match) + #:use-module (ice-9 format) #:autoload (ice-9 pretty-print) (pretty-print) #:export (service-extension service-extension? @@ -109,6 +111,15 @@ (define-module (gnu services) extra-special-file etc-service-type etc-directory + + host + host-entry + host-entry? + host-entry-address + host-entry-canonical-name + host-entry-aliases + hosts-service-type + setuid-program-service-type profile-service-type firmware-service-type @@ -809,6 +820,58 @@ (define (etc-service files) FILES must be a list of name/file-like object pairs." (service etc-service-type files)) +(define (valid-name? name) + "Return true if @var{name} is likely to be a valid hostname." + (false-if-exception (not (string-any char-set:whitespace name)))) + +(define-compile-time-procedure (assert-valid-name (name valid-name?)) + "Ensure @var{name} is likely to be a valid hostname." + ;; TODO: RFC compliant implementation. + (unless (valid-name? name) + (raise + (make-compound-condition + (formatted-message (G_ "hostname '~a' contains invalid characters.") + name) + (condition (&error-location + (location + (source-properties->location procedure-call-location))))))) + name) + +(define-record-type* host-entry + make-host-entry host-entry? + (address host-entry-address) + (canonical-name host-entry-canonical-name + (sanitize assert-valid-name)) + (aliases host-entry-aliases + (default '()) + (sanitize (cut map assert-valid-name <>)))) + +(define* (host address canonical-name #:optional (aliases '())) + "More compact way of creating records" + (make-host-entry address canonical-name aliases)) + +(define hosts-service-type + ;; Extend etc-service-type with a entry for @file{/etc/hosts}. + (let* ((serialize-host-entry-record + (lambda (record) + (match-record record (address canonical-name aliases) + (format #f "~a~/~a~{~^~/~a~}~%" address canonical-name aliases)))) + (host-etc-service + (lambda (lst) + `(("hosts" ,(plain-file "hosts" + (format #f "~{~a~}" + (map serialize-host-entry-record + lst)))))))) + (service-type + (name 'etc-hosts) + (extensions + (list + (service-extension etc-service-type + host-etc-service))) + (compose concatenate) + (extend append) + (description "Populate the @file{/etc/hosts} file.")))) + (define (setuid-program->activation-gexp programs) "Return an activation gexp for setuid-program from PROGRAMS." (let ((programs (map (lambda (program)