From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id eENvNPw81GNQ2AAAbAwnHQ (envelope-from ) for ; Fri, 27 Jan 2023 22:07:08 +0100 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id kHc8NPw81GM0uwAAauVa8A (envelope-from ) for ; Fri, 27 Jan 2023 22:07:08 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 92EEA384FB for ; Fri, 27 Jan 2023 22:07:07 +0100 (CET) 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1674853628; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=B/YEC1U/L2O7lHYW1RU4JtZ6aOTUI4dXmyAjHfDYRAU=; b=q39+3PmyVdt3uQIzCIXsMonoplwvfmnXszTonyHYeTekr32tmduO4Sro4eUG8QRgXNx6da jmKFc5PHKFgqwq2t9c6TF3TzW6AqTOWYWf1tEKENdVyt81L7Cn9+G/QZmKP2QupcdEOYQp B384F3PKr66E5SGluwItB8qsRs5QNIcQ/S0EPGQnivffhIXy+TdiMpd+/R14LAFSfcw673 NPj1zHwM7L8UQ6RQsQQ3ji0kOwQMuPKX2Pz01f1awBvMbCGxWiIlniZ6gM/lhJb8Pm6Ms3 tGGgo3A4m4kdv8wH4OAun8pDBpomomjoI1d/kXktNorXHpGiTNXaPeGzc/sRKw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1674853628; a=rsa-sha256; cv=none; b=ZM35EcMIBZZMQG6U0kDi88l8ASWz/rUC4q8rz5+pghQQB7AYX4uUtj8l0PnEIB06ETEVeY acYNhfbAOOwukjRdxBqNz1+4Vf8cLEQHyej3Rwk8+bhANnr87tso82DaAzE6UCZE9K/QDH dbCfJgwD5PxvjsJ7V9WS15gta/0cAWAleabceVQXSq6H+JYSaLGbNEjSBUWx9r+3AareST Mf3VXHLwTZo/ZiYGRl8nuuE+saJeGlY9t9kZh9w4r0p3b/tOx89QCWQbLNhHw050jFgvz/ shaBs/K1X39cmp1xT79stM32j7brZI94jkE63LTTnMQw0PgiGJw3cSpi/T6TdQ== Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Scanner: scn0.migadu.com X-Migadu-Spam-Score: -2.47 X-Spam-Score: -2.47 X-Migadu-Queue-Id: 92EEA384FB X-TUID: vqC/LclDb+01 * 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(+) 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.")))) ;;; base-commit: 35e626f312aa5f8c9c4c3f06751db5e3394c66b6 -- 2.38.1