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.")))