From patchwork Fri Sep 3 07:44:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 32535 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 8898627BBE4; Fri, 3 Sep 2021 08:45:45 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,T_DKIM_INVALID, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 328E027BBE3 for ; Fri, 3 Sep 2021 08:45:44 +0100 (BST) Received: from localhost ([::1]:49798 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mM3tP-0005bM-7N for patchwork@mira.cbaines.net; Fri, 03 Sep 2021 03:45:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mM3sk-0005b0-V5 for guix-patches@gnu.org; Fri, 03 Sep 2021 03:45:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:59440) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mM3sk-0005ZK-Mq for guix-patches@gnu.org; Fri, 03 Sep 2021 03:45:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mM3sk-0002nb-HU for guix-patches@gnu.org; Fri, 03 Sep 2021 03:45:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50332] [PATCH v2] home-services: Add Shepherd. Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 03 Sep 2021 07:45:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50332 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Xinglu Chen , 50332@debbugs.gnu.org Received: via spool by 50332-submit@debbugs.gnu.org id=B50332.163065507810699 (code B ref 50332); Fri, 03 Sep 2021 07:45:02 +0000 Received: (at 50332) by debbugs.gnu.org; 3 Sep 2021 07:44:38 +0000 Received: from localhost ([127.0.0.1]:42753 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mM3sL-0002mV-Ai for submit@debbugs.gnu.org; Fri, 03 Sep 2021 03:44:38 -0400 Received: from mail-lj1-f176.google.com ([209.85.208.176]:35460) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mM3sH-0002m3-Cb for 50332@debbugs.gnu.org; Fri, 03 Sep 2021 03:44:36 -0400 Received: by mail-lj1-f176.google.com with SMTP id y6so8283408lje.2 for <50332@debbugs.gnu.org>; Fri, 03 Sep 2021 00:44:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trop-in.20150623.gappssmtp.com; s=20150623; h=from:to:subject:in-reply-to:references:date:message-id:mime-version; bh=TVjA3lQ8vVA1tfTx5MI/pbnVDX9mrXlNZsivjC0Yl2k=; b=qo+h/mTThAkBKXtjLIgTSxEIF9kj1DQnYfx5syhvUPH9efQIgD84cb9v8PtKvpF9dM z/XjAbf140Ew8t1xp17CpkiKZQ6wbHF6OCD45LEFLJ++tyv3BWkskMCukK/zxBgHfhGd FgJWMKUyz6MUu52jJF+GI4mwofjJXJxbRPm53b6KRhOmaHmCP1t72CPF056/8QRye7jw Xch77LEThjw0yAzIltVceDh8rmC5YHeTqK2S6eQRI5V9+aIvURGnWOxfVEmt1Vr+HKNi jDX3Kl4B1+/OUrycdmUqd7yUbTWXluvQBAsGsYZup7iLCBufCaKT8iZkU1cDVor3SWIl lYzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:in-reply-to:references:date :message-id:mime-version; bh=TVjA3lQ8vVA1tfTx5MI/pbnVDX9mrXlNZsivjC0Yl2k=; b=SBumUnt74y2C8649vefO52tbO4xAv+eyhyJInxd+2W3naam2K0bmLAkIdvTvdVmH6J cjC+cZ5k717sbQQqTv2RQuR7QrJcaBGUf+Qw2IUTOVnfWOk4TIzjKbSKYhWeqET+pWAP c98yXRg6XJzCsw7eW73/vzO6TvjyBIyjiP6zkq/PAZPTlo6mHV7ckoHSjKvyY8Ajo1Uz kjE5oJpUDIz34qSTz8lSFDL39qLJBLWHOeudT1KtGuXfS5j0Ag472vEJMmFE+CiW8nib aauBMNSKOkWJtTZ+m/QLh2SyttgWyH7wmT38tvH+PWH44WDWHRRFoLZ+snobxDGSV/KA nvbQ== X-Gm-Message-State: AOAM532Ycd1egX4TkE/IA54MNcKlJtGdjXqpSXhwASUlv/NuAhMmRNuF +1qylhkBJhFqs/Ypzh8ImCCgwH2nR9gMhw== X-Google-Smtp-Source: ABdhPJwcN7d9/8iOgAG8ddFnZ2GdJ2k/tBEx3U4O6Ez4I6CNMsqO3BvULpyYXkRjY+4l4kuOeSkVwQ== X-Received: by 2002:a05:651c:1695:: with SMTP id bd21mr1931045ljb.5.1630655067108; Fri, 03 Sep 2021 00:44:27 -0700 (PDT) Received: from localhost ([109.252.93.92]) by smtp.gmail.com with ESMTPSA id g10sm426372lfb.87.2021.09.03.00.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 00:44:26 -0700 (PDT) From: Andrew Tropin In-Reply-To: <874kb3ja56.fsf@yoctocell.xyz> References: <87lf4ffhd6.fsf@trop.in> <874kb3ja56.fsf@yoctocell.xyz> Date: Fri, 03 Sep 2021 10:44:22 +0300 Message-ID: <871r66umqx.fsf@trop.in> 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" X-getmail-retrieved-from-mailbox: Patches On 2021-09-02 16:59, Xinglu Chen wrote: > On Thu, Sep 02 2021, Andrew Tropin wrote: > >> * gnu/home-services/shepherd.scm: New file. >> * doc/guix.texi: Add documentation about Shepherd Home Service. >> --- >> doc/guix.texi | 31 +++++++- >> gnu/home-services/shepherd.scm | 133 +++++++++++++++++++++++++++++++++ >> 2 files changed, 163 insertions(+), 1 deletion(-) >> create mode 100644 gnu/home-services/shepherd.scm >> >> diff --git a/doc/guix.texi b/doc/guix.texi >> index 622a973bdf..51a317e8a7 100644 >> --- a/doc/guix.texi >> +++ b/doc/guix.texi >> @@ -35538,7 +35538,36 @@ mcron info here >> >> @node Shepherd Home Service >> @subsection Managing User's Daemons >> -shepherd info here >> + >> +@cindex shepherd services >> + >> +@defvr {Scheme Variable} shepherd-home-service-type >> +The service type for the userland Shepherd, which allows to manage > > “allows one to manage” > >> +long-running process or one-shot tasks. Almost all the information > > s/process/processes/ > >> +described in (@pxref{Shepherd Services}) is applicable here too. > > What is not applicable? > Clarified. > >> +This is the service type that extensions target when they want to create >> +shepherd services (@pxref{Service Types and Services}, for an example). >> +Each extension must pass a list of @code{}. Its >> +value must be a @code{shepherd-configuration}, as described below. >> +@end defvr >> + >> +@deftp {Data Type} shepherd-configuration >> +This data type represents the Shepherd's configuration. >> + >> +@table @code >> +@item shepherd (default: @code{shepherd}) >> +The Shepherd package to use. >> + >> +@item auto-start? (default: @code{#t}) >> +Wether or not to start Shepherd on first login. > > s/Wether/Whether/ > >> +@item services (default: @code{'()}) >> +A list of @code{} to start. >> +You should probably use the service extension >> +mechanism instead (@pxref{Shepherd Services}). >> +@end table >> +@end deftp >> >> @node Invoking guix home >> @section Invoking @code{guix home} >> diff --git a/gnu/home-services/shepherd.scm b/gnu/home-services/shepherd.scm >> new file mode 100644 >> index 0000000000..158b50bdb6 >> --- /dev/null >> +++ b/gnu/home-services/shepherd.scm >> @@ -0,0 +1,133 @@ >> +;;; GNU Guix --- Functional package management for GNU >> +;;; Copyright © 2021 Andrew Tropin >> +;;; Copyright © 2021 Xinglu Chen >> +;;; >> +;;; This file is 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 . >> + >> +(define-module (gnu home-services shepherd) >> + #:use-module (gnu home-services) >> + #:use-module (gnu packages admin) >> + #:use-module (gnu services shepherd) >> + #:use-module (guix sets) >> + #:use-module (guix gexp) >> + #:use-module (guix i18n) >> + #:use-module (guix records) >> + >> + #:use-module (srfi srfi-1) >> + >> + #:re-export (shepherd-service >> + shepherd-action)) >> + >> +(define-record-type* >> + home-shepherd-configuration make-home-shepherd-configuration >> + home-shepherd-configuration? >> + (shepherd home-shepherd-configuration-shepherd >> + (default shepherd)) ; package >> + (auto-start? home-shepherd-configuration-auto-start? >> + (default #t)) >> + (services home-shepherd-configuration-services >> + (default '()))) >> + >> +(define (home-shepherd-configuration-file services shepherd) >> + "Return the shepherd configuration file for SERVICES. SHEPHERD is used >> +as shepherd package." >> + (assert-valid-graph services) >> + >> + (let ((files (map shepherd-service-file services)) >> + ;; TODO: Add compilation of services, it can improve start >> + ;; time. >> + ;; (scm->go (cute scm->go <> shepherd)) >> + ) >> + (define config >> + #~(begin >> + (use-modules (srfi srfi-34) >> + (system repl error-handling)) >> + (apply >> + register-services >> + (map >> + (lambda (file) (load file)) >> + '#$files)) >> + (action 'root 'daemonize) >> + (format #t "Starting services...~%") > > Maybe (G_ ...) should be used to make strings translatable? > > >> + (for-each >> + (lambda (service) (start service)) >> + '#$(append-map shepherd-service-provision >> + (filter shepherd-service-auto-start? >> + services))) >> + (newline))) > > Is ‘newline’ necessary? > Not necessary, but nice to have for better separation of different processes launched during activation. It could newline at the beginning of each activation subscript, but it is what it is) > >> + (scheme-file "shepherd.conf" config))) >> + >> +(define (launch-shepherd-gexp config) >> + (let* ((shepherd (home-shepherd-configuration-shepherd config)) >> + (services (home-shepherd-configuration-services config))) >> + (if (home-shepherd-configuration-auto-start? config) >> + (with-imported-modules '((guix build utils)) >> + #~(let ((log-dir (or (getenv "XDG_LOG_HOME") >> + (format #f "~a/.local/var/log" (getenv "HOME"))))) >> + ((@ (guix build utils) mkdir-p) log-dir) >> + (system* >> + #$(file-append shepherd "/bin/shepherd") >> + "--logfile" >> + (string-append >> + log-dir >> + "/shepherd.log") >> + "--config" >> + #$(home-shepherd-configuration-file services shepherd)))) >> + #~""))) >> + >> +(define (reload-configuration-gexp config) >> + (let* ((shepherd (home-shepherd-configuration-shepherd config)) >> + (services (home-shepherd-configuration-services config))) >> + #~(system* >> + #$(file-append shepherd "/bin/herd") >> + "load" "root" >> + #$(home-shepherd-configuration-file services shepherd)))) >> + >> +(define (ensure-shepherd-gexp config) >> + #~(if (file-exists? >> + (string-append >> + (or (getenv "XDG_RUNTIME_DIR") >> + (format #f "/run/user/~a" (getuid))) >> + "/shepherd/socket")) >> + #$(reload-configuration-gexp config) >> + #$(launch-shepherd-gexp config))) >> + >> +(define-public home-shepherd-service-type >> + (service-type (name 'home-shepherd) >> + (extensions >> + (list (service-extension >> + home-run-on-first-login-service-type >> + launch-shepherd-gexp) >> + (service-extension >> + home-activation-service-type >> + ensure-shepherd-gexp) >> + (service-extension >> + home-profile-service-type >> + (lambda (config) >> + `(,(home-shepherd-configuration-shepherd config)))))) > > Nit: I would use ‘list’ instead of quasiquoting and unquoting. > It's probably done this way to keep the line under 80 characters long, but I agree, using list would be a little more cleaner. > >> + (compose concatenate) >> + (extend >> + (lambda (config extra-services) >> + (home-shepherd-configuration >> + (inherit config) >> + (services >> + (append (home-shepherd-configuration-services config) >> + extra-services))))) >> + (default-value (home-shepherd-configuration)) >> + (description "Configure and install userland Shepherd."))) >> + >> + >> -- >> 2.33.0 Replied or addressed all the comments. From c12b51bdbc86fa995c88f331b5d04b0b963087cf Mon Sep 17 00:00:00 2001 From: Andrew Tropin Date: Thu, 2 Sep 2021 12:33:36 +0300 Subject: [PATCH v2] home-services: Add Shepherd. * gnu/home-services/shepherd.scm: New file. * doc/guix.texi: Add documentation about Shepherd Home Service. --- doc/guix.texi | 32 +++++++- gnu/home-services/shepherd.scm | 133 +++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 gnu/home-services/shepherd.scm diff --git a/doc/guix.texi b/doc/guix.texi index 622a973bdf..0591848f7e 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -35538,7 +35538,37 @@ mcron info here @node Shepherd Home Service @subsection Managing User's Daemons -shepherd info here + +@cindex shepherd services + +@defvr {Scheme Variable} home-shepherd-service-type +The service type for the userland Shepherd, which allows one to manage +long-running processes or one-shot tasks. User's Shepherd is not an +init process (PID 1), but almost all other information described in +(@pxref{Shepherd Services}) is applicable here too. + +This is the service type that extensions target when they want to create +shepherd services (@pxref{Service Types and Services}, for an example). +Each extension must pass a list of @code{}. Its +value must be a @code{shepherd-configuration}, as described below. +@end defvr + +@deftp {Data Type} shepherd-configuration +This data type represents the Shepherd's configuration. + +@table @code +@item shepherd (default: @code{shepherd}) +The Shepherd package to use. + +@item auto-start? (default: @code{#t}) +Whether or not to start Shepherd on first login. + +@item services (default: @code{'()}) +A list of @code{} to start. +You should probably use the service extension +mechanism instead (@pxref{Shepherd Services}). +@end table +@end deftp @node Invoking guix home @section Invoking @code{guix home} diff --git a/gnu/home-services/shepherd.scm b/gnu/home-services/shepherd.scm new file mode 100644 index 0000000000..04366f4b81 --- /dev/null +++ b/gnu/home-services/shepherd.scm @@ -0,0 +1,133 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2021 Andrew Tropin +;;; Copyright © 2021 Xinglu Chen +;;; +;;; This file is 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 . + +(define-module (gnu home-services shepherd) + #:use-module (gnu home-services) + #:use-module (gnu packages admin) + #:use-module (gnu services shepherd) + #:use-module (guix sets) + #:use-module (guix gexp) + #:use-module (guix i18n) + #:use-module (guix records) + + #:use-module (srfi srfi-1) + + #:re-export (shepherd-service + shepherd-action)) + +(define-record-type* + home-shepherd-configuration make-home-shepherd-configuration + home-shepherd-configuration? + (shepherd home-shepherd-configuration-shepherd + (default shepherd)) ; package + (auto-start? home-shepherd-configuration-auto-start? + (default #t)) + (services home-shepherd-configuration-services + (default '()))) + +(define (home-shepherd-configuration-file services shepherd) + "Return the shepherd configuration file for SERVICES. SHEPHERD is used +as shepherd package." + (assert-valid-graph services) + + (let ((files (map shepherd-service-file services)) + ;; TODO: Add compilation of services, it can improve start + ;; time. + ;; (scm->go (cute scm->go <> shepherd)) + ) + (define config + #~(begin + (use-modules (srfi srfi-34) + (system repl error-handling)) + (apply + register-services + (map + (lambda (file) (load file)) + '#$files)) + (action 'root 'daemonize) + (format #t (G_ "Starting services...~%")) + (for-each + (lambda (service) (start service)) + '#$(append-map shepherd-service-provision + (filter shepherd-service-auto-start? + services))) + (newline))) + + (scheme-file "shepherd.conf" config))) + +(define (launch-shepherd-gexp config) + (let* ((shepherd (home-shepherd-configuration-shepherd config)) + (services (home-shepherd-configuration-services config))) + (if (home-shepherd-configuration-auto-start? config) + (with-imported-modules '((guix build utils)) + #~(let ((log-dir (or (getenv "XDG_LOG_HOME") + (format #f "~a/.local/var/log" (getenv "HOME"))))) + ((@ (guix build utils) mkdir-p) log-dir) + (system* + #$(file-append shepherd "/bin/shepherd") + "--logfile" + (string-append + log-dir + "/shepherd.log") + "--config" + #$(home-shepherd-configuration-file services shepherd)))) + #~""))) + +(define (reload-configuration-gexp config) + (let* ((shepherd (home-shepherd-configuration-shepherd config)) + (services (home-shepherd-configuration-services config))) + #~(system* + #$(file-append shepherd "/bin/herd") + "load" "root" + #$(home-shepherd-configuration-file services shepherd)))) + +(define (ensure-shepherd-gexp config) + #~(if (file-exists? + (string-append + (or (getenv "XDG_RUNTIME_DIR") + (format #f "/run/user/~a" (getuid))) + "/shepherd/socket")) + #$(reload-configuration-gexp config) + #$(launch-shepherd-gexp config))) + +(define-public home-shepherd-service-type + (service-type (name 'home-shepherd) + (extensions + (list (service-extension + home-run-on-first-login-service-type + launch-shepherd-gexp) + (service-extension + home-activation-service-type + ensure-shepherd-gexp) + (service-extension + home-profile-service-type + (lambda (config) + `(,(home-shepherd-configuration-shepherd config)))))) + (compose concatenate) + (extend + (lambda (config extra-services) + (home-shepherd-configuration + (inherit config) + (services + (append (home-shepherd-configuration-services config) + extra-services))))) + (default-value (home-shepherd-configuration)) + (description "Configure and install userland Shepherd."))) + + -- 2.33.0