From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id CL3RMRf1QmG9CgEAgWs5BA (envelope-from ) for ; Thu, 16 Sep 2021 09:41:11 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id UIucLRf1QmHtFwAA1q6Kng (envelope-from ) for ; Thu, 16 Sep 2021 07:41:11 +0000 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 022981292F for ; Thu, 16 Sep 2021 09:41:11 +0200 (CEST) Received: from localhost ([::1]:41624 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQm18-0001Ml-0K for larch@yhetil.org; Thu, 16 Sep 2021 03:41:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36102) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQm10-0001KM-R8 for guix-patches@gnu.org; Thu, 16 Sep 2021 03:41:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42132) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mQm10-0002ih-IW for guix-patches@gnu.org; Thu, 16 Sep 2021 03:41:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mQm10-0000qe-68 for guix-patches@gnu.org; Thu, 16 Sep 2021 03:41:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50350] [PATCH core-updates] utils: Add helpers for list environment variables. Resent-From: Xinglu Chen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 16 Sep 2021 07:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50350 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: Sarah Morgensen , 50350@debbugs.gnu.org Received: via spool by 50350-submit@debbugs.gnu.org id=B50350.16317780343218 (code B ref 50350); Thu, 16 Sep 2021 07:41:02 +0000 Received: (at 50350) by debbugs.gnu.org; 16 Sep 2021 07:40:34 +0000 Received: from localhost ([127.0.0.1]:53678 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mQm0Y-0000pp-2m for submit@debbugs.gnu.org; Thu, 16 Sep 2021 03:40:34 -0400 Received: from h87-96-130-155.cust.a3fiber.se ([87.96.130.155]:54898 helo=mail.yoctocell.xyz) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mQm0T-0000pV-SA for 50350@debbugs.gnu.org; Thu, 16 Sep 2021 03:40:32 -0400 From: Xinglu Chen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yoctocell.xyz; s=mail; t=1631778020; bh=8XXyyz4mJ2Qa46vCHmrzKD0XqxTIiAA0+Aub3Hf6/LE=; h=From:To:Subject:In-Reply-To:References:Date; b=b1fB6agyyiXKaUDh3/Hj0tIl/Y5+VGPWIi5vACaIhxpQg9PsIhJWGWOdzF6wyGe1F aqBjH+CefVVUTkNyBaY+mXdY3W6yryADlFCRRQ/bfKTbe9S8y7CskeRXZGaTO2AS6T RVlE3Cc9XrBDqoqXfAb46JZaDVKfORqyGBi/W2Js= In-Reply-To: <40d8e502f754fd7bbbe3a916950a85d1f1c57073.1630646500.git.iskarian@mgsn.dev> References: <40d8e502f754fd7bbbe3a916950a85d1f1c57073.1630646500.git.iskarian@mgsn.dev> Date: Thu, 16 Sep 2021 09:40:17 +0200 Message-ID: <874kalrmse.fsf@yoctocell.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" 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" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1631778071; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: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: dkim-signature; bh=zzIGuD+pzt1M8ENqH3acYEQn5XvGTsTsrsDlIlabkzM=; b=eJnKIX3eS/2vfv6DUPfIEdE7lnMEzl7pSScxpukxLRHQ1mfdHz4LAufu4Xy1eoESsrOjUt jg1FL+vEB4bGg73e07b+ZF2cCy097EjuclokmS8hssN7i6dUw9PBtCfha+mq5N8/eL0Abp CVIJG+9Vk/LCNT0H3HjRUzByI6+vOBYoBe4ejkXDt9E7SSzG2ai97Ix1tCBGugQF//aK0h o2baqsU8mpM37Ia6opNm8Du9Qxizs8DGocYih/g3LRzlej1jInEB3ByoLgm7+kEjS9FwLz nG5mj3/H2+UBiB1NBSmP3IHl9RNuV1lNta/BqCoLPDFow5/a4JwDgCJyMQmjAg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1631778071; a=rsa-sha256; cv=none; b=q5AdyEiTxUO7ywlU09yL07+by/NdrcJve5ZrSl1tnCUjb0pAydv2FF6Ncw5PEEeEndKmcd t1iwLlKSgcra63YJa4lj5RQc3ItOuydeaIZCsWXhbiVQCbtpUGPMWTxYG8GrpqBxaQlXSB EEfLoFvKlnq/mowjEyBOzXSGXswevFbHykzE0FAYD3ri2lKmKh5MNU55tLFcHlcficNo/R UK8hDQp+YNm3MSSiSIk+zumT7qW3TW+Hbg8XaFnM+lEeslDInkK1rELga+GJVoH/M15rfo fTckSJCigiiK4o75o/TIQXSc7+moGJ90+sDdp/zH0W4+GUXQ36f5Wzl3ID+WIw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=yoctocell.xyz header.s=mail header.b=b1fB6agy; dmarc=fail reason="SPF not aligned (relaxed)" header.from=yoctocell.xyz (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -3.40 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=yoctocell.xyz header.s=mail header.b=b1fB6agy; dmarc=fail reason="SPF not aligned (relaxed)" header.from=yoctocell.xyz (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: 022981292F X-Spam-Score: -3.40 X-Migadu-Scanner: scn1.migadu.com X-TUID: YV5zfvk5mJYp --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, Sep 02 2021, Sarah Morgensen wrote: > Add helpers 'getenv/list', 'setenv/list', 'setenv/list-extend', and > 'setenv/list-delete' for list environment variables (such as search > paths). > > * guix/build/utils.scm (getenv/list, setenv/list) > (setenv/list-extend, setenv/list-delete): New procedures. > * .dir-locals.el (scheme-mode): Indent them. > * tests/build-utils.scm ("getenv/list", "getenv/list: unset") > ("setenv/list: ignore empty elements") > ("setenv/list: unset if empty") > ("setenv/list-extend: single element, prepend") > ("setenv/list-extend: multiple elements, prepend") > ("setenv/list-extend: multiple elements, append") > ("setenv/list-delete: single deletion") > ("setenv/list-delete: multiple deletions"): New tests. > --- > Hello Guix, > > I noticed that there are over 200 occurrences of this pattern in packages: > > --8<---------------cut here---------------start------------->8--- > (setenv "PYTHONPATH" > (string-append (getcwd) ":" (getenv "PYTHONPATH"))) > --8<---------------cut here---------------end--------------->8--- > > This patch introduces some helper procedures for these kinds of cases. Wi= th > this patch, instead of the above you could write: > > (setenv/list-extend "PYTHONPATH" (getcwd)) When I hear the word =E2=80=9Cextend=E2=80=9D, I would usually think that I= am adding something to the end of something else (appending). In this case, it is prepending the value rather than appending the value. But since prepending something to an environment variable seems more common, it might not be worth it to change it from prepending to appending by default. > Sometimes you want to add to the end of the path: > > --8<---------------cut here---------------start------------->8--- > (setenv "GEM_PATH" > (string-append (getenv "GEM_PATH") ":" new-gem)) > --8<---------------cut here---------------end--------------->8--- > > With this patch, you could write instead: > > (setenv/list-extend "GEM_PATH" new-gem #:prepend? #f) > > Adding include paths becomes much more readable in conjunction with > search-input-directory, with this: > > --8<---------------cut here---------------start------------->8--- > (setenv "CPATH" > (string-append (assoc-ref inputs "libtirpc") > "/include/tirpc/:" > (or (getenv "CPATH") ""))) > --8<---------------cut here---------------end--------------->8--- > > becoming this: > > (setenv/list-extend "CPATH" > (search-input-directory "/include/tirpc")) > > A less common case, of removing a path: > > --8<---------------cut here---------------start------------->8--- > (setenv "CPLUS_INCLUDE_PATH" > (string-join > (delete (string-append gcc "/include/c++") > (string-split (getenv "CPLUS_INCLUDE_PATH") #\:)) > ":")) > --8<---------------cut here---------------end--------------->8--- > > becomes this: > > (setenv/list-delete "CPLUS_INCLUDE_PATH" > (string-append gcc "/include/c++")) > > What do you all think? Thanks for working on this! I have running into situations like these lately. > (Bikeshed opportunity: I'm not in love with the names. I originally named > these 'setenv/path' rather than 'setenv/list', because I wanted to avoid > confusion with Guix's search paths, but I'm not sure 'setenv/list' is act= ually > more clear. > > I considered getenv*, setenv*, and so on, but I didn't think they were qu= ite > clear enough either. > > I did consider 'setenv/list-extend!' and 'setenv/list-delete!' since they= do > modify the env var in place, but "setenv" should already imply that. I would prefer =E2=80=98setenv/extend=E2=80=99 and =E2=80=98setenv/delete= =E2=80=99; the second argument can be a string or a list, so I don=E2=80=99t think it needs to have =E2=80= =98list-=E2=80=99 in the name. > Finally, it might be better to have e.g. 'setenv/path-prepend!' and > 'setenv/path-append!' rather than the single 'setenv/path-extend', but I = could > not settle on memorable, representative names. Using 'append' carries a > connotation that you are dealing with lists, because of 'append', but it = also > accepts a single element. Using 'extend'/'prepend' together seems confus= ing > to me, because I might reach for 'extend' to add to the beginning of the = list > if I forget about 'prepend'.) > > -- > Sarah > > .dir-locals.el | 4 ++++ > guix/build/utils.scm | 56 +++++++++++++++++++++++++++++++++++++++++++ > tests/build-utils.scm | 53 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 113 insertions(+) > > diff --git a/.dir-locals.el b/.dir-locals.el > index 919ed1d1c4..4b58220526 100644 > --- a/.dir-locals.el > +++ b/.dir-locals.el > @@ -69,6 +69,10 @@ > (eval . (put 'add-before 'scheme-indent-function 2)) > (eval . (put 'add-after 'scheme-indent-function 2)) >=20=20 > + (eval . (put 'setenv/list 'scheme-indent-function 1)) > + (eval . (put 'setenv/list-extend 'scheme-indent-function 1)) > + (eval . (put 'setenv/list-delete 'scheme-indent-function 1)) > + > (eval . (put 'modify-services 'scheme-indent-function 1)) > (eval . (put 'with-directory-excursion 'scheme-indent-function 1)) > (eval . (put 'with-file-lock 'scheme-indent-function 1)) > diff --git a/guix/build/utils.scm b/guix/build/utils.scm > index 3beb7da67a..d0ac33a64f 100644 > --- a/guix/build/utils.scm > +++ b/guix/build/utils.scm > @@ -8,6 +8,7 @@ > ;;; Copyright =C2=A9 2020 Efraim Flashner > ;;; Copyright =C2=A9 2020, 2021 Maxim Cournoyer > ;;; Copyright =C2=A9 2021 Maxime Devos > +;;; Copyright =C2=A9 2021 Sarah Morgensen > ;;; > ;;; This file is part of GNU Guix. > ;;; > @@ -75,6 +76,11 @@ > find-files > false-if-file-not-found >=20=20 > + getenv/list > + setenv/list > + setenv/list-extend > + setenv/list-delete > + > search-path-as-list > set-path-environment-variable > search-path-as-string->list > @@ -521,6 +527,56 @@ also be included. If FAIL-ON-ERROR? is true, raise = an exception upon error." > #f > (apply throw args))))) >=20=20 > + > +;;; > +;;; Multiple-valued environment variables. > +;;; > + > +(define* (setenv/list env-var lst #:key (separator #\:)) > + "Set environment variable ENV-VAR to the elements of LST separated by > +SEPARATOR. Empty elements are ignored. If ENV-VAR would be set to the = empty > +string, unset ENV-VAR." > + (let ((path (string-join (delete "" lst) (string separator)))) > + (if (string-null? path) > + (unsetenv env-var) > + (setenv env-var path)))) > + > +(define* (getenv/list env-var #:key (separator #\:)) > + "Return a list of the SEPARATOR-separated elements of environment vari= able > +ENV-VAR, or the empty list if ENV-VAR is unset." It might be worth mentioning that SEPARATOR has be a char-set; a string won=E2=80=99t cut it. ;-) > + (or (and=3D> (getenv env-var) > + (cut string-split <> separator)) > + '())) > + > +(define* (setenv/list-extend env-var list-or-str > + #:key (separator #\:) (prepend? #t)) > + "Add the element(s) LIST-OR-STR to the environment variable ENV-VAR us= ing > +SEPARATOR between elements. Empty elements are ignored. Elements are p= laced > +at the beginning if PREPEND? is #t, or at the end otherwise." > + (let* ((elements (match list-or-str > + ((? string? str) (list str)) > + ((? list? lst) lst))) > + (original (or (getenv env-var) "")) > + (path-list (if prepend? > + (append elements (list original)) > + (cons original elements)))) > + (when (not (null? elements)) Use =E2=80=98unless=E2=80=99 instead of (when (not ...)) > + (setenv/list env-var path-list #:separator separator)))) > + > +(define* (setenv/list-delete env-var list-or-str #:key (separator #\:)) > + "Remove the element(s) LIST-OR-STR from the SEPARATOR-separated enviro= nment > +variable ENV-VAR, and set ENV-VAR to that value. If ENV-VAR would be se= t to > +the empty string, unset ENV-VAR." > + (let* ((elements (match list-or-str > + ((? string? str) (list str)) > + ((? list? lst) lst))) > + (original (getenv/list env-var #:separator separator)) > + (path-list (lset-difference string=3D? original elements)) > + (path (string-join path-list (string separator)))) > + (if (string-null? path) > + (unsetenv env-var) > + (setenv env-var path)))) The new procedures should probably be documented in the manual. The rest LGTM! --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJJBAEBCAAzFiEEAVhh4yyK5+SEykIzrPUJmaL7XHkFAmFC9OEVHHB1YmxpY0B5 b2N0b2NlbGwueHl6AAoJEKz1CZmi+1x5gL8P/2aRYRhQZbEULq4n6C+0CGRySJTL zOBvvbvJU+b5xrZ85Lffo90vAEaHIm+OkWH+NJ0UDdaWUwW/7+tUoRNOgxKHXPVy I1XWua57IjbkjtouG0CZk14Z8zYhNyumiIUwWnbg4d/MIT5VvcSqZ7fK1g4ovu57 JsdnVBjn0PtDj1f1mi3mcN+uTQrlWr4te/8qkpusbelu4tHP/ABZ7KKxV+QQKuVM 4tdyGCBA2sk2SWoSytJLvEJjK04jPR/gTkGvpt5M/jRxu7WmpGGOgDeh2o5GFOp0 GhxXGA3WClvyGk9ojKzxLphonDEIYCNSxeuCiV4Fmar1ePpFQ8QdgM5rYcxjPALP 62f5l64J14VFRPcLLpIB9xmuw4kVbr2Z/4i2L7B9FWrkG6jMv3zn1TP31u8K3enR +tjPEVQ6wdgsuMPCpMCmEJMlKPu3tMAIAsrLVpjorizh6M2EGA6QEuvQOR98rQq4 jFQS1ZQ0cS9zTC8+kbsvTSqFotIYbUKqA4nHbNY4o/5tE20BnnK7maPKZttyTuZS r1C1JQMG6Ogbg3cVroc5Ck+SSlCdc3H25qp2GDdarOkaJSUP+dHfXZVqqlNMc3d0 otYF0htJLgicKHVv0zAf+mQqAnlPsU64FRYfVjUkAqLdkH1ZMDsqlBsFEEdNCCKk NIkkmt5O1RsaykPm =dlF2 -----END PGP SIGNATURE----- --=-=-=--