From patchwork Mon Jul 5 15:37:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 31178 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 7FA1B27BC81; Mon, 5 Jul 2021 16:38:11 +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,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 99A4E27BC78 for ; Mon, 5 Jul 2021 16:38:10 +0100 (BST) Received: from localhost ([::1]:37532 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0Qfh-0008NG-NG for patchwork@mira.cbaines.net; Mon, 05 Jul 2021 11:38:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0Qfa-0008Kg-DA for guix-patches@gnu.org; Mon, 05 Jul 2021 11:38:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:34956) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0Qfa-00067H-5p for guix-patches@gnu.org; Mon, 05 Jul 2021 11:38:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m0Qfa-0006fB-4U for guix-patches@gnu.org; Mon, 05 Jul 2021 11:38:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49419] [PATCH 1/4] home-services: Add most essential home services References: <87y2akhiz1.fsf@trop.in> In-Reply-To: <87y2akhiz1.fsf@trop.in> Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 05 Jul 2021 15:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49419 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49419@debbugs.gnu.org Received: via spool by 49419-submit@debbugs.gnu.org id=B49419.162549944625563 (code B ref 49419); Mon, 05 Jul 2021 15:38:02 +0000 Received: (at 49419) by debbugs.gnu.org; 5 Jul 2021 15:37:26 +0000 Received: from localhost ([127.0.0.1]:46502 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0Qez-0006eF-IU for submit@debbugs.gnu.org; Mon, 05 Jul 2021 11:37:26 -0400 Received: from mail-lf1-f41.google.com ([209.85.167.41]:46628) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0Qex-0006e0-EG for 49419@debbugs.gnu.org; Mon, 05 Jul 2021 11:37:24 -0400 Received: by mail-lf1-f41.google.com with SMTP id p21so11394538lfj.13 for <49419@debbugs.gnu.org>; Mon, 05 Jul 2021 08:37:23 -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=iyJgr5O2mkA2akelmDVgRasC8dLxHj56J3bChxqZo0g=; b=m6CoXxGwRUyFJiVM9vk3TWmiuGmujHMdqfL/jYYfWF5+OgTLnwpH0UfIBRCwgml3Ma /yF/J4hiTDa+WaEL1A4OuUusfCRounceMQvFKUolQbeAu8L4gBXn9d079e/rwY1CL5d6 lMMiduBDcGPTlK9brPphRq2CKFRXy8J+X1h5VGgKUKRZ+sWjOvmBV8VhIvI4vHwhYl+Z oBfMHqjA/0s2etAPXtDRjFbjQmMkt16zzGXZwMSxLkF4uO9x6h9nw0omXOH8hWrQGVty T5LwAzNnypU1AW9WX5LYWmEUz8nsgpJmGM7iQPIDeqgFv7oa76PTKLMJuygBwXUm7ljs 4Z2g== 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=iyJgr5O2mkA2akelmDVgRasC8dLxHj56J3bChxqZo0g=; b=pqwWv1Oe35eBxr5+v3lfmS3G4BSP53AQFS46p3kKqy72ASFlGmZC0kRYOnAI81CUjj lDrVOh72Ko/nHvw79fFc8T+U76s9sfdtJYji5dnQyFrH4qodw7yt8ia0niCzq61zmN1W vFmNMIxaro7SdTo5CQBmmzq7jv3YXH3qVYTaqO6ZPDMAAiAQNff2mFquDzXRU9dgtb78 WOKpYnyQhy02wF24Qb37PmX9uae2B8DDzVOuD7QR6M3rUOmWn5dnzQHw7Q1kDwujR0lR mOB7VpV9xol7B9JrpGuvcB/ziZP3Lwzo0J7Dq6YMedp27SAx6tUbyMbcdlv3IlYu0qo+ ii8g== X-Gm-Message-State: AOAM53277UTkEtinUvSELSfAy4m4zJjxoaFiNs5vJ8FvOu8o0tMVPV+F LjCA+F9I3ehjZpkckTee7za3UH2Tm3vfIA== X-Google-Smtp-Source: ABdhPJw3wkgbKtETRvOFpi15aAvxLqPpQIU+zj3kvFdPAuLp1b2ykJOsTWaAG+i7U1irkpFwCgiflg== X-Received: by 2002:a05:6512:23a6:: with SMTP id c38mr11363573lfv.78.1625499436903; Mon, 05 Jul 2021 08:37:16 -0700 (PDT) Received: from ixy ([109.252.117.164]) by smtp.gmail.com with ESMTPSA id f2sm1069763lft.225.2021.07.05.08.37.15 for <49419@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 08:37:16 -0700 (PDT) From: Andrew Tropin Date: Mon, 05 Jul 2021 18:37:13 +0300 Message-ID: <87wnq4hivq.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 home-service-type is a root of home services DAG. home-profile-service-type is almost the same as profile-service-type, at least for now. home-environment-variables-service-type generates a @file{setup-environment} shell script, which is expected to be sourced by login shell or other program, which starts early and spawns all other processes. Home services for shells automatically add code for sourcing this file, if person do not use those home services they have to source this script manually in their's shell *profile file (details described in the manual). home-files-service-type is similar to etc-service-type, but doesn't extend home-activation, because deploy mechanism for config files is pluggable and can be different for different home environments: The default one is called symlink-manager (will be introudced in a separate patch series), which creates links for various dotfiles (like $XDG_CONFIG_HOME/$APP/...) to store, but is possible to implement alternative approaches like read-only home from Julien's guix-home-manager. home-run-on-first-login-service-type provides an @file{on-first-login} guile script, which runs provided gexps once, when user makes first login. It can be used to start user's Shepherd and maybe some other process. It relies on assumption that /run/user/$UID will be created on login by some login manager (elogind for example). home-activation-service-type provides an @file{activate} guile script, which do three main things: - Sets environment variables to the values declared in @file{setup-environment} shell script. It's necessary, because user can set for example XDG_CONFIG_HOME and it should be respected by activation gexp of symlink-manager. - Sets GUIX_NEW_HOME and possibly GUIX_OLD_HOME vars to paths in the store. Later those variables can be used by activation gexps, for example by symlink-manager or run-on-change services. - Run all activation gexps provided by other home services. --- gnu/home-services.scm | 328 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 gnu/home-services.scm diff --git a/gnu/home-services.scm b/gnu/home-services.scm new file mode 100644 index 0000000000..44a7e68934 --- /dev/null +++ b/gnu/home-services.scm @@ -0,0 +1,328 @@ +;;; 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) + #:use-module (gnu services) + #:use-module (guix channels) + #:use-module (guix monads) + #:use-module (guix store) + #:use-module (guix gexp) + #:use-module (guix profiles) + #:use-module (guix sets) + #:use-module (guix ui) + #:use-module (guix discovery) + #:use-module (guix diagnostics) + + #:use-module (srfi srfi-1) + #:use-module (ice-9 match) + + #:export (home-service-type + home-profile-service-type + home-environment-variables-service-type + home-files-service-type + home-run-on-first-login-service-type + home-activation-service-type) + + #:re-export (service + service-type + service-extension)) + +;;; Comment: +;;; +;;; This module is similar to (gnu system services) module, but +;;; provides Home Services, which are supposed to be used for building +;;; home-environment. +;;; +;;; Home Services use the same extension as System Services. Consult +;;; (gnu system services) module or manual for more information. +;;; +;;; Code: + + +(define (home-derivation entries mextensions) + "Return as a monadic value the derivation of the 'home' +directory containing the given entries." + (mlet %store-monad ((extensions (mapm/accumulate-builds identity + mextensions))) + (lower-object + (file-union "home" (append entries (concatenate extensions)))))) + +(define home-service-type + ;; This is the ultimate service type, the root of the home service + ;; DAG. The service of this type is extended by monadic name/item + ;; pairs. These items end up in the "home-environment directory" as + ;; returned by 'home-environment-derivation'. + (service-type (name 'home) + (extensions '()) + (compose identity) + (extend home-derivation) + (default-value '()) + (description + "Build the home environment top-level directory, +which in turn refers to everything the home environment needs: its +packages, configuration files, activation script, and so on."))) + +(define (packages->profile-entry packages) + "Return a system entry for the profile containing PACKAGES." + ;; XXX: 'mlet' is needed here for one reason: to get the proper + ;; '%current-target' and '%current-target-system' bindings when + ;; 'packages->manifest' is called, and thus when the 'package-inputs' + ;; etc. procedures are called on PACKAGES. That way, conditionals in those + ;; inputs see the "correct" value of these two parameters. See + ;; . + (mlet %store-monad ((_ (current-target-system))) + (return `(("profile" ,(profile + (content (packages->manifest + (map identity + ;;(options->transformation transformations) + (delete-duplicates packages eq?)))))))))) + +;; MAYBE: Add a list of transformations for packages. It's better to +;; place it in home-profile-service-type to affect all profile +;; packages and prevent conflicts, when other packages relies on +;; non-transformed version of package. +(define home-profile-service-type + (service-type (name 'home-profile) + (extensions + (list (service-extension home-service-type + packages->profile-entry))) + (compose concatenate) + (extend append) + (description + "This is the @dfn{home profile} and can be found in +@file{~/.guix-home/profile}. It contains packages and +configuration files that the user has declared in their +@code{home-environment} record."))) + +(define (environment-variables->setup-environment-script vars) + "Return a file that can be sourced by a POSIX compliant shell which +initializes the environment. The file will source the home +environment profile, set some default environment variables, and set +environment variables provided in @code{vars}. @code{vars} is a list +of pairs (@code{(key . value)}), @code{key} is a string and +@code{value} is a string or gexp. + +If value is @code{#f} variable will be omitted. +If value is @code{#t} variable will be just exported. +For any other, value variable will be set to the @code{value} and +exported." + (define (warn-about-duplicate-defenitions) + (fold + (lambda (x acc) + (when (equal? (car x) (car acc)) + (warning + (G_ "duplicate definition for `~a' environment variable ~%") (car x))) + x) + (cons "" "") + (sort vars (lambda (a b) + (stringsetup-environment-script))) + (compose concatenate) + (extend append) + (default-value '()) + (description "Set the environment variables."))) + +(define (files->files-directory files) + "Return a @code{files} directory that contains FILES." + (define (assert-no-duplicates files) + (let loop ((files files) + (seen (set))) + (match files + (() #t) + (((file _) rest ...) + (when (set-contains? seen file) + (raise (formatted-message (G_ "duplicate '~a' entry for files/") + file))) + (loop rest (set-insert file seen)))))) + + ;; Detect duplicates early instead of letting them through, eventually + ;; leading to a build failure of "files.drv". + (assert-no-duplicates files) + + (file-union "files" files)) + +(define (files-entry files) + "Return an entry for the @file{~/.guix-home/files} +directory containing FILES." + (with-monad %store-monad + (return `(("files" ,(files->files-directory files)))))) + +(define home-files-service-type + (service-type (name 'home-files) + (extensions + (list (service-extension home-service-type + files-entry))) + (compose concatenate) + (extend append) + (default-value '()) + (description "Configuration files for programs that +will be put in @file{~/.guix-home/files}."))) + +(define (compute-on-first-login-script _ gexps) + (gexp->script + "on-first-login" + #~(let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR") + (format #f "/run/user/~a" (getuid)))) + (flag-file-path (string-append + xdg-runtime-dir "/on-first-login-executed")) + (touch (lambda (file-name) + (call-with-output-file file-name (const #t))))) + ;; XDG_RUNTIME_DIR dissapears on logout, that means such trick + ;; allows to launch on-first-login script on first login only + ;; after complete logout/reboot. + (when (not (file-exists? flag-file-path)) + (begin #$@gexps (touch flag-file-path)))))) + +(define (on-first-login-script-entry m-on-first-login) + "Return, as a monadic value, an entry for the on-first-login script +in the home environment directory." + (mlet %store-monad ((on-first-login m-on-first-login)) + (return `(("on-first-login" ,on-first-login))))) + +(define home-run-on-first-login-service-type + (service-type (name 'home-run-on-first-login) + (extensions + (list (service-extension + home-service-type + on-first-login-script-entry))) + (compose identity) + (extend compute-on-first-login-script) + (default-value #f) + (description "Run gexps on first user login. Can be +extended with one gexp."))) + + +(define (compute-activation-script init-gexp gexps) + (gexp->script + "activate" + #~(let* ((he-init-file (lambda (he) (string-append he "/setup-environment"))) + (he-path (string-append (getenv "HOME") "/.guix-home")) + (new-home-env (getenv "GUIX_NEW_HOME")) + (new-home (or new-home-env + ;; Path of the activation file if called interactively + (dirname (car (command-line))))) + (old-home-env (getenv "GUIX_OLD_HOME")) + (old-home (or old-home-env + (if (file-exists? (he-init-file he-path)) + (readlink he-path) + #f)))) + (if (file-exists? (he-init-file new-home)) + (let* ((port ((@@ (ice-9 popen) open-input-pipe) + (format #f "source ~a && env" + (he-init-file new-home)))) + (result ((@@ (ice-9 rdelim) read-delimited) "" port)) + (vars (map (lambda (x) + (let ((si (string-index x #\=))) + (cons (string-take x si) + (string-drop x (1+ si))))) + ((@@ (srfi srfi-1) remove) + string-null? + (string-split result #\newline))))) + (close-port port) + (map (lambda (x) (setenv (car x) (cdr x))) vars) + + (setenv "GUIX_NEW_HOME" new-home) + (setenv "GUIX_OLD_HOME" old-home) + + #$@gexps + + ;; Do not unset env variable if it was set outside. + (unless new-home-env (setenv "GUIX_NEW_HOME" #f)) + (unless old-home-env (setenv "GUIX_OLD_HOME" #f))) + (format #t "\ +Activation script was either called or loaded by file from this direcotry: +~a +It doesn't seem that home environment is somewhere around. +Make sure that you call ./activate by symlink from -home store item.\n" + new-home))))) + +(define (activation-script-entry m-activation) + "Return, as a monadic value, an entry for the activation script +in the home environment directory." + (mlet %store-monad ((activation m-activation)) + (return `(("activate" ,activation))))) + +(define home-activation-service-type + (service-type (name 'home-activation) + (extensions + (list (service-extension + home-service-type + activation-script-entry))) + (compose identity) + (extend compute-activation-script) + (default-value #f) + (description "Run gexps to activate the current +generation of home environment and update the state of the home +directory. @command{activate} script automatically called during +reconfiguration or generation switching. This service can be extended +with one gexp, but many times, and all gexps must be idempotent."))) + From patchwork Mon Jul 5 15:39:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 31179 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 2BE9E27BC81; Mon, 5 Jul 2021 16:40:14 +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,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 A3AF727BC78 for ; Mon, 5 Jul 2021 16:40:13 +0100 (BST) Received: from localhost ([::1]:39864 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0Qhg-0001Yn-NP for patchwork@mira.cbaines.net; Mon, 05 Jul 2021 11:40:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0QhW-0001Vq-TN for guix-patches@gnu.org; Mon, 05 Jul 2021 11:40:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:34960) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0QhW-0007OP-K6 for guix-patches@gnu.org; Mon, 05 Jul 2021 11:40:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m0QhW-0006iL-IB for guix-patches@gnu.org; Mon, 05 Jul 2021 11:40:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49419] [PATCH 2/4] home-services: Add home-run-on-change-service-type References: <87y2akhiz1.fsf@trop.in> In-Reply-To: <87y2akhiz1.fsf@trop.in> Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 05 Jul 2021 15:40:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49419 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49419@debbugs.gnu.org Received: via spool by 49419-submit@debbugs.gnu.org id=B49419.162549959425792 (code B ref 49419); Mon, 05 Jul 2021 15:40:02 +0000 Received: (at 49419) by debbugs.gnu.org; 5 Jul 2021 15:39:54 +0000 Received: from localhost ([127.0.0.1]:46506 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0QhO-0006hw-HS for submit@debbugs.gnu.org; Mon, 05 Jul 2021 11:39:54 -0400 Received: from mail-lf1-f45.google.com ([209.85.167.45]:47101) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0QhN-0006hj-Aa for 49419@debbugs.gnu.org; Mon, 05 Jul 2021 11:39:53 -0400 Received: by mail-lf1-f45.google.com with SMTP id p21so11405270lfj.13 for <49419@debbugs.gnu.org>; Mon, 05 Jul 2021 08:39:53 -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=olmO8mEMNwH4BVsJGexsIySLLrOtlHomIvmMBQz/qWo=; b=ucVJhbYCfmkKeVxCGSZFV64NTvUTibFoDAcun5FDSjX0wkRj3d/YypGva/TAe0Tj3v /bOJRNBGYux3ECCXNS3chjxK0t3V1tEloSnVPMix78BbermrY6S9JCmhUHwTAqqjQvoP Irww3vdndUiiJXumJCUr86x3K3SOrgm34M93cGyjGhfWrcilFqNK6lcHPVwWo8Q5EUpr G1fUFLsixtnXJXSOLntZ1GD/gj3cq4XmiJEHUj9VNy/6FJk45ki0KRjPAOvqVHcPS43b kPuiVzjwTFSEufif9Tu4XBZO5lE8Ce7e8LAmd3797gg7Xye/lhkKUkVeFilUsYff2yhp OV0g== 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=olmO8mEMNwH4BVsJGexsIySLLrOtlHomIvmMBQz/qWo=; b=hdiOLJgVmG6eFP6jomCxcvzWiUiEeNfSOR4ISMnkjPxi9DSPimZLPPATPE3AcI+5aY SR6iNrrxauf5Y6xHoZzslPflq0sm3oMttce+hmYa/J2t+WGNQbXqoYr2cUWcH9qbDYC/ zag00ucqDi9Tx8Vk46C3qkId9yJnBd5fDkOufRjDP0cADVY2iNoiIisIMPkiLIb5WbZ/ c/k9aPxzxTQKecWSBIEAyJEaoLJYQzMOsZKTESxhqPb5JVCxAP1EB9xJ9HGjLKiKh6vi jjSo+hKMdEqPObk/TiDL4G3msxjX9OOsguLznCnI/fJ/OQUhcV0fNyqzKOgpOlC/6Ey9 Sbag== X-Gm-Message-State: AOAM532y1+WvYxzs9MrLxYe1OG5kdUTSQ0X1gE/DEvPuxj5ZZ2vDzbnE 1P9wEHJbOeFBBayZ8mksbJQSABBdlVG6Cw== X-Google-Smtp-Source: ABdhPJxmQJ+1EqoPiIjE6I8Oe/pgroF+qdPArWhwATYNKEFFP/BhLB0kD5lSIMKnTILeG4Hpqrf/9Q== X-Received: by 2002:ac2:5a4c:: with SMTP id r12mr9475789lfn.498.1625499586863; Mon, 05 Jul 2021 08:39:46 -0700 (PDT) Received: from ixy ([109.252.117.164]) by smtp.gmail.com with ESMTPSA id u3sm212824lfu.241.2021.07.05.08.39.45 for <49419@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 08:39:46 -0700 (PDT) From: Andrew Tropin Date: Mon, 05 Jul 2021 18:39:44 +0300 Message-ID: <87v95ohirj.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 Service allows to trigger actions during activation if file or directory specified by pattern is changed. --- gnu/home-services.scm | 95 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/gnu/home-services.scm b/gnu/home-services.scm index 44a7e68934..002a003d65 100644 --- a/gnu/home-services.scm +++ b/gnu/home-services.scm @@ -37,7 +37,8 @@ home-environment-variables-service-type home-files-service-type home-run-on-first-login-service-type - home-activation-service-type) + home-activation-service-type + home-run-on-change-service-type) #:re-export (service service-type @@ -326,3 +327,95 @@ directory. @command{activate} script automatically called during reconfiguration or generation switching. This service can be extended with one gexp, but many times, and all gexps must be idempotent."))) + +;;; +;;; On-change. +;;; + +(define (compute-on-change-gexp eval-gexps? pattern-gexp-tuples) + #~(begin + (define (equal-regulars? file1 file2) + "Check if FILE1 and FILE2 are bit for bit identical." + (let* ((cmp-binary #$(file-append + (@@ (gnu packages base) diffutils) "/bin/cmp")) + (status (system* cmp-binary file1 file2))) + (= status 0))) + + (define (equal-symlinks? symlink1 symlink2) + "Check if SYMLINK1 and SYMLINK2 are pointing to the same target." + (string=? (readlink symlink1) (readlink symlink2))) + + (define (equal-directories? dir1 dir2) + "Check if DIR1 and DIR2 have the same content." + (define (ordinary-file file) + (not (or (string=? file ".") + (string=? file "..")))) + (let* ((files1 (scandir dir1 ordinary-file)) + (files2 (scandir dir2 ordinary-file))) + (if (equal? files1 files2) + (map (lambda (file) + (equal-files? + (string-append dir1 "/" file) + (string-append dir2 "/" file))) + files1) + #f))) + + (define (equal-files? file1 file2) + "Compares files, symlinks or directories of the same type." + (case (file-type file1) + ((directory) (equal-directories? file1 file2)) + ((symlink) (equal-symlinks? file1 file2)) + ((regular) (equal-regulars? file1 file2)) + (else + (display "The file type is unsupported by on-change service.\n") + #f))) + + (define (file-type file) + (stat:type (lstat file))) + + (define (something-changed? file1 file2) + (cond + ((and (not (file-exists? file1)) + (not (file-exists? file2))) #f) + ((or (not (file-exists? file1)) + (not (file-exists? file2))) #t) + + ((not (eq? (file-type file1) (file-type file2))) #t) + + (else + (not (equal-files? file1 file2))))) + + (define expressions-to-eval + (map + (lambda (x) + (let* ((file1 (string-append (getenv "GUIX_OLD_HOME") "/" (car x))) + (file2 (string-append (getenv "GUIX_NEW_HOME") "/" (car x))) + (_ (format #t "Comparing ~a and\n~10t~a..." file1 file2)) + (any-changes? (something-changed? file1 file2)) + (_ (format #t " done (~a)\n" + (if any-changes? "changed" "same")))) + (if any-changes? (cadr x) ""))) + '#$pattern-gexp-tuples)) + + (if #$eval-gexps? + (begin + (display "Evaling on-change gexps.\n\n") + (for-each primitive-eval expressions-to-eval) + (display "On-change gexps evaluation finished.\n\n")) + (display "\ +On-change gexps won't evaluated, disabled by service configuration.\n")))) + +(define home-run-on-change-service-type + (service-type (name 'home-run-on-change) + (extensions + (list (service-extension + home-activation-service-type + identity))) + (compose concatenate) + (extend compute-on-change-gexp) + (default-value #t) + (description "\ +G-expressions to run if the specified files have changed since the +last generation. The extension should be a list of lists where the +first element is the pattern for file or directory that expected to be +changed, and the second element is the G-expression to be evaluated."))) From patchwork Mon Jul 5 15:41:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 31181 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 28EC327BC78; Mon, 5 Jul 2021 16:42:11 +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,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 9907427BC81 for ; Mon, 5 Jul 2021 16:42:10 +0100 (BST) Received: from localhost ([::1]:43496 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0QjZ-00049r-NK for patchwork@mira.cbaines.net; Mon, 05 Jul 2021 11:42:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58876) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0QjS-000489-O1 for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:34968) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0QjS-0000F1-GR for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m0QjS-0006mE-2F for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49419] [PATCH 3/4] home-services: Add home-provenance-service-type References: <87y2akhiz1.fsf@trop.in> In-Reply-To: <87y2akhiz1.fsf@trop.in> Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 05 Jul 2021 15:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49419 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49419@debbugs.gnu.org Received: via spool by 49419-submit@debbugs.gnu.org id=B49419.162549967825976 (code B ref 49419); Mon, 05 Jul 2021 15:42:02 +0000 Received: (at 49419) by debbugs.gnu.org; 5 Jul 2021 15:41:18 +0000 Received: from localhost ([127.0.0.1]:46511 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0Qij-0006ku-WA for submit@debbugs.gnu.org; Mon, 05 Jul 2021 11:41:18 -0400 Received: from mail-lj1-f171.google.com ([209.85.208.171]:37430) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0Qii-0006kh-PO for 49419@debbugs.gnu.org; Mon, 05 Jul 2021 11:41:17 -0400 Received: by mail-lj1-f171.google.com with SMTP id k8so25141055lja.4 for <49419@debbugs.gnu.org>; Mon, 05 Jul 2021 08:41:16 -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=bdHvBm0ThHKYKK9a/VSWh3+Kmiw7MCeF7dEvTLC4654=; b=fxbFZOm13fbaTFuGbxMle/FePkrbLm4YDSrGO7dsXaqlJw+UHdfNSI+D0lVxZmRqZH SPiSgXjboPsZeK/b+po65BpGNsb1J6PTtOQF5qEoZfljsWUGUmpTj2A40XyGb9P/reXJ GwV58MZlIjUZmX/hlvb1cC7l2y02ScstZZyPg5Fny+fMz6kX0gr1URCFv26youbcpc6/ UAsbT/3b5Gsws0I5SRmyqqaWWikOiWWV4zHBb3b6+mht4+WKMJBAQTdZxesS88AfKlBS 3DFclBRLMmEfCuCHtT9BnTUxvBFVSWUKVEnhApZJGYk8OUyqEi4jiqlfw23iws7QRC4p iOtA== 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=bdHvBm0ThHKYKK9a/VSWh3+Kmiw7MCeF7dEvTLC4654=; b=GrFJ9z+pt6mnYVGHSocH/RlStoakVt3yTNSLsvlPHm0SCihZRhxvLYd9/WnOQjCS+P wVORAEX3ui7wNwbhkbKdNJ37uaZ71SY74s2s7umWeZuYOfUlVI+fTIdaU2+8stJEOwbX IO9zIR4pW1Lr4WH56S1O7bADS5+Lwo9/mEvNSbyGjy2fw3d9Oz2u37jjJ5W7uYTeGj64 NKkJDzFbRkq4qF6FrAx52vvrLi2PO/3gGb7JIlbOn4ddeDqo4w0sWQrf876UvXSFqlPn LzB8Z135gwjd3M9Vqbo/VSsr4bcGvlHTZZlOHvhjaxmx2BTbXNDGTeQ0KhsVpwdAGdRw IKnQ== X-Gm-Message-State: AOAM530Ur2idepbLeTAmJYTSvs3cLhu3TqdlQdAMnxxvcQJnmbmZpMTG OoL0SdTj2vWgCKfzEpu4x0ufrBoVkyS8Ew== X-Google-Smtp-Source: ABdhPJzI6pN7sspy+bXsrrketO/YK9Fbd8nEIWty4gWfH24c6Gn0j++XPkxXmEafV8cPYqdOkVpYGA== X-Received: by 2002:a2e:3e07:: with SMTP id l7mr11475368lja.131.1625499670354; Mon, 05 Jul 2021 08:41:10 -0700 (PDT) Received: from ixy ([109.252.117.164]) by smtp.gmail.com with ESMTPSA id u2sm1119646lff.37.2021.07.05.08.41.08 for <49419@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 08:41:09 -0700 (PDT) From: Andrew Tropin Date: Mon, 05 Jul 2021 18:41:07 +0300 Message-ID: <87sg0ship8.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 * gnu/home-services.scm (home-provenance-service-type, sexp->home-provenance, home-provenance): New variables. --- gnu/home-services.scm | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/gnu/home-services.scm b/gnu/home-services.scm index 002a003d65..20a9537650 100644 --- a/gnu/home-services.scm +++ b/gnu/home-services.scm @@ -38,7 +38,10 @@ home-files-service-type home-run-on-first-login-service-type home-activation-service-type - home-run-on-change-service-type) + home-run-on-change-service-type + home-provenance-service-type + + fold-home-service-types) #:re-export (service service-type @@ -419,3 +422,25 @@ G-expressions to run if the specified files have changed since the last generation. The extension should be a list of lists where the first element is the pattern for file or directory that expected to be changed, and the second element is the G-expression to be evaluated."))) + + +;;; +;;; Provenance tracking. +;;; + +(define home-provenance-service-type + (service-type + (name 'home-provenance) + (extensions + (list (service-extension + home-service-type + (service-extension-compute + (first (service-type-extensions provenance-service-type)))))) + (default-value #f) ;the HE config file + (description "\ +Store provenance information about the home environment in the home +environment itself: the channels used when building the home +environment, and its configuration file, when available."))) + +(define sexp->home-provenance sexp->system-provenance) +(define home-provenance system-provenance) From patchwork Mon Jul 5 15:41:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Tropin X-Patchwork-Id: 31180 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 04BE327BC83; Mon, 5 Jul 2021 16:42:11 +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,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 944A027BC78 for ; Mon, 5 Jul 2021 16:42:10 +0100 (BST) Received: from localhost ([::1]:43494 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0QjZ-00049o-Lz for patchwork@mira.cbaines.net; Mon, 05 Jul 2021 11:42:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58878) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0QjS-00048A-Oo for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:34969) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0QjS-0000F5-HA for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1m0QjS-0006mM-Fi for guix-patches@gnu.org; Mon, 05 Jul 2021 11:42:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49419] [PATCH 4/4] home-services: Add fold-home-service-types function References: <87y2akhiz1.fsf@trop.in> In-Reply-To: <87y2akhiz1.fsf@trop.in> Resent-From: Andrew Tropin Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 05 Jul 2021 15:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49419 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49419@debbugs.gnu.org Received: via spool by 49419-submit@debbugs.gnu.org id=B49419.162549971626039 (code B ref 49419); Mon, 05 Jul 2021 15:42:02 +0000 Received: (at 49419) by debbugs.gnu.org; 5 Jul 2021 15:41:56 +0000 Received: from localhost ([127.0.0.1]:46514 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0QjM-0006lt-8r for submit@debbugs.gnu.org; Mon, 05 Jul 2021 11:41:56 -0400 Received: from mail-lf1-f48.google.com ([209.85.167.48]:40555) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1m0QjK-0006lb-5y for 49419@debbugs.gnu.org; Mon, 05 Jul 2021 11:41:54 -0400 Received: by mail-lf1-f48.google.com with SMTP id q18so33137168lfc.7 for <49419@debbugs.gnu.org>; Mon, 05 Jul 2021 08:41:54 -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=OTM/NfLS28K1HZuhTzrT9b9se42Y6/V4cVqAWD8LQag=; b=O4R7fUQHRH48IOHQfxpGOLIqoYae1jf+xaAxDm+m2femv/zRGVOjZXAtzs4285Fw2g 1x9d15Lrvzhg5VFBNt9QawrjQFCudu+aXwqRtPduTbx/QbBrok4c94ZoSG81NtuuaL9o yfnlVf82dg5TZXrrvZ5FP+NI5UlF2iMfo89c/4LeOpt3beSt4qcLLQ/7o8h8DCgTdFAx AP/xcia4XfcwKBRDJjYvWY+B1+BfxfrkUwWblWMI7aE9Ht7DTRV6sA1t1o6NkaVB8DJO 3uC8sGZwW7jTrIZOgzkRSMJLjvzy/G29lxtaIIEIqObAqTN3YzZqViJYlrn5tn0UqOX8 E7vw== 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=OTM/NfLS28K1HZuhTzrT9b9se42Y6/V4cVqAWD8LQag=; b=XhE9/a/7SwDcQtEhwspZCJh+lvRo5tZVXP/NnHvOUFH/g/anY4E5Y6ZF1LKeNEasdf kPgI3JY5n9bwi3fgGxJ/Y/1Y7EfxABizegTtwbUsz2uJ3UUPnh5zq51tce+9ysjtrMAR Gu09+iwC2whGdJpO6xRfUTF+cr2CiEVPAi4Ijf1mKMvTDeMdmg8b3F5OBsF0ntkQ/VyF aE73+D4GqA1qP113J9+J0HXiQA1uuLr7Hi/85EgG6Q4rijEO2TnnqOsOGXjVfR2YIqUZ 53on0EY4KUzu2rDF7gYAT1ciYKcNwdEiaTPe2lRHPAGSMgU7tOwuotIeXxPIN8lYk6ew e44g== X-Gm-Message-State: AOAM530/XgxIJ56tFECe+bIi1J58G0wtV2eOHnyZ9DJnlRZeANQ4aN44 JEiV/s6vZp8Q/jyWJ8UGHReyn/DxpFVfBQ== X-Google-Smtp-Source: ABdhPJyIIaYNAprn7ubbkL4iqg8fbImP7kG5ix0KgDcFbESGTfxh63+ugv9OKBRPBlXvE3Q8xxECNA== X-Received: by 2002:a19:f807:: with SMTP id a7mr11243949lff.149.1625499708129; Mon, 05 Jul 2021 08:41:48 -0700 (PDT) Received: from ixy ([109.252.117.164]) by smtp.gmail.com with ESMTPSA id y1sm1293242ljc.29.2021.07.05.08.41.47 for <49419@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jul 2021 08:41:47 -0700 (PDT) From: Andrew Tropin Date: Mon, 05 Jul 2021 18:41:45 +0300 Message-ID: <87pmvwhio6.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 * gnu/home-services.scm (parent-directory, %guix-home-root-directory, %service-type-path, all-home-service-modules, fold-home-service-types): New variables. --- gnu/home-services.scm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gnu/home-services.scm b/gnu/home-services.scm index 20a9537650..a06cd72459 100644 --- a/gnu/home-services.scm +++ b/gnu/home-services.scm @@ -444,3 +444,29 @@ environment, and its configuration file, when available."))) (define sexp->home-provenance sexp->system-provenance) (define home-provenance system-provenance) + + +;;; +;;; Searching +;;; + +(define (parent-directory directory) + "Get the parent directory of DIRECTORY" + (string-join (drop-right (string-split directory #\/) 1) "/")) + +(define %guix-home-root-directory + ;; Absolute file name of the module hierarchy. + (parent-directory (dirname (search-path %load-path "gnu/home-services.scm")))) + +(define %service-type-path + ;; Search path for service types. + (make-parameter `((,%guix-home-root-directory . "gnu/home-services")))) + +(define (all-home-service-modules) + "Return the default set of home-service modules." + (cons (resolve-interface '(gnu home-services)) + (all-modules (%service-type-path) + #:warn warn-about-load-error))) + +(define* (fold-home-service-types proc seed) + (fold-service-types proc seed (all-home-service-modules)))