From patchwork Thu Sep 2 09:33:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 32510 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 64E3327BBE3; Thu, 2 Sep 2021 10:47:43 +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=unavailable 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 7EE0227BBE1 for ; Thu, 2 Sep 2021 10:47:42 +0100 (BST) Received: from localhost ([::1]:42642 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mLjJt-0007ay-Hk for patchwork@mira.cbaines.net; Thu, 02 Sep 2021 05:47:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54584) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mLjAY-00054V-VO for guix-patches@gnu.org; Thu, 02 Sep 2021 05:38:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:56528) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mLjAY-0006ML-Mk for guix-patches@gnu.org; Thu, 02 Sep 2021 05:38:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mLjAY-0002r1-JU for guix-patches@gnu.org; Thu, 02 Sep 2021 05:38:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50332] [PATCH] home-services: Add Shepherd. Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 02 Sep 2021 09:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 50332 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 50332@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.163057545910940 (code B ref -1); Thu, 02 Sep 2021 09:38:02 +0000 Received: (at submit) by debbugs.gnu.org; 2 Sep 2021 09:37:39 +0000 Received: from localhost ([127.0.0.1]:39841 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mLjAA-0002qO-Ve for submit@debbugs.gnu.org; Thu, 02 Sep 2021 05:37:39 -0400 Received: from lists.gnu.org ([209.51.188.17]:44722) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mLjA8-0002qF-5I for submit@debbugs.gnu.org; Thu, 02 Sep 2021 05:37:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54550) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mLjA7-00041y-TM for guix-patches@gnu.org; Thu, 02 Sep 2021 05:37:35 -0400 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]:36395) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mLjA5-0005y3-KX for guix-patches@gnu.org; Thu, 02 Sep 2021 05:37:35 -0400 Received: by mail-lj1-x22a.google.com with SMTP id p15so2400037ljn.3 for ; Thu, 02 Sep 2021 02:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trop-in.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version; bh=TWITtcevWRcHYyF9HlYa3kvQVF3ofa5rZLfpsWg6X9M=; b=Y+CCtfbzPryGFK8psR9scXbiTwQP3KwXtPxcTua9EeQJwi303V6Ksqml5qsu5uH3hC yD1o++sEHPOcMciI1WFmX5meG+e9TDUJ73HJBTy499vdZzXIMTAZwyjFIpWKlo2xfK5Z yLtqz2uvGdi4MH+9nkck5MCdUKURUXFEmtf5LDypbPA+AhB6HMuFtuWEyeBwGZXm6YJi TrYPioPii8Chu/kGFX0Q0/rKwL5NMHoImDA6A+ujSg9X6rP46vcUF/HyxoRGL6NLw4d2 d0qvyCIW9QEcr58eoJu4G10vZTwIv/6atxV9gDgnhBxzIzV1O3AY1gvCeB8/nd1zVaFP gdRQ== 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:date:message-id:mime-version; bh=TWITtcevWRcHYyF9HlYa3kvQVF3ofa5rZLfpsWg6X9M=; b=kn6n1IJuw2HCnki69+q2UGmf01SA3EMb4KAzzSDWv06FRZyANGRbhX/ZJ1/49FB2Ue iMPKlIxfeNxZpnH5vTsJngBMR2LEncd1es6ZhBS0QIUpo4pPRL57FRR9R3sRQZy/Kkzf I1sRQHmW5NuQpwSDEyHORUbr0OShlQQVHoHYP5WzHbhJwYcqQurFsbottxDsBudG3/na ELK7O68xJRHRlLed87G4MvY1OAP8RJ4Y+i5R1iHgPXA0sqRFYn1Q8N2P8pSDWxTKdp1N fkRQEbuxNY3YHVZXYcOYOLwCRtGbtc4ANiq3BpTBmQt5QYeq2gdTMzSQR6TZZSq2mMCh KMIA== X-Gm-Message-State: AOAM53155gBmrIA/RoH77vQLj2Pj4JvtgWfM+OYNvLQPCMUVH6IUCRfT m9MXvnSe4vzd00b6Ntf09bYXGxBgyS5jYg== X-Google-Smtp-Source: ABdhPJyqOx5xAaxungCFBVyDGB4TFubKg5WD5vYH9mAwrwa+2uUkFpMl1imM6kqbBkd5tII/TpjDzw== X-Received: by 2002:a2e:9ac7:: with SMTP id p7mr1791246ljj.72.1630575450418; Thu, 02 Sep 2021 02:37:30 -0700 (PDT) Received: from localhost ([109.252.93.92]) by smtp.gmail.com with ESMTPSA id m7sm152331ljj.58.2021.09.02.02.37.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Sep 2021 02:37:29 -0700 (PDT) From: Andrew Tropin Date: Thu, 2 Sep 2021 12:33:36 +0300 Message-ID: <87lf4ffhd6.fsf@trop.in> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::22a; envelope-from=andrew@trop.in; helo=mail-lj1-x22a.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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 * 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 +long-running process or one-shot tasks. Almost all the 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}) +Wether 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..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...~%") + (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."))) + +