From patchwork Fri Jan 27 21:06: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: 46464 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 BC5EE27BBED; Fri, 27 Jan 2023 21:07:09 +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=ham 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 20BB427BBE9 for ; Fri, 27 Jan 2023 21:07:08 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pLVw7-0001MX-V5; Fri, 27 Jan 2023 16:07: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 1pLVw6-0001Lg-IS for guix-patches@gnu.org; Fri, 27 Jan 2023 16:07: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 1pLVw6-0003g9-7Y for guix-patches@gnu.org; Fri, 27 Jan 2023 16:07:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pLVw6-0007KJ-2l for guix-patches@gnu.org; Fri, 27 Jan 2023 16:07:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#60735] [PATCH v3 1/3] services: Add hosts-service-type. References: In-Reply-To: Resent-From: Bruno Victal Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 27 Jan 2023 21:07: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: Bruno Victal Received: via spool by 60735-submit@debbugs.gnu.org id=B60735.167485360828144 (code B ref 60735); Fri, 27 Jan 2023 21:07:02 +0000 Received: (at 60735) by debbugs.gnu.org; 27 Jan 2023 21:06:48 +0000 Received: from localhost ([127.0.0.1]:38724 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pLVvr-0007Jr-Jm for submit@debbugs.gnu.org; Fri, 27 Jan 2023 16:06:48 -0500 Received: from smtpm5.myservices.hosting ([185.26.105.236]:56310) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pLVvn-0007Jf-FG for 60735@debbugs.gnu.org; Fri, 27 Jan 2023 16:06:46 -0500 Received: from mail1.netim.hosting (unknown [185.26.106.172]) by smtpm5.myservices.hosting (Postfix) with ESMTP id 0E4AC20CFC for <60735@debbugs.gnu.org>; Fri, 27 Jan 2023 22:06:41 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail1.netim.hosting (Postfix) with ESMTP id B150880096; Fri, 27 Jan 2023 22:06:41 +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 Lin9rSRk4mdT; Fri, 27 Jan 2023 22:06:38 +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 7D29E80093; Fri, 27 Jan 2023 22:06:38 +0100 (CET) From: Bruno Victal Date: Fri, 27 Jan 2023 21:06:11 +0000 Message-Id: <23c3ca682838a1722e619148bac0aee88124d212.1674853530.git.mirai@makinata.eu> 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/base.scm (host, %host, host-address, host-canonical-name, host-aliases) (hosts-service-type): New variable. (host?): New predicate. * doc/guix.texi: Document it. --- doc/guix.texi | 75 +++++++++++++++++++++++++++++++++++++++++++ gnu/services/base.scm | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) base-commit: 35e626f312aa5f8c9c4c3f06751db5e3394c66b6 diff --git a/doc/guix.texi b/doc/guix.texi index 2b1ad77ba5..e38c2c4b9b 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 @@ -40193,6 +40194,80 @@ Service Reference pointing to the given file. @end defvar +@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} 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 "192.0.2.1" "example.com" + '("example.net" "example.org")) + (host "2001:db8::1" "example.com" + '("example.net" "example.org")))) +@end lisp + +@quotation Note +@cindex @file{/etc/host} default entries +By default @file{/etc/host} comes with the following entries: +@example +127.0.0.1 localhost @var{host-name} +::1 localhost @var{host-name} +@end example + +For most setups this is what you want though if you find yourself in +the situation where you want to change the default entries, you can +do so in @code{operating-system}.@pxref{operating-system Reference,@code{essential-services}} + +The following example shows how one would unset @var{host-name} +from being an alias of @code{localhost}. +@lisp +(operating-system + ;; @dots{} + + (essential-services + (modify-services + (operation-system-default-essential-services this-operating-system) + (hosts-service-type config => (list + (host "127.0.0.1" "localhost") + (host "::1" "localhost"))))) + + ;; @dots{} +) +@end lisp +@end quotation + +@deftp {Data Type} host +Available @code{host} 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 + +@defun host address canonical-name [aliases] +Procedure for creating @code{host} records. +@end defun + +@quotation Note +The @code{host} data type constructor is @code{%host} though it is +tiresome to create multiple records with it so in practice the procedure +@code{host} (which wraps around @code{%host}) is used instead. +@end quotation +@end defvar + @defvar 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/base.scm b/gnu/services/base.scm index 9e799445d2..53eda9ea1e 100644 --- a/gnu/services/base.scm +++ b/gnu/services/base.scm @@ -20,6 +20,7 @@ ;;; Copyright © 2022 Guillaume Le Vaillant ;;; Copyright © 2022 Justin Veilleux ;;; Copyright © 2022 ( +;;; Copyright © 2023 Bruno Victal ;;; ;;; This file is part of GNU Guix. ;;; @@ -103,6 +104,14 @@ (define-module (gnu services base) console-font-service virtual-terminal-service-type + host + %host + host? + host-address + host-canonical-name + host-aliases + hosts-service-type + static-networking static-networking? static-networking-addresses @@ -685,6 +694,67 @@ (define* (rngd-service #:key (rngd-configuration (rng-tools rng-tools) (device device)))) + +;;; +;;; /etc/hosts +;;; + +(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 + ;; XXX: Using the record type constructor becomes tiresome when + ;; there's multiple records to make. + make-host host? + (address host-address) + (canonical-name host-canonical-name + (sanitize assert-valid-name)) + (aliases host-aliases + (default '()) + (sanitize (cut map assert-valid-name <>)))) + +(define* (host address canonical-name #:optional (aliases '())) + "Public constructor for records." + (%host + (address address) + (canonical-name canonical-name) + (aliases aliases))) + +(define hosts-service-type + ;; Extend etc-service-type with a entry for @file{/etc/hosts}. + (let* ((serialize-host-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-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.")))) ;;;